poj1273

Drainage Ditches

点击打开链接

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 56466   Accepted: 21694

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

Source

USACO 93

题意:

赤裸裸的网络流题目。给定点数,边数,每条

边的容量,以及源点,汇点,求最大流。

可以用来当做网络流的入门题。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 500
#define INF 100000000
int n, m, s, t;
int a[maxn];
int p[maxn];
int cap[maxn][maxn];
int flow[maxn][maxn];
int main()
 {
    while(~scanf("%d%d",&m,&n))
    {
     memset(cap,0,sizeof(cap));
     for(int i = 0; i < m; i++)
     {
        int u, v, c;
        scanf("%d%d%d", &u, &v, &c);
        cap[u][v] += c;
     }
      s = 1;
      t = n;
      int ans = 0;
      queue<int> q;
      memset(flow, 0, sizeof(flow));
      for(;;)
        {
        memset(a, 0, sizeof(a));
        a[s] = INF;
        q.push(s);
        while(!q.empty())   //bfs找增广路
        {
          int u = q.front();
          q.pop();
          for(int v = 1; v <= n; v++) if(!a[v] && cap[u][v]>flow[u][v])
          {                            //找到新节点V并入队
            p[v] = u; q.push(v);
            if(a[u]<cap[u][v]-flow[u][v])   //s-v上最小残量
            a[v]=a[u];
            else
                a[v]=cap[u][v]-flow[u][v];
          }
        }
        if(a[t] == 0) break;         //找不到,则当前已经是最小残量
        for(int u = t; u != s; u = p[u])  //更新流量
        {
          flow[p[u]][u] += a[t];
          flow[u][p[u]] -= a[t];
        }
        ans += a[t]; //更新从S流出的总流量
      }
      printf("%d\n", ans);
}
  return 0;
}
时间: 2024-11-05 18:17:27

poj1273的相关文章

POJ1273 最大流模板

之前自己写的,以后当一个模板的基础吧,不管是最大流,最小割,二分图匹配 下面是POJ1273的一个裸题.. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <queue> 6 using namespace std; 7 struct node{ 8 int to,cap,rev; 9 node(int _t

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

[笔记] 网络流-最大流 POJ-1273\HDU-4240

[1] POJ-1273 题目:http://poj.org/problem?id=1273 最直接的最大流问题,用了Ford-Fulkerson方法,DFS随机搜索增广路. 算法原理参考:http://blog.csdn.net/smartxxyx/article/details/9293665 /************************ POJ-1273* Ford-Fulkerson***********************/#include <stdio.h> #inclu

poj1273最大流dinc

bfs 构建层次图,dfs 寻找增广路.dfs在寻找增广路的同时自我调整直到此时的层次图无增广路时 重新构图,直到无增广路为止. 对于添加反弧,觉得对于每点 进流量和 出流量应该守恒,反向弧的添加方便自我调整,而通过每点的流量没变,最后导致流到终点的流量不变. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string>

HDU 1532||POJ1273:Drainage Ditches(最大流)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8574    Accepted Submission(s): 3991 Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie's

最大流的理解以及dinic模板 poj1273

增广路以及残留网络的定义不再赘述了.算导上说的很清楚,证明也有,看懂了就知道怎么求最大流了. 而算导上提到的FF方法以及ek算法的伪代码中都是将流与残留容量分开储存,其实代码实现的时候我们只需存正反向弧的残留容量即可. 然后是对残留网络的一些理解,残留网络中的反向弧是怎么来的? 残留网络的每条边都是这条有向边的残留容量,而残留容量又由公式cf(u,v)=c(u,v)-f(u,v)得到,那么对于一条不存在的有向边(v,u),其容量c(v,u)=0,f(v,u)=-f(u,v)通过反对称性可知,那么

POJ1273 最大流 EK算法

套了个EK的模板 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <climits> #include <cstring> #include <cmath> #include <stack> #include <queue> #i

dinic算法学习(以poj1273为例)

Dinic 算法模板 Dinic算法是一种比较容易实现的,相对比较快的最大流算法. 求最大流的本质,就是不停的寻找增广路径.直到找不到增广路径为止. 对于这个一般性的过程,Dinic算法的优化如下: (1)Dinic算法首先对图进行一次BFS,然后在BFS生成的层次图中进行多次DFS. 层次图的意思就是,只有在BFS树中深度相差1的节点才是连接的. 这就切断了原有的图中的许多不必要的连接.很牛逼! 这是需要证明的,估计证明也很复杂. (2)除此之外,每次DFS完后,会找到路径中容量最小的一条边.

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];

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