poj 1273 (nyoj 323) Drainage Ditches : 最大流
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 49648 | Accepted: 18829 |
Description
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
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
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
1号是源点,最后一个点m为汇点,求最大流,直接拿模板
#include<stdio.h>
#include<stack>
#include<queue>
#include<string.h>
using namespace std;
#define max 300
int map[max][max];
int layer[max];
int m;
int source;
int target;
bool bfs()
{
queue<int> q;
q.push(source);
bool used[max] = {0};
memset(layer, 0, sizeof(layer));
used[source] = 1;
while(!q.empty())
{
int top = q.front();
q.pop();
int i;
if(map[top][target] > 0)
{
return true;
}
for(i = 1; i < m; i++)
{
if(map[top][i] > 0 && !used[i])
{
layer[i] = layer[top] + 1;
q.push(i);
used[i] = 1;
}
}
}
return false;
}
int dinic()
{
int max_flow = 0;
int prev[max] = {0};
int used[max] = {0};
while(bfs())
{
stack<int> s;
memset(prev, 0, sizeof(prev));
memset(used, 0, sizeof(used));
prev[source] = source;
s.push(source);
while(!s.empty())
{
int top = s.top();
if(map[top][target] > 0)
{
int j = top;
int min = map[top][target];
int mark = top;
while(prev[j] != j)
{
if(map[prev[j]][j] < min)
{
min = map[prev[j]][j];
mark = prev[j];
}
j = prev[j];
}
j = top;
map[top][target] -= min;
map[target][top] += min;
while(prev[j] != j)
{
map[prev[j]][j] -= min;
map[j][prev[j]] += min;
j = prev[j];
}
max_flow += min;
while(!s.empty() && s.top() != mark)
s.pop();
}
else
{
int i;
for(i = 1; i < m; i++)
{
if(map[top][i] > 0 && layer[i] == layer[top] + 1 && !used[i])
{
s.push(i);
used[i] = 1;
prev[i] = top;
break;
}
}
if(i == m)
s.pop();
}
}
}
return max_flow;
}
int main()
{
int n;
// freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &m) != EOF)
{
memset(map, 0, sizeof(map));
int i;
int u, v, f;
for(i = 0; i < n; i++)
{
scanf("%d%d%d", &u, &v, &f);
map[u][v] += f;
}
source = 1;
target = m;
printf("%d\n", dinic());
}
return 0;
}
poj 1273 (nyoj 323) Drainage Ditches : 最大流的更多相关文章
- POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)
Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
- NYOJ 323 Drainage Ditches 网络流 FF 练手
Drainage Ditches 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Every time it rains on Farmer John's fields, ...
- 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 ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
- TZOJ 4085 Drainage Ditches(最大流)
描述 Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. Th ...
- HDU1532 Drainage Ditches —— 最大流(sap算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/ ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
随机推荐
- python中时间日期格式化符号
python中时间日期格式化符号: import time print(time.strftime('%Y%H%M%S', time.localtime())) 运行结果: 2016092308 %y ...
- android 实现拍照的2种方法
android系统的照相功能,已实现2种方法,可供大家参考: 1.调用系统摄像头来拍照 首先,找到AndroidManifest.xml文件里加入用户权限 <uses-permission an ...
- 区别: @Secured(), @PreAuthorize() 及 @RolesAllowed()
在Spring security的使用中,为了对方法进行权限控制,通常采用的三个注解,就是@Secured(), @PreAuthorize() 及 @RolesAllowed(). 但是着三者之间的 ...
- Object-C中需要注意的小细节
--------------------------------------------关于命名------------------------------------------------- 1. ...
- 理解Javascript参数中的arguments对象
ECMAScript中函数没有标签名的特性,所以ECMAScript函数中没有重载. Javascript中arguments的存在可以弥补javascript中函数没有重载的不足. Javascri ...
- Python标准库
Python标准库是随Python附带安装的,它包含大量极其有用的模块.熟悉Python标准库是十分重要的,因为如果你熟悉这些库中的模块,那么你的大多数问题都可以简单快捷地使用它们来解决. sys模块 ...
- C# 理解lock
本文为转载 .. 一. 为什么要lock,lock了什么? 当我们使用线程的时候,效率最高的方式当然是异步,即各个线程同时运行,其间不相互依赖和等待.但当不同的线程都需要访问某个资源的时候,就需要同步 ...
- MySQL类型转换
mysql为我们提供了两个类型转换函数:CAST和CONVERT,现成的东西我们怎能放过? BINARY[(N)] CHAR[(N)] DATE DATETIME DECIMAL SIGNED [IN ...
- titan
简介 (1)titan:存储,查询图形结构的数据库.分布式集群环境下,可支持数以千亿级别的点和边,同时支持上千个并发的实时的复杂图形遍历,支持ACID事务. (2)架构:支持以下3方面的自由组合 节点 ...
- setValue:forUndefinedKey
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewControlle ...