網絡流入門,模板POJ1273


Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 60838   Accepted: 23347

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

源點爲1,匯點爲m,模板題



#include <iostream>

#include <stdio.h>
#include <queue>
#include <cmath>
#include <string.h>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;


#define LL                                      long long
#define scan(a)                             scanf("%d",&a)
#define maxn                                222
#define REP(i,a,b)                          for(int i=a;i<b;++i)
#define mset(a,b)                           memset(a,b,sizeof a)


const LL mod = 1000000000;


int path[maxn],s,e,m,n;
const int INF = 0x7FFFFFFF ;
int flow[maxn][maxn],cap[maxn][maxn],minf[maxn];
queue<int> que;
vector<int> vec[maxn];


int bfs()
{
    while(!que.empty())  que.pop();
    mset(path,-1);
    //path[s]=s;
    minf[s]=INF;
    que.push(s);
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        if(u==e)    return 1;
        int sz=vec[u].size();
        REP(i,0,sz)
        {
            int nxt=vec[u][i];
            if(cap[u][nxt] <= flow[u][nxt] || path[nxt]!=-1 )   continue;
            path[nxt] = u;
            minf[nxt]=min(cap[u][nxt]-flow[u][nxt],minf[u]);
            que.push(nxt);
        }
    }
    return 0;
}


LL solve()
{
    LL ret=0;
    while(bfs())
    {
        for(int i=e;i!=s;i=path[i])
        {
            flow[path[i]][i]+=minf[e];
            flow[i][path[i]]-=minf[e];
        }
        ret+=minf[e];
    }
    return ret;
}


int main()
{
    s=1;
    while(cin>>n>>m)
    {
        //mset(minf,0);
        mset(flow,0);
        mset(cap,0);
        e=m;
        REP(i,0,m+1)    vec[i].clear();
        REP(i,0,n)
        {
            int u,v,c;
            scanf("%d%d%d",&u,&v,&c);
            vec[u].push_back(v);
            vec[v].push_back(u);
            cap[u][v]+=c;
        }
        printf("%I64d\n",solve());
    }
}





























你可能感兴趣的:(網絡流入門,模板POJ1273)