Space flight program issues (network flow 24 questions)

The meaning of problems

There are m experiments, n th equipment, each experiment done will get some money, each experiment requires some equipment to complete, will spend money to buy equipment, equipment can be used together, seeking the most benefit.

n,m<=50

answer

Direct write approach, and I did not want it out.

Source connected to the experimental side, the resulting flow experiments money, equipment and even want to Meeting Point side, flow equipment spending, even experiment with the corresponding equipment side, the flow inf.

Assume no money to do all the experiments got all the money ans (not face).

Minimum cut can find it again, ans- minimum cut is the answer.

Even edge is not cut off experiment this experiment, even cut off the equipment side is to buy the equipment.

And outputs to output scheme s communication. (I do not know why)

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

const int maxn=105;
const int maxm=2705;
const int inf=1000005;
int n,m,s,t,ans;
int val[maxn];
bool flag,get[maxn];
int cnt=1,head[maxn];
struct edge{
  int x,y,next,val;
}e[maxm<<1];

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

template<class T>inline void read(T &x){
  x=0;char ch=getchar();
  while(!isdigit(ch)) ch=getchar();
  while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
  flag|=(ch=='\n');
}

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));
      if(!k) d[y]=0;
      e[i].val-=k;
      e[i^1].val+=k;
      rest-=k;
    }
  }
  return flow-rest;
}

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

int main(){
  read(m);read(n);
  s=0;t=n+m+1;
  for(int i=1;i<=m;i++){
    int x;read(x);
    ans+=x;
    add(s,i,x);
    add(i,s,0);
    flag=false;
    while(!flag){
      read(x);
      add(i,x+m,inf);
      add(x+m,i,0);
    }
  }
  for(int i=1;i<=n;i++){
    int x;read(x);
    add(i+m,t,x);
    add(t,i+m,0);
  }
  ans-=dinic();
  for(int i=1;i<=m;i++) if(d[i]) printf("%d ",i);
  putchar(10);
  for(int i=m+1;i<=n+m;i++) if(d[i]) printf("%d ",i-m);
  putchar(10);
  printf("%d",ans);
}
View Code

 

Guess you like

Origin www.cnblogs.com/sto324/p/11334074.html
Recommended