网络流--最大流--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 ...
随机推荐
- (js描述的)数据结构[栈结构](2)
(js描述的)数据结构[栈结构](2) 一.什么是栈结构 1.一种受限制的线性结构,这种结构可以基于数组来实现. 2.可以抽象成一个容器,上面的是栈顶,底下的是栈底.所以仅允许对栈顶进行操作, 二.栈 ...
- javascript中的constructor
constructor,构造函数,对这个名字,我们都不陌生,constructor始终指向创建当前对象的构造函数. 这里有一点需要注意的是,每个函数都有一个prototype属性,这个prototyp ...
- Linux学习,path,环境变量的配置
方法一: 1.查看当前环境变量配置的所与信息 echo $PATH 注意: echo是输出的意思 加$表示它是一个变量 2.配置环境命令 PATH="$PATH":comdir 注 ...
- Java第二十天,Map集合(接口)
Map接口 一.定义 Map集合是双列集合,即一个元素包含两个值(一个key,一个value),Collection集合是单列集合. 定义格式: public interface Map<K,V ...
- JS 浏览器BOM-->简介和属性
1.简介: BOM:浏览器对象模型(Browser Object Model),是一个用于访问浏览器和计算机屏幕的对象集合.我们可以通过全局对象window来访问这些对象. 2.属性 window. ...
- sql 案例
select now();#获取当前系统时间 select now() from dual;#与Oracle兼容 show character set;#产看当前数据库支持的字符集 create da ...
- 虚拟机安装windows sever2008
1.打开并进行新建虚拟机 2.默认选择“典型” 3.选择“安装程序盘映像文件”,并‘浏览’选择本地的文件 4. 5.后面的默认选择即可,安装路径可自己修改 6.这一步的磁盘大小可自己修改的,这里先预设 ...
- Go语言 中文分词技术使用技巧(一)
分词技术就是搜索引擎针对用户提交查询的关键词串进行的查询处理后根据用户的关键词串用各种匹配方法进行分词的一种技术. 中文分词(Chinese Word Segmentation)指的是将一个汉字序列( ...
- search(5)- elastic4s-构建索引
按照计划,这篇开始尝试用elastic4s来做一系列索引管理和搜索操作示范.前面提过,elastic4s的主要功能之一是通过组合Dsl语句形成json请求.那么我们先试试组合一些Dsl语句,再想办法产 ...
- Daily Scrum 1/18/2016
Yandong & Zhaoyang: Prepare bug bash slides for Beta release; Dong & Fuchen:Prepare demo for ...