Drainage Ditches

                             Drainage Ditches

Time Limit:1000ms
Case Time Limit:1000ms
Memory Limit:10000KB
64-bit integer IO format:%lld      Java class name:Main
SubmitStatus
Font Size:
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 

解题思路:最基本的网络流。。。。。。

(1)建图,用矩阵存储flow[i][j]存储节点i到节点j的流量(残留网络)。

(2)找增广路径,用广度优先搜索找增广路劲,pre[i]记录节点i的访问前驱,min1[i]记录该增广路径的最大流量(最小分流量)[min1[i]=max(flow[temp][i],min1[temp])],返回该增广路径的最大流量。若未找到增广路径,则返回-1。

(3)若(2)的返回值为-1,输出网络最大流。否则,根据(2)的返回值,修改残余网络的流量和网络最大流,继续需找增广路径。


#include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; int m,n; int flow[205][205];   //残余网络流 int pre[205];  //记录前驱,用于寻找路径 int bfs()   //广度优先搜索寻找增广路径 {     int i,j;     int temp;     int min1[205];   //用于记录得出最增广路的最大流量     queue<int> q;     memset(pre,-1,sizeof(pre));     for(i=1;i<=n;i++) min1[i]=0xfffffff;     q.push(1);     while(!q.empty())     {         temp=q.front();q.pop();         if(temp==n) break;         for(i=2;i<=n;i++)         {             if(i!=temp&&flow[temp][i]>0&&pre[i]==-1)             {                 pre[i]=temp;                 min1[i]=flow[temp][i]<min1[temp]?flow[temp][i]:min1[temp];                 q.push(i);             }         }     }     if(pre[n]==-1) return -1;     return min1[n]; } long long EK()   //增广路径法求网络最大流 {     int temp;     long long sumflow=0;     int i,j;     while((temp=bfs())!=-1)  //找不到增广路径     {         i=n;         while(i!=1)   //已近到起点了         {             j=i;             i=pre[j];             flow[i][j]-=temp;             flow[j][i]+=temp;         }         sumflow+=temp;   //最大流增加     }     return sumflow; } void input() {     int i,j;     int x,y,d;     memset(flow,0,sizeof(flow));     for(i=0;i<m;i++)         scanf("%d%d%d",&x,&y,&d),flow[x][y]+=d; } int main() {     while(scanf("%d%d",&m,&n)!=EOF)         input(),printf("%lld\n",EK());     return 0; } 


你可能感兴趣的:(Drainage Ditches)