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 ImportError: No module named 的问题
https://my.oschina.net/leejun2005/blog/109679 python中,每个py文件被称之为模块,每个具有__init__.py文件的目录被称为包.只要模块或者包所 ...
- Mysql 索引 转载
转自 :http://blog.csdn.net/wud_jiyanhui/article/details/7403375 什么是索引 索引时一种特殊的文件,他们包涵着对数据表里所有记录的引用指针. ...
- oracle定时运行 存储过程
/* 查询: select job,broken,what,interval,t.* from user_jobs t; job job的唯一标识,自动生成的 broken 是否处于运行状态,N;运行 ...
- ORACLE 常用数值函数
1 ABS(n)返回数值弄参数的绝对值.它接受一个数值型值作为输入参数,或者任何可以隐式地转换为数值型值的值.并且返回数值型值的绝对值. Select abs(-1) from dual ABS(-1 ...
- mysql 获取一个表中缺失的最小编号
select count(*),t1.`name` from test_id t1INNER JOIN test_id t2on t1.id >= t2.idgroup by t1.id,t1. ...
- Android开发框架androidannotations的使用
Android开发框架AndroidAnnotations,它除了有依赖注入的特性以外,还集成了Ormlite,Spring-android中的REST模板.使用起来非常方便,大大提高了开发效率. 使 ...
- Android 程式开发:(十三)特殊碎片 —— 13.2 DialogFragment
Android 程式开发:(十三)特殊碎片 —— 13.2 DialogFragment 原文地址 我们也可以创建另外一种碎片——DialogFragment.顾名思义,DialogFragment就 ...
- 详解Android首选项框架ListPreference
详解Android首选项框架ListPreference 原文地址 探索首选项框架 在深入探讨Android的首选项框架之前,首先构想一个需要使用首选项的场景,然后分析如何实现这一场景.假设你正在编写 ...
- [转]Linux环境下查看线程数的几种方法
1.cat /proc/${pid}/status 2.pstree -p ${pid} 3.top -p ${pid} 再按H,或者直接输入 top -bH -d 3 -p ${pid} top ...
- Angular学习(4)- 数组双向梆定
示例: <!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 4< ...