Roundtable problem (network flow 24 questions)

The meaning of problems

There are n number of units, each unit there are some people, there are m tables, each table can take a number of people, each unit can not sit at a table, output program.

1<=n<=150, 1<=m<=270。

answer

Beginning to everyone as a point, then turn into them one by one, is greedy (and this may have anything to do with network streaming), then he gave up. See the solution to a problem, really ** can be greedy, need some sort.

Solution then flows to the network, as the two parts of the unit and tables, even a side flow conditions can be ensured between the source node and the number of units connected to the unit side flow rate between the table and the unit, between the sink and the table even the table edge flow capacity.

(Stupid me)

Then pay a wave of the usual network stream Last write a crazy point T, obviously all very fast in front, and then a look at a lot of discussion is the last point T, and some say the current arc to optimize her.

(Do not want to learn) went to see solution to a problem, see a dalao say that this problem without dismantling point arc optimization, half the answer, xjb map building is clean (doubt face). A look feel nothing different thing, only in dfs inside one if (rest!) Break;

Plus direct result of take-off place.

#include<bits/stdc++.h>
using namespace std;

const int maxn=506;
const int maxm=90006;
int n,m,tot;
int cnt=1,s,t,head[maxn];
struct edge{
  int x,y,val,next;
}e[maxm];

void add(int x,int y,int val){
  e[++cnt]=(edge){x,y,val,head[x]};
  head[x]=cnt;
}

int d[maxn];

bool bfs(){
  queue<int> q;
  memset(d,0,sizeof(d));
  q.push(s);d[s]=1;
  while(!q.empty()){
    int x=q.front();
    q.pop();
    for(int i=head[x];i;i=e[i].next){
      int y=e[i].y;
      if(e[i].val&&!d[y]){
        d[y]=d[x]+1;
        if(y==t) return true;
        q.push(y);
      }
    }
  }
  return false;
}

int dfs(int x,int flow){
  if(x==t) return flow;
  int rest=flow,k;
  for(int i=head[x];i;i=e[i].next){
    int y=e[i].y;
    if(e[i].val&&d[y]==d[x]+1){
      k=dfs(y,min(rest,e[i].val));
      e[i].val-=k;
      e[i^1].val+=k;
      rest-=k;
      if(!rest) break;
    }
  }
  return flow-rest;
}

int dinic(){
  int res=0;
  while(bfs()) res+=dfs(s,0x3f3f3f);
  return res;
}

int main(){
  scanf("%d%d",&n,&m);
  s=0;t=n+m+1;
  for(int i=1;i<=n;i++){
    int x;scanf("%d",&x);
    tot+=x;
    add(s,i,x);
    add(i,s,0);
    for(int j=n+1;j<=n+m;j++){
      add(i,j,1);
      add(j,i,0);
    }
  }
  for(int i=n+1;i<=n+m;i++){
    int x;scanf("%d",&x);
    add(i,t,x);
    add(t,i,0);
  }
  int ans=dinic();
  if(ans!=tot){printf("0");return 0;}
  printf("1\n");
  for(int i=1;i<=n;i++){
    for(int j=head[i];j;j=e[j].next)
     if(!e[j].val) printf("%d ",e[j].y-n);
    putchar(10);
  }
}
View Code

 

Guess you like

Origin www.cnblogs.com/sto324/p/11353646.html