[Solution] problem space flight program issues

Space flight plan issue

Or the use of the minimum cut nature, this theme is called Maximum Weight closed subgraph.

Modeling is so

img

Minimum cut directly run, with prize money minus the minimum cut is the answer. Program is the last time bfs can traverse point

Consider minimum cut meaning, is deleted such that the edge \ (S, T \) do not communicate.

Similarly, because we can not change the relationship between the instrument and task-dependent, you can only change a point is selected in this state, so we let the instrument test and even side is \ (inf \) means that we can not change this relationship.

This is one of the bonus side of a flow restriction relationship, is a disadvantage: we consider the dove out of the bonus side of what it means

Consider cutting off the instrument side is what it means: This instrument side too small to make the experiment of casual flow stream is full.

Consider the total prize money minus the cost of what is the minimum cut is
\ [\ sum \ text {bonus} - \ sum \ text {Do winnings} - \ sum \ text {} do not buy equipment \]
In fact, we can consider each flow path of the flow, similar understanding can be obtained.

Other questions I was able to work independently, have to look at this question ... it is a solution to a problem I thought strong enough

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;  typedef long long ll;
int qaq,n;
inline int qr(){
      register int ret=0,f=0;
      register char c=getchar();
      while(c<48||c>57) f|=c==45,c=getchar();
      while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
      return f?-ret:ret;
}

inline int sp(){
      register int ret=0,f=0;
      register char c=getchar();
      while(c<48||c>57) f|=c==45,c=getchar();
      while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
      if(c=='\n') f=1;
      return f?-ret:ret;
}
const int maxn=1e2+5;
struct E{
      int to,nx,w;
      E(){to=nx=w=0;}
      E(const int&a,const int&b,const int&c){to=a;nx=b;w=c;}
}e[maxn<<4];
int head[maxn];
int cnt=1;
const int inf=0x3f3f3f3f;
int S,T,m;
inline void add(const int&fr,const int&to,const int&w,const int&f){
      //printf("fr=%d to=%d w=%d cnt=%d\n",fr,to,w,cnt);
      e[++cnt]=E(to,head[fr],w);
      head[fr]=cnt;
      if(f) add(to,fr,0,0);
}

int sum=0;
queue < int > q;
int d[maxn],cur[maxn];
inline bool bfs(){
      for(register int t=1;t<=m+n+2;++t) d[t]=0,cur[t]=head[t];
      d[S]=1;q.push(S);
      while(q.size()){
        register int now=q.front();
        q.pop();
        for(register int t=head[now];t;t=e[t].nx){
          if(e[t].w>0&&d[e[t].to]==0){
            d[e[t].to]=d[now]+1;
            q.push(e[t].to);        
          }
        }
      }
      return d[T];

}

int dfs(const int&now,int fl){
      if(now==T||fl==0)return fl;
      register int ret=0;
      for(register int&t=cur[now];t;t=e[t].nx){
        if(e[t].w>0&&d[e[t].to]==d[now]+1){
          int d=dfs(e[t].to,min(e[t].w,fl));
          e[t].w-=d;e[t^1].w+=d;ret+=d;fl-=d;
        }
      }
      return ret;
}


inline int dinic(){
      int ret=0;
      while(bfs())ret+=dfs(S,inf);
      return ret;
}


int main(){
#ifndef ONLINE_JUDGE
      //freopen("in.in","r",stdin);
      //freopen("out.out","w",stdout);
#endif
      n=qr();m=qr();
      S=1;T=n+m+2;
      for(register int t=1;t<=n;++t){
        int t1=qr();
        add(S,t+1,t1,1);
        sum+=t1;
        while(1){
          t1=sp();
          //cout<<t<<' '<<t1<<endl;
          add(t+1,abs(t1)+n+1,inf,1);
          if(t1<0)break;
        }
      }
      for(register int t=1;t<=m;++t) add(t+n+1,T,qr(),1);
      int ans=sum-dinic();
      for(register int t=head[S];t;t=e[t].nx)
        if(d[e[t].to]) printf("%d ",e[t].to-1);
      putchar('\n');
      for(register int t=head[T];t;t=e[t].nx)
        if(d[e[t].to]) printf("%d ",e[t].to-n-1);
      printf("\n%d\n",ans);
      return 0;
}

Guess you like

Origin www.cnblogs.com/winlere/p/11234631.html