http://poj.org/problem?id=1273

Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 62708   Accepted: 24150

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

题目大意:m条边,每条边都有一个流量值,n个点,求1到n的最大流量

Dinic模板:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#define N 210
#define INF 0x3f3f3f3f using namespace std; int G[N][N], vis[N], layer[N];
int n; bool BFS()//分层处理
{
deque<int>Q;//定义双端队列
memset(layer, -, sizeof(layer));
Q.push_back();//源点入队列(双端队列尾部增加一个元素x)
layer[] = ;//标记源点
while(!Q.empty())
{
int u = Q.front(), i;
Q.pop_front();//删除双端队列中最前一个元素
for(i = ; i <= n ; i++)//遍历点判断是否能分层
{
if(G[u][i] > && layer[i] == -)//当u点到i点这条边有流量且i点尚未被分层
{
layer[i] = layer[u] + ;//则将i点分层
if(i == n)//当到达汇点时分层成功
return true;
else
Q.push_back(i);//否则继续
}
}
}
return false;
} int Dinic()
{
int Maxflow = ;
while(BFS() == true)
{
deque<int>Q;
memset(vis, , sizeof(vis));
Q.push_back();
vis[] = ;
while(!Q.empty())
{
int v = Q.back(), i;
if(v != n)
{
for(i = ; i <= n ; i++)
{
if(G[v][i] > && layer[v] + == layer[i] && !vis[i])//如果v到i有流量且i点是v点的增广路且点i未被访问过
{
vis[i] = ;
Q.push_back((i));//点i进入队列(即点i属于增广路上的一个点)
break;
}
}
if(i > n)//如果遍历所有点后在下一层没有找到增广路,就退出本层继续寻找下一条
Q.pop_back();//删除双端队列中最后一个元素
}//找增广路
else
{
int Minflow = INF, nv;
int len = Q.size();//进入队列中点的个数
for(int i = ; i < len ; i++)
{
int x = Q[i - ];//前一个点
int y = Q[i];//后一个点
if(Minflow > G[x][y])
{
Minflow = G[x][y];
nv = x;//nv记录前端点
}//查找最小值即增光流量
}
Maxflow += Minflow;
for(int i = ; i < len ; i++) //更新流量
{
int x = Q[i - ];
int y = Q[i];
G[x][y] -= Minflow;//更新正向
G[y][x] += Minflow;//反向增加
}
while(!Q.empty() && Q.back() != nv)
Q.pop_back();//出队
}
}
}
return Maxflow;
} int main()
{
int a, b, c, m;
while(~scanf("%d%d", &m, &n))
{
memset(G, , sizeof(G));
while(m--)
{
scanf("%d%d%d", &a, &b, &c);
G[a][b] += c;//处理重边
}
printf("%d\n", Dinic());
}
return ;
}
/*
10 8
1 2 10
1 3 10
2 4 20
2 5 20
3 6 20
3 7 20
4 8 30
5 8 30
6 8 30
7 8 30
*/

邻接表+Dinic(仅供参考)

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#define INF 0x3f3f3f3f
#define N 1010
using namespace std; struct Edge
{
int u, v, next, flow;
} edge[N * N]; int layer[N], head[N], cnt; void Init()
{
memset(head, -, sizeof(head));
cnt = ;
} void AddEdge(int u, int v, int flow)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].flow = flow;
edge[cnt].next = head[u];
head[u] = cnt++;
} bool BFS(int Start, int End)//分层
{
queue<int>Q;
memset(layer, -, sizeof(layer));
Q.push(Start);
layer[Start] = ;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
if(u == End)
return true;
for(int i = head[u] ; i != - ; i = edge[i].next)
{
int v = edge[i].v;
if(layer[v] == - && edge[i].flow > )
{
layer[v] = layer[u] + ;
Q.push(v);
}
}
}
return false;
} int DFS(int u, int Maxflow , int End)
{
if(u == End)
return Maxflow;
int uflow = ;
for(int i = head[u] ; i != - ; i = edge[i].next)
{
int v = edge[i].v;
if(layer[v] == layer[u] + && edge[i].flow > )
{
int flow = min(edge[i].flow, Maxflow - uflow);
flow = DFS(v, flow, End);
edge[i].flow -= flow;
edge[i^].flow += flow;
uflow += flow; if(uflow == Maxflow)
break;
}
}
if(uflow == )
layer[u] = ;
return uflow;
} int Dinic(int Start, int End)
{
int Maxflow = ;
while(BFS(Start, End))
Maxflow += DFS(Start, INF, End);
return Maxflow;
} int main()
{
int m, n;
while(~scanf("%d%d", &m, &n))
{
Init();
int u, v, flow;
while(m--)
{
scanf("%d%d%d", &u, &v, &flow);
AddEdge(u, v, flow);
AddEdge(v, u, );
}
printf("%d\n", Dinic(, n));
}
return ;
}

poj 1273 Drainage Ditches(最大流)的更多相关文章

  1. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  2. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  3. Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  4. POJ 1273 Drainage Ditches 最大流

    这道题用dinic会超时 用E_K就没问题 注意输入数据有重边.POJ1273 dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的. 然而dinic主要依靠 ...

  5. POJ 1273 Drainage Ditches | 最大流模板

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  6. POJ 1273 Drainage Ditches(最大流Dinic 模板)

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...

  7. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  8. poj 1273 Drainage Ditches【最大流入门】

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63924   Accepted: 2467 ...

  9. POJ 1273 Drainage Ditches(网络流,最大流)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

随机推荐

  1. R语言实战读书笔记(一)R语言介绍

    1.3.3 工作空间 getwd():显示当前工作目录 setwd():设置当前工作目录 ls():列出当前工作空间中的对象 rm():删除对象 1.3.4 输入与输出 source():执行脚本

  2. jquery仿天猫商城左侧导航菜单

    之前看到有博友写了一个仿天猫商城左侧导航菜单,可惜不提供免费下载,也没有代码.以前自己也写过类似的效果,只是都是一小块一小块的,现在重新拼凑.我将一步一步的实现拼凑过程,希望对你有所帮助. Demo在 ...

  3. Windows Live Writer配置测试

    文字可以了  不知道代码怎么发布

  4. HDU 1231 最大连续子序列

    和前面两道题一样 不过这题要求输出子序列首尾的元素的值,而且如果所有元素都小于0的话,规定子序列的和为0,并输出整个序列的首尾元素. //#define LOCAL #include <iost ...

  5. Git之 手把手教你使用Git

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...

  6. 【同行说技术】swift最全学习资料汇集(一)

    Swift,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C*共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序.2015年12月4日,苹果公司宣 ...

  7. BZOJ 1452 Count

    长知识啦..二维BIT. #include<iostream> #include<cstdio> #include<cstring> #include<alg ...

  8. 【英语】Bingo口语笔记(73) - 以tly,tely结尾的误读

  9. sound tips

    ASaudio&SoundAS 两个开源项目阅读: ASaudio&SoundAS 都是比较小巧的声音控制,但似乎都不能直接拿到项目只直接使用. ASaudio ASaudio的Tra ...

  10. Java核心技术II读书笔记(一)

    Char2 XML 解析器:读入一个文件,确认其具有正确的格式,然后将其分解成各种元素,使程序员能够访问这些元素. java库提供了两种XML解析器:DOM和SAX,即文档对象模型和流机制解析器. D ...