POJ 1273 Drainage Ditches题解——S.B.S.
Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 67823 | Accepted: 26209 |
Description
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
Output
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cassert>
#include<climits>
#define maxn 210
using namespace std;
void find();
void flow();
void update();
struct Edge
{
int c;
int f;
}edge[maxn][maxn];
int n,m;
int s,t;
int residual[maxn][maxn];
int que[maxn*maxn],head,tail;
int pre[maxn];
bool vis[maxn];
int max_flow,min_flow;
void find()
{
int i,cu;
memset(vis,false,sizeof(vis));
memset(residual,,sizeof(residual));
memset(pre,,sizeof(pre));
head=;que[head]=s;pre[s]=s;vis[s]=true;tail=;
while(head<tail&&pre[t]==)
{
cu=que[head];
for(i=;i<=n;i++)
{
if(vis[i]==false)
{
if(edge[cu][i].c-edge[cu][i].f>)
{
residual[cu][i]=edge[cu][i].c-edge[cu][i].f;
pre[i]=cu;que[tail++]=i;vis[i]=true;
}
else if(edge[i][cu].f>)
{
residual[cu][i]=edge[i][cu].f;
pre[i]=cu;que[tail++]=i;vis[i]=true;
}
}
}
head++;
}
}
void flow()
{
int i=t,j;
if(pre[i]==)
{
min_flow=;return;
}
j=0x7fffffff;
while(i!=s)
{
if(residual[pre[i]][i]<j) j=residual[pre[i]][i];
i=pre[i];
}
min_flow=j;
}
void update()
{
int i=t;
if(pre[i]==) return;
while(i!=s)
{
if(edge[pre[i]][i].c-edge[pre[i]][i].f>)
edge[pre[i]][i].f+=min_flow;
else if(edge[i][pre[i]].f>)
edge[pre[i]][i].f+=min_flow;
i=pre[i];
}
}
void solve()
{
s=;t=n;
max_flow=;
while(true)
{
find();flow();
max_flow+=min_flow;
if(min_flow>) update();
else return;
}
}
int main()
{
std::ios::sync_with_stdio(false);
int i,u,v,c;
while(scanf("%d %d",&m,&n)!=EOF)
{
memset(edge,,sizeof(edge));
for(i=;i<m;i++)
{
scanf("%d %d %d",&u,&v,&c);
edge[u][v].c+=c;
}
solve();
printf("%d\n",max_flow);
}
return ;
}
POJ 1273 Drainage Ditches题解——S.B.S.的更多相关文章
- 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 - [最大流模板题] - [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(最大流,E-K算法)
一.Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clove ...
- POJ 1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67387 Accepted: 2603 ...
- POJ 1273 Drainage Ditches -dinic
dinic版本 感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了 Description Every time it rains on Farmer John's fields, ...
- 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 ...
随机推荐
- 人工智能 - AI
CNCC 2016 | 人工智能60年浪潮 (原文链接) Intelligence,智能是指生物一般性的精神能力,其三因素理论: 成分智力 Componential Intelligence:思维和对 ...
- N皇后问题
题目描述 在n×n格的棋盘上放置彼此不受攻击的n个皇后.按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子.n后问题等价于再n×n的棋盘上放置n个后,任何2个皇后不妨在同一行或同 ...
- 编程模式之模板方法模式(Template Method)
模板方法模式由两个角色组成:父类角色,子类角色. 父类角色:提供模板. 子类角色:为父类模板提供实现. 类图: JAVA代码: AbstractClass.java package com.templ ...
- java Byte[] to String(hex)
1. 字节数组转换成16进制字符展示 2.代码 package com.goodfan; public class ByteArrayToString { private static char[] ...
- P2P核心业务体系
看似一个简单的充值.提现按钮,背后可是巨多的逻辑和业务处理.
- 【工匠大道】Git的使用总结
初衷是想将一些常用的代码整理在博客园上,但是考虑到博客园上的代码量多,需要折叠,折叠后就不能直接修改,于是想到了 大家都常用的 gitHub来进行代码的管理. 其实之前我是用过 Osa的git的,但是 ...
- JS高程3.基本概念(4)操作符
ECMA-262用于操作数据值的操作符包括: 算术操作符 位操作符 关系操作符 相等操作符 ECMAScript操作符的不同之处在于:它能够适用于很多值,包括字符串,数字值,布尔值,甚至是对象.(在应 ...
- 如何使用github搭建个人博客
1.去github官网注册个人帐号:没有的:猛戳这里去注册,比如我的账户名:wjf444128852,我的已经汉化(可在github里搜索github如何汉化有插件) 2.点击仓库-新建,仓库名字必须 ...
- 原生JS:JSON对象详解
JSON对象 支持到IE8,旧版的IE需要Polyfill 本文参考MDN做的详细整理,方便大家参考[MDN](https://developer.mozilla.org/zh-CN/docs/Web ...
- ios webview自适应实际内容高度4种方法
有的时候会碰见类似的苦逼需求, webview自适应实际内容高度 下面有四种方法供使用 方法1:获取webview中scrovllview的contentsize进行设置 1 2 3 4 5 6 ...