POJ-1273 Drainage Ditches 最大流Dinic
Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 65146 Accepted: 25112
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
题目大意:草地排水。。。
多组数据,有源有汇。。。。
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define maxn 300
int dis[maxn]={0};
int way[maxn][maxn]={0};
int q[maxn*10],h,t;
int n,m,ans;
bool bfs()
{
h=0;t=1;
memset(dis,0xff,sizeof(dis));
dis[1]=0;
q[1]=1;
while (h<t)
{
int j=q[++h];
for (int i=1; i<=n; i++)
{
if (dis[i]<0 && way[j][i]>0)
{
dis[i]=dis[j]+1;
q[++t]=i;
}
}
}
if (dis[n]>0)
return true;
else
return false;
}
int dfs(int loc,int low)
{
int ans=0;
if (loc==n) return low;
for (int i=1; i<=n; i++)
if (way[loc][i]>0 && dis[i]==dis[loc]+1 && (ans=dfs(i,min(loc,way[loc][i]))))
{
way[loc][i]-=ans;
way[i][loc]+=ans;
return ans;
}
return 0;
}
int main()
{
while (~scanf("%d%d",&m,&n))
{
memset(way,0,sizeof(way));
for (int i=1; i<=m; i++)
{
int s,e,water;
scanf("%d%d%d",&s,&e,&water);
way[s][e]+=water;
}
ans=0;
while (bfs())
{
int now;
while (now=dfs(1,0x7fffffff))
ans+=now;
}
printf("%d\n",ans);
}
return 0;
}
POJ-1273 Drainage Ditches 最大流Dinic的更多相关文章
- POJ 1273 Drainage Ditches(最大流Dinic 模板)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...
- 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(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- 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(最大流)
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 ...
随机推荐
- 【shiro】shiro学习笔记1 - 初识shiro
[TOC] 认证流程 st=>start: Start e=>end: End op1=>operation: 构造SecurityManager环境 op2=>operati ...
- BIO、NIO与NIO.2的区别与联系
BIO.NIO.NIO.2之间的区别主要是通过同步/异步.阻塞/非阻塞来进行区分的 同步: 程序与操作系统进行交互的时候采取的是问答的形式 异步: 程序与操作系统取得连接后,操作系统会主动通知程序消息 ...
- 第二章 下山遇虎(@helper)
@helper方法定义 使用@helper关键字可以定义一个方法,这样就可以在页面中调 用这个方法了,和C#中的方法一样.在页面中定义的方法可以访问ViewBag,HttpContext等等页面的属性 ...
- U3D prefab
1,prefab相当于一个类,字面意思就是预设,预先设计好的类.把一个prefab拖放到场景中就生成了一个实例,把二个prefab放到场景中就生成了两个实例. 不同的实例独立动作,拥有自己独立的状态与 ...
- js屏蔽回车键
document.onkeydown = function () { if (window.event && window.event.keyCode == 13 ...
- ActionBar在Android2.x的实现,类似新版微信界面。
ActionBar完美兼容Android4.x的机型,虽然现在Android2.x的系统越来越少,还有有一部分人使用的仍是2.x的系统,所以我们还得考虑着兼容性问题. 对比图: Test例子与微信的对 ...
- 三种实例化委托的方式(C# 编程指南)
1.定义的委托和方法 delegate void TestDelegate(string s); static void M(string s) { Console.WriteLine(s); } 2 ...
- 再次遇到\r\n转\r问题
帮助小伙伴做jenkins的环境搭建.以为5分钟的事情,但是发现了一个诡异的问题.总是提示SVN的url不合法“URL '%s' is not properly URI-encoded”. 由于选择了 ...
- 百度地图ip定位,不算bug的bug
做为一个入行不足两年的菜鸟,能在博客园写下第一篇博客,是需要多大的勇气啊.主要还是怕大神们喷啊.其次自己文笔实在太差了. 哈哈~还请各位大神,口下留情啊. 首先说下我的需求:一个需要城市分站的手机站. ...
- Android Support Annotations :安卓注解快速上手
我们都知道,安卓资源文件都是int类型的ID来保存其引用,通过注解类型,可以让我们在写代码的时候,及时发现参数类型的错误,避免潜在的BUG,如下: 我们通过@LayoutRes指定了参数必须要是R.l ...