poj1273--Drainage Ditches(最大流)

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(最大流)

时间: 08-17

poj1273--Drainage Ditches(最大流)的相关文章

poj-1273 Drainage Ditches(最大流基础题)

题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted: 26075 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 cover

poj1273 Drainage Ditches(裸最大流)

Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Every time it rains on Farmer Joh

POJ1273 Drainage Ditches 【最大流Dinic】

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56870   Accepted: 21863 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

POJ-1273 Drainage Ditches 【最大流】

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

hdu 1532 Drainage Ditches(最大流)

Drainage Ditches 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 drai

poj 1273 Drainage Ditches (最大流入门)

1 /****************************************************************** 2 题目: Drainage Ditches(POJ 1273) 3 链接: http://poj.org/problem?id=1273 4 题意: 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条 5 水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水 6 渠中所能流过的水的最大容量.水流是单向的. 7 算法: 最大流之增广路(入门)

POJ1273 Drainage Ditches【最大流】【SAP】

题目链接: http://poj.org/problem?id=1273 题目大意: 农民John的田里有M个池塘和N条水沟用来排水,池塘编号为1~M,1号池塘是所有水沟的源点, M号池塘是水沟的汇点.给你N条水沟所连接的池塘和所能流过的水量,求整个水沟从源点到汇点 最多能流多少水. 思路: 很明显的求网络流最大流问题.用链式前向星(邻接表)来存储网络,这样就不用考虑重边问题了.这 里的重边其实就是平行边.用SAP算法+GAP优化来求最大流就可以了.SAP+GAP模板参考我的另 一篇博文:htt

[POJ1273]Drainage Ditches 网络流(最大流)

题目链接:http://poj.org/problem?id=1273 网络流裸题,注意有重边.重边的处理方法很简单,就是将对应的c叠加到对应边上.注意初始化为0. 我用的是最朴素的FF方法,即找增广路.之前用dfs找增广路WA了,应该是碰到了随机找一条增光路这种方法碰到了killer case.给出WA代码(初学用喜闻乐见链式前向星建图,比较繁琐.不过好在最终还是WA掉了). 1 #include <algorithm> 2 #include <iostream> 3 #incl

HDU 1532 Drainage Ditches(最大流 EK算法)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用"+="替换"=". 对网络流不熟悉的,给一篇讲解:http://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html. ?(? ? ??)我是看这篇博客才入门的. 代码: 1 #include <cstdio> 2 #includ

poj1273 Drainage Ditches

思路: Edmonds-Karp最大流模板. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 7 const int INF = 0x3f3f3f3f; 8 int m, n, x, y, c; 9 int G[205][205], pre[205]; 10 bool vis[205];