POJ

来源:互联网 发布:bonjour是什么软件 编辑:程序博客网 时间:2024/06/17 08:52

题目链接


                               Drainage Ditches

 POJ - 1273 
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 41 2 401 4 202 4 202 3 303 4 10
Sample Output
50
思路:这是个最大流的模板题,很容易理解。
#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>using namespace std;#define MAX 0x3f3f3f3f#define maxn 1010int map[maxn][maxn];    //原始最大的网络图int flow[maxn][maxn];   //能到终点的流量图int dis[maxn];          //标记父亲节点int vis[maxn];          //记录流到终点的流量int m,n;
int  Ford_fulkerson(int a,int k){    queue<int>Q;    memset(flow,0,sizeof(flow));    int sum=0;    while(1)    {        memset(vis,0,sizeof(vis));        vis[a]=MAX;        Q.push(a);        while(!Q.empty())        {            int p=Q.front();            Q.pop();            for(int i=1; i<=k; i++)                if(!vis[i]&&map[p][i]>flow[p][i]) //vis[i]判断不要出现循环                 {                    dis[i]=p;                    //记录父亲节点                    Q.push(i);                   //入队                    vis[i]=min(vis[p],map[p][i]-flow[p][i]);//寻找最小的流量                }        }        sum+=vis[k];   //寻找到一个增广路线,进行累加,        if(vis[k]==0)      //无增广路线,            return sum;        for(int i=k;i!=a;i=dis[i])    //找到一条增广路线,要把flow进行更新。        {            flow[i][dis[i]]-=vis[k];  //从终点开始,dis[i]是i的父亲节点,所以减少。               flow[dis[i]][i]+=vis[k];  //从终点开始,dis[i]是i的父亲节点,所以相加。        }    }}int main(){    while(~scanf("%d %d",&m,&n))    {        int a,b,c;        memset(map,0,sizeof(map));        for(int i=1; i<=m; i++)        {            scanf("%d %d %d",&a,&b,&c);            map[a][b]+=c;        }        printf("%d\n",Ford_fulkerson(1,n));    }    return 0;}