HDU3549 NetWork Flow Problem

https://vjudge.net/problem/HDU-3549

The rudiments of Network Problem

I use the algorithm of Edomonds-Karp

You should pay attention to the double edge

And to improve my English power

I will write in English of my blog

another reason is the typewriting of Ubuntu is very hard to use 

The code of AC:

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10,inf=(1<<29);
int A[100][100];
int head[N],ver[N],edge[N],Next[N],incf[N],pre[N],v[N];
int s,t;
int maxflow;
int tot;
void add(int x,int y,int z){
    ver[++tot]=y;
    edge[tot]=z;
    Next[tot]=head[x];
    head[x]=tot;
    ver[++tot]=x;
    edge[tot]=0;
    Next[tot]=head[y];
    head[y]=tot;
}
bool bfs(){
    memset(v,0,sizeof v);
    queue<int> q;
    q.push(s);
    v[s]=1;
    incf[s]=inf;
    while(q.size()){
        int x=q.front();
        q.pop();
        for(int i=head[x];i;i=Next[i]){
            if(edge[i]){
                int y=ver[i];
                if(v[y]) continue;
                incf[y]=min(edge[i],incf[x]);
                v[y]=1;
                q.push(y);
                pre[y]=i;
                if(y==t) return 1;
            }
        }
    }
    return 0;
}
void update(){
    int x=t;
    while(x!=s){
        int i=pre[x];
        edge[i]-=incf[t];
        edge[i^1]+=incf[t];
        x=ver[i^1];
    }
    maxflow+=incf[t];
}
int main(){
    int T;
    int a,b,c;
    cin>>T;
    int flag=0;
    while(T--){
        int n,m;
        cin>>n>>m;
        tot=1;
        maxflow=0;
        s=1,t=n;
        memset(A,0,sizeof A);
        for(int i=0;i<N;++i){
            incf[i]=edge[i]=head[i]=Next[i]=ver[i]=pre[i]=v[i]=0;
        }
        for(int i=1;i<=m;++i){
            cin>>a>>b>>c;
            A[a][b]+=c;
        }
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                if(A[i][j]>0)
                    add(i,j,A[i][j]);
            }
        }
        while(bfs()) update();
        cout<<"Case "<<++flag<<": "<<maxflow<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/gipsy_danger/article/details/80259159
今日推荐