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 ...
随机推荐
- 什么是PHP Guzzle?
Guzzle是一个使得利用PHP实现发送HTTP 请求,方便和web service集成的PHP 客户端模拟组件.一句话,它就像一个PHP写的浏览器.当你的服务端程序需要作为客户端来访问其他的serv ...
- 如何在linux中从源代码编译安装nodejs?
$ sudo yum groupinstall 'Development Tools'安装开发环境$ wget https://nodejs.org/dist/v0.12.2/node-v0.12.2 ...
- UVa 10420 List of Conquests
题意就是有N个pl妹子,然后每行第一个单词是妹子的国籍,后面是妹子的名字. 你的任务就是统计相同国籍妹子的个数,然后按字母表顺序输出. 我首先把所有的国籍都读入,然后用qsort()按字母表顺序排序. ...
- 51nod1379 索函数
果断打表找规律.然后看得出来是2^k-1之后又不知道怎么求出k有什么卵用... http://blog.csdn.net/guhaiteng/article/details/52094210 %%%% ...
- 10个实用的PHP正则表达式 (转)
http://www.iteye.com/news/23231 1. 验证E-mail地址 这是一个用于验证电子邮件的正则表达式.但它并不是高效.完美的解决方案.在此不推荐使用. $email = & ...
- 【转】iOS开发-Protocol协议及委托代理(Delegate)传值
原文网址:http://www.cnblogs.com/GarveyCalvin/p/4210828.html 前言:因为Object-C是不支持多继承的,所以很多时候都是用Protocol(协议)来 ...
- Oracle RAC 服务器端连接负载均衡(Load Balance)
Oracle RAC服务器端的负载均衡是根据RAC中各节点的连接负荷数情况,将新的连接请求分配到负荷最小的节点上去.当数据库处于运行时,RAC中各节点的PMON进程每3秒会将各自节点的连接负荷数更新到 ...
- jQuery学习备忘
jQuery对象转换成DOM对象 var #cr = $("#cr"); //jQuery对象 var cr = $cf[0]; //DOM对象 alert(cr.checked) ...
- 用VS2010打开vs2008
用VS2010打开vs2008,对应的9.0.0.0版本,而应该该使用版本10.0.0.0 解决方案:修改配置文件.将Microsoft.ReportViewer的版本为9.0.0.0改为10.0.0 ...
- smarty缓存函数
原来在Smarty中在3.0以上版本中不在使用这个clear_all_cache(),而是以$smarty->clearAllCache(); 代替.其中$smarty->clear_ca ...