码迷,mamicode.com
首页 > 其他好文 > 详细

poj1273--Drainage Ditches(最大流)

时间:2014-08-18 20:31:32      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:des   os   io   strong   for   ar   cti   div   

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 56084   Accepted: 21547

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 <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define maxn 500
#define INF 0x3f3f3f3f
struct edge{
    int v , w ;
    int next ;
} p[maxn];
int head[maxn] , cnt , pre[maxn] , vis[maxn] ;
queue <int> q ;
void add(int u,int v,int w)
{
    p[cnt].v = v ; p[cnt].w = w ;
    p[cnt].next = head[u] ; head[u] = cnt++ ;
    p[cnt].v = u ; p[cnt].w = 0 ;
    p[cnt].next = head[v] ; head[v] = cnt++ ;
}
int bfs(int s,int t)
{
    int u , v , min1 = INF , i ;
    memset(vis,0,sizeof(vis));
    vis[s] = 1 ;
    while( !q.empty() )
        q.pop() ;
    q.push(s) ;
    while( !q.empty() )
    {
        u = q.front();
        q.pop() ;
        for(i = head[u] ; i != -1 ; i = p[i].next)
        {
            v = p[i].v ;
            if( !vis[v] && p[i].w )
            {
                vis[v] = 1 ;
                min1 = min(min1,p[i].w) ;
                pre[v] = i ;
                q.push(v) ;
            }
        }
    }
    if( vis[t] )
        return min1 ;
    return -1 ;
}
int main()
{
    int n , m , u , v , w , max_flow , i;
    while(scanf("%d %d", &m, &n)!=EOF)
    {
        cnt = 0 ;max_flow = 0 ;
        memset(head,-1,sizeof(head));
        while(m--)
        {
            scanf("%d %d %d", &u, &v, &w);
            add(u,v,w);
        }
        memset(pre,-1,sizeof(pre)) ;
        while(1)
        {
            int k = bfs(1,n);
            if(k == -1)
                break;
            max_flow += k ;
            for(i = pre[n] ; i != -1 ; i = pre[ p[i^1].v ])
            {
                p[i].w -= k ;
                p[i^1].w += k ;
            }
        }
        printf("%d\n", max_flow);
    }
    return 0;
}

poj1273--Drainage Ditches(最大流),布布扣,bubuko.com

poj1273--Drainage Ditches(最大流)

标签:des   os   io   strong   for   ar   cti   div   

原文地址:http://blog.csdn.net/winddreams/article/details/38665033

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!