【uva11248】网络扩容

网络流裸题。

求完最大流之后保留残余容量信息,依次将已经加入最小割的弧变成c再跑,记录下即可。

#include<bits/stdc++.h>
#define N 20005
#define inf 1000000007
#define naive 0
using namespace std;
struct Edge{int u,v,f,next;}G[N<<1],E[N<<1];
int n,m,c,head[N],tot=naive,s,t,cnt,level[N];
struct edge{int u,v;edge(int u=0,int v=0):u(u),v(v){};}a[N];
bool cmp(edge a,edge b){if(a.u==b.u)return a.v<b.v;return a.u<b.u;}
inline void addedge(int u,int v,int f){
    G[tot].u=u;G[tot].v=v;G[tot].f=f;G[tot].next=head[u];head[u]=tot++;
    G[tot].u=v;G[tot].v=u;G[tot].f=0;G[tot].next=head[v];head[v]=tot++;
}
inline bool bfs(int s,int t){
    queue<int>q;
    memset(level,naive,sizeof(level));
    q.push(s);level[s]=1;
    while(!q.empty()){
        int u=q.front();q.pop();
        if(u==t)return 1;
        for(int i=head[u];~i;i=G[i].next){
            int v=G[i].v,f=G[i].f;
            if(f&&!level[v])q.push(v),level[v]=level[u]+1;
        }
    }
    return naive;
}
int dfs(int u,int maxf,int t){
    if(u==t)return maxf;int rat=0;
    for(int i=head[u];~i&&rat<maxf;i=G[i].next){
        int v=G[i].v,f=G[i].f;
        if(f&&level[v]==level[u]+1){
            f=dfs(v,min(f,maxf-rat),t);
            G[i].f-=f;G[i^1].f+=f;rat+=f;
        }
    }
    if(!rat)level[u]=inf;
    return rat;
}
inline int dinic(){int ans=0;while(bfs(s,t))ans+=dfs(s,inf,t);return ans;}
inline int read(){
    int f=1,x=naive;char ch;
    do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
    do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
    return f*x;
}
int main(){int T=naive;
    while(scanf("%d%d%d",&n,&m,&c)&&n){
        printf("Case %d: ",++T);
        memset(head,-1,sizeof(head));tot=0;s=1;t=n+1;cnt=0;
        for(int i=1;i<=m;i++){int u=read(),v=read(),f=read();addedge(u,v,f);}
        addedge(n,t,c);
        int ans=dinic();for(int i=0;i<tot;i++)E[i]=G[i];
        if(ans==c)puts("possible");
        else{
            for(int i=0;i<tot-2;i+=2)
            if(!G[i].f){
                for(int j=0;j<tot;j++)G[j]=E[j];
                G[i].f=c;int tmp=dinic()+ans;if(tmp==c)a[cnt++]=edge(G[i].u,G[i].v);
            }
            if(!cnt)puts("not possible");
            else{
                printf("possible option:");sort(a,a+cnt,cmp);
                printf("(%d,%d)",a[0].u,a[0].v);
                for(int i=1;i<cnt;i++)printf(",(%d,%d)",a[i].u,a[i].v);printf("\n");
            }
        }
    }
    return 0;
}

 

posted @ 2017-06-23 08:18  zcysky  阅读(174)  评论(0编辑  收藏  举报