POJ 1273Drainage Ditches(入门网络流)

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 83222   Accepted: 32330

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50
    #include<iostream>  
    #include<string>  
    #include<cstdio>  
    #include<queue>  
    #include<vector>  
    #include<algorithm>  
    #define LL long long  
    #define inf 1<<29  
    #define s(a) scanf("%d",&a)  
    #define CL(a,b) memset(a,b,sizeof(a))  
    using namespace std;  
    const int N=205;  
    int n,m,u,v,cost;  
    int Map[N][N],path[N],flow[N];  
    int start,endd;  
    queue<int>q;  
    int bfs()  
    {  
        while(!q.empty()) q.pop();  
        CL(path,-1);  
        path[start]=0,flow[start]=inf;  //  起始容量为inf;  
        q.push(start);  
        while(!q.empty()){  
            int t=q.front();  
            q.pop();  
            if(t==endd) break;  
            for(int i=1;i<=m;i++){      //  遍历可走路径;  
                if(i!=start&&path[i]==-1&&Map[t][i]){       //  符合这三种情况表示该路径可走;  
                    flow[i]=flow[t]<Map[t][i]?flow[t]:Map[t][i];  
                    q.push(i);  
                    path[i]=t;  
                }  
            }  
        }  
        if(path[endd]==-1) return -1;   //  说明没有找到可走路径,返回-1;  
        return flow[endd];              //  找到一条路径之后的增流量;  
    }  
    int Edmonds_Karp()  
    {  
        int max_flow=0,step,now,pre;  
        while((step=bfs())!=-1){    //  找不到路径之后便退出;  
            max_flow+=step;         //  累加流量;  
            now=endd;  
            while(now!=start){      //  将找到的路径进行反向处理,并更新实际容量;  
                pre=path[now];  
                Map[pre][now]-=step;        //  更新正向边的实际容量;  
                Map[now][pre]+=step;        //  添加反向边;  
                now=pre;  
            }  
        }  
        return max_flow;  
    }  
    int main()  
    {  
        while(~scanf("%d%d",&n,&m)){  
            CL(Map,0);  
            for(int i=0;i<n;i++){  
                scanf("%d%d%d",&u,&v,&cost);  
                Map[u][v]+=cost;        //  为防止一条边不止一次输入;  
            }  
            start=1,endd=m;  
            printf("%d\n",Edmonds_Karp());  
        }  
        return 0;  
    }  

猜你喜欢

转载自www.cnblogs.com/yi-ye-zhi-qiu/p/9086186.html