网络流--最大流--POJ 1273 Drainage Ditches
链接
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
模板题,题意很明了,直接测板子。
#include<cstdio>
#include<cstring>
#include<queue>
#define INF 1e9
using namespace std;
const int maxn=200+5;
struct Edge
{
int from,to,cap,flow;
Edge() {}
Edge(int f,int t,int c,int flow):from(f),to(t),cap(c),flow(flow) {}
};
struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int cur[maxn];
int d[maxn];
void init(int n,int s,int t)
{
this->n=n, this->s=s, this->t=t;
edges.clear();
for(int i=1; i<=n; i++)
G[i].clear();
}
void AddEdge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
memset(vis,0,sizeof(vis));
queue<int> Q;
d[s]=0;
Q.push(s);
vis[s]=true;
while(!Q.empty())
{
int x=Q.front();
Q.pop();
for(int i=0; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
Q.push(e.to);
d[e.to]= 1+d[x];
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
if(x==t || a==0)
return a;
int flow=0,f;
for(int& i=cur[x]; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow) ))>0 )
{
e.flow+=f;
edges[G[x][i]^1].flow -=f;
flow+=f;
a-=f;
if(a==0)
break;
}
}
return flow;
}
int Maxflow()
{
int flow=0;
while(BFS())
{
memset(cur,0,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
} DC;
int main()
{
int n,m,t;
while(scanf("%d%d",&m,&n)==2){
DC.init(n,1,n);
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
DC.AddEdge(u,v,w);
}
printf("%d\n",DC.Maxflow());
}
return 0;
}
网络流--最大流--POJ 1273 Drainage Ditches的更多相关文章
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1273 Drainage Ditches(网络流,最大流)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- 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 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches
Drainage Ditches 题目抽象:给你m条边u,v,c. n个定点,源点1,汇点n.求最大流. 最好的入门题,各种算法都可以拿来练习 (1): 一般增广路算法 ford() #in ...
随机推荐
- 修复Windows10引导,适用gpt+uefi环境
在双硬盘多系统安装时,容易损坏Win10的开机引导文件. 可以尝试用Windows原版安装盘启动,进入命令提示符模式: 首先使用diskpart命令确认需要修复的Windows分区的安装卷X:. 再运 ...
- 【翻译】OpenVINO Pre-Trained 预训练模型介绍
OpenVINO 系列软件包预训练模型介绍 本文翻译自 Intel OpenVINO 的 "Overview of OpenVINO Toolkit Pre-Trained Models& ...
- 基于 Jepsen 来发现几个 Raft 实现中的一致性问题(2)
Nebula Graph 是一个高性能.高可用.强一致的分布式图数据库.由于 Nebula Graph 采用的是存储计算分离架构,在存储层实际只是暴露了简单的 kv 接口,采用 RocksDB 作为状 ...
- python3(二十九) orderClass
""" """ __author__ = 'shaozhiqi' # Python的class中还有许多有特殊用途的函数,可以帮助我们定制类 ...
- AJ学IOS(41)UI之核心动画 两行代码搞定3D转场
AJ分享,必须精品 效果: 代码: 其实代码很少,苹果都给封装好了 // 1.创建核心动画 CATransition *ca = [CATransition animation]; // 1.1动画过 ...
- 假的数论gcd,真的记忆化搜索(Codeforce 1070- A. Find a Number)
题目链接: 原题:http://codeforces.com/problemset/problem/1070/A 翻译过的训练题:https://vjudge.net/contest/361183#p ...
- 简单的Tuple声明和输出
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- PHP安全(文件包含、变量覆盖、代码执行)
文件包含漏洞 本地文件包含 截断技巧: ../../etc/passwd%00(\x00 \0) 利用操作系统对目录最大长度的限制,可以不需要0字节而达到截断的目的.目录字符串,在windows下25 ...
- stand up meeting 11/18/2015
今日工作总结: 冯晓云:完成C#版本API的class library编译,尝试与主程序进行通信:昨天临时通知让用C++封装,不解!!![后续:我用C#做了一个查词的APP,调用的就是这个API的DL ...
- ORA-0245
经常有客户报错ORA-0245 1.11.2 rac环境, rman存在snap控制文件路径,默认是文件系统[非共享,导致备份控制文件报错] 解决方法:将snap路径配置到ASM磁盘组共享路径[nfs ...