"24 network flow problem" space flight program issues

Question

Where have

Solution

A minimum cut.
1.S even to the experiment, the right side is the cost of experimentation
2. Experimental connected to the instrument, the right side of infinity, because they can not cut.
3. The instrument is connected to the T, the right side of the instrument costs
run over a network to stream.

Code

#include<cstdio>
#include<algorithm>
#define N 10010
using namespace std;
struct node{int v,fr,c;}e[N<<1];
int tail[N],dis[N],gap[N],ans=0;
int n,m,x,y,S=0,T,cnt=1;
bool flag,vis[N];

inline int read()
{
    int x=0,f=0; char c=getchar();
    while (c<'0' || c>'9') f=(c=='-') ? 1:f,c=getchar();
    while (c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    if (c=='\r' || c=='\n') flag=1;
    return f ? -x:x;
}

inline void add(int u,int v,int c)
{
    e[++cnt]=(node){v,tail[u],c}; tail[u]=cnt;
    e[++cnt]=(node){u,tail[v],0}; tail[v]=cnt;
}

int dfs(int x,int hav)
{
    if (x==T) return hav;
    int gone=0;
    for (int p=tail[x],v,can;p;p=e[p].fr)
    {
        v=e[p].v;
        if (dis[x]!=dis[v]+1) continue;
        can=dfs(v,std::min(hav-gone,e[p].c));
        e[p].c-=can,e[p^1].c+=can,gone+=can;
        if (gone==hav) return gone;
    }
    gap[dis[x]]--;
    if (!gap[dis[x]]) dis[1]=T;
    gap[++dis[x]]++;
    return gone;
}

void DFS(int x)
{
    vis[x]=1;
    for (int p=tail[x];p;p=e[p].fr)
        if (!vis[e[p].v] && e[p].c) DFS(e[p].v);
}

int main()
{
    freopen("space.in","r",stdin);
    freopen("space.out","w",stdout);
    m=read(),n=read();T=n+m+1;
    for (int i=1;i<=m;i++)
    {
        x=read(),add(S,i,x);
        flag=0;ans+=x;
        while (!flag) y=read(),add(i,m+y,1010580540);
    }
    for (int i=1;i<=n;i++)
        x=read(),add(m+i,T,x);
    while (dis[1]<T) ans-=dfs(S,1010580540);
    DFS(S);
    x=1; while (!vis[x]) x++;
    printf("%d",x);
    for (int i=x+1;i<=m;i++)
        if (vis[i]) printf(" %d",i);
    x=m+1; while (!vis[x]) x++;
    printf("\n%d",x-m);
    for (int i=x+1;i<=m+n;i++)
        if (vis[i]) printf(" %d",i-m);
    printf("\n%d",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/jz929/p/11354456.html