poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 62708 | Accepted: 24150 |
Description
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
Output
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(最大流)的更多相关文章
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...
- POJ 1273 Drainage Ditches 最大流
这道题用dinic会超时 用E_K就没问题 注意输入数据有重边.POJ1273 dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的. 然而dinic主要依靠 ...
- POJ 1273 Drainage Ditches | 最大流模板
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...
- POJ 1273 Drainage Ditches(最大流Dinic 模板)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- poj 1273 Drainage Ditches【最大流入门】
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63924 Accepted: 2467 ...
- POJ 1273 Drainage Ditches(网络流,最大流)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
随机推荐
- linux下从源代码安装git
之所以有这样的需求,是因为部分预安装的git版本太低,很多功能没有并且安全性存在问题. 比如git submodule add xxx@host:yyy.git必须在父repo的root目录安装,而新 ...
- UIView的user Interaction Enabled属性
A Boolean value that determines whether user events are ignored and removed from the event queue. 译: ...
- IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)
一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...
- XE7 - 程序图标及启动画面图片的注意事项
还是继续昨晚写的,年前已经解决了这个问题,现在补记下.启动画面失真是本篇笔记的重点.搜索了很多文章,基本上大同小异,几乎都没怎么提及启动画面失真的问题.不知道是不是我的操作不对头,. Project ...
- 【转】IOS中定时器NSTimer的开启与关闭
原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...
- socket基础函数(2)
http://www.cnblogs.com/RascallySnake/archive/2013/07/11/3185071.html 一.select winsock中 #include & ...
- Linux makefile教程之概述一[转]
概述—— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些 Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makef ...
- ECSide标签属性说明之<ec:table>
<ec:table>标签说明 ◆ 属性: tableId描述: 设置列表的唯一标识,默认为"ec",当一个页面内有多个ECSIDE列表时,必须为每个列表指定不同的tab ...
- js四舍五入
7-13 向上取整ceil() 7-14 向下取整floor() 7-15 四舍五入round() 7-16 随机数 random()
- jquery功能实现总结
最近一直在做.net这方面的,也学习了jquery一些东西,其中实现了自动关闭页面,json解析字符串,拼接字符串,for循环,函数调用,等一些功能,自己也学习了,也希望可以帮助大家,大家看后给提提意 ...