2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)
P2936 [USACO09JAN]全流Total Flow
题目描述
Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an apparently haphazard way. He wants to calculate the flow through the pipes.
Two pipes connected in a row allow water flow that is the minimum of the values of the two pipe’s flow values. The example of a pipe with flow capacity 5 connecting to a pipe of flow capacity 3 can be reduced logically to a single pipe of flow capacity 3:
+—5—+—3—+ -> +—3—+
Similarly, pipes in parallel let through water that is the sum of their flow capacities:
+—5—+
—+ +— -> +—8—+
+—3—+
Finally, a pipe that connects to nothing else can be removed; it contributes no flow to the final overall capacity:
+—5—+
—+ -> +—3—+
+—3—+–
All the pipes in the many mazes of plumbing can be reduced using these ideas into a single total flow capacity.
Given a map of the pipes, determine the flow capacity between the well (A) and the barn (Z).
Consider this example where node names are labeled with letters:
+———–6———–+
A+—3—+B +Z
+—3—+—5—+—4—+
C D
Pipe BC and CD can be combined:
+———–6———–+
A+—3—+B +Z
+—–3—–+—–4—–+
D Then BD and DZ can be combined:
+———–6———–+
A+—3—+B +Z
+———–3———–+
Then two legs of BZ can be combined:
B A+—3—+—9—+Z
Then AB and BZ can be combined to yield a net capacity of 3:
A+—3—+Z
Write a program to read in a set of pipes described as two endpoints and then calculate the net flow capacity from ‘A’ to ‘Z’. All
networks in the test data can be reduced using the rules here.
Pipe i connects two different nodes a_i and b_i (a_i in range
‘A-Za-z’; b_i in range ‘A-Za-z’) and has flow F_i (1 <= F_i <= 1,000). Note that lower- and upper-case node names are intended to be treated as different.
The system will provide extra test case feedback for your first 50 submissions.
约翰总希望他的奶牛有足够的水喝,因此他找来了农场的水管地图,想算算牛棚得到的水的 总流量.农场里一共有N根水管.约翰发现水管网络混乱不堪,他试图对其进行简 化.他简化的方式是这样的:
两根水管串联,则可以用较小流量的那根水管代替总流量.
两根水管并联,则可以用流量为两根水管流量和的一根水管代替它们
当然,如果存在一根水管一端什么也没有连接,可以将它移除.
请写个程序算出从水井A到牛棚Z的总流量.数据保证所有输入的水管网络都可以用上述方法 简化.
输入输出格式
输入格式:
* Line 1: A single integer: N
* Lines 2..N + 1: Line i+1 describes pipe i with two letters and an integer, all space-separated: a_i, b_i, and F_i
输出格式:
* Line 1: A single integer that the maximum flow from the well (‘A’) to the barn (‘Z’)
输入输出样例
输入样例#1:
5
A B 3
B C 3
C D 5
D Z 4
B Z 6
输出样例#1:
3
一眼题,最大流模板,这道题主要考我们输入的处理,其它就只和你写的dinic" role="presentation" style="position: relative;">dinicdinic有关了。
代码如下:
#include<bits/stdc++.h>
#define N 1000
#define M 100005
using namespace std;
int cnt=-1,m,s,t,first[N],d[N];
struct Node{int v,next,c;}e[M];
inline void add(int u,int v,int c){
e[++cnt].v=v,e[cnt].c=c,e[cnt].next=first[u],first[u]=cnt;
e[++cnt].v=u,e[cnt].c=0,e[cnt].next=first[v],first[v]=cnt;
}
inline bool bfs(){
queue<int>q;
memset(d,-1,sizeof(d));
q.push(s),d[s]=0;
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=first[x];i!=-1;i=e[i].next){
int v=e[i].v;
if(d[v]!=-1||e[i].c<=0)continue;
d[v]=d[x]+1;
if(v==t)return true;
q.push(v);
}
}
return false;
}
inline int dfs(int x,int f){
if(x==t||!f)return f;
int flow=f;
for(int i=first[x];i!=-1;i=e[i].next){
int v=e[i].v;
if(e[i].c>0&&d[v]==d[x]+1&&flow){
int tmp=dfs(v,min(flow,e[i].c));
if(!tmp)d[v]=-1;
e[i].c-=tmp;
e[i^1].c+=tmp;
flow-=tmp;
}
}
return f-flow;
}
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
return ans;
}
int main(){
cnt=-1;
memset(first,-1,sizeof(first));
m=read(),s='A'-'A'+1,t='Z'-'A'+1;
for(int i=1;i<=m;++i){
char c[2];
int u,v,w;
scanf("%s",c);
u=(int)(c[0]-'A'+1);
scanf("%s%d",c,&w);
v=(int)(c[0]-'A'+1);
add(u,v,w);
}
int ans=0;
while(bfs())ans+=dfs(s,0x3f3f3f3f);
printf("%d",ans);
return 0;
}
2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)的更多相关文章
- 洛谷——P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- 洛谷 P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- 2018.07.01洛谷P2617 Dynamic Rankings(带修主席树)
P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i ...
- 2018.07.17 洛谷P1368 工艺(最小表示法)
传送门 好的一道最小表示法的裸板,感觉跑起来贼快(写博客时评测速度洛谷第二),这里简单讲讲最小表示法的实现. 首先我们将数组复制一遍接到原数组队尾,然后维护左右指针分别表示两个即将进行比较的字符串的头 ...
- 2018.11.06 洛谷P1099 树网的核(最短路+枚举)
传送门 之前看李煜东的书一直感觉是道神题. 然后发现这题数据范围只有300?300?300? 直接上floydfloydfloyd然后暴力就完了啊. 代码: #include<bits/stdc ...
- 2018.11.06 洛谷P1941 飞扬的小鸟(背包)
传送门 上升看成完全背包. 下降看成01背包. 注意边界转移就行了. 代码: #include<bits/stdc++.h> using namespace std; inline int ...
- 2018.07.01 洛谷小B的询问(莫队)
P2709 小B的询问 题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数 ...
- 2018.07.31洛谷P1552 [APIO2012]派遣(可并堆)
传送门 貌似是个可并堆的模板题,笔者懒得写左偏堆了,直接随机堆水过.实际上这题就是维护一个可合并的大根堆一直从叶子合并到根,如果堆中所有数的和超过了上限就一直弹直到所有数的和不超过上限为止,最后对于当 ...
- 2018.07.23 洛谷P4097 [HEOI2013]Segment(李超线段树)
传送门 给出一个二维平面,给出若干根线段,求出x" role="presentation" style="position: relative;"&g ...
随机推荐
- 序列化和反序列化(json 和pickle)dumps 为序列化, json为反序列化
json 可以在不同语言中进行使用 下面先介绍一下json的适用方法 import json, pickle t1 = { 'name':'alex', ', ' } t1 = json.dumps( ...
- Spring boot 配置文件 使用占位符号
配置文件占位符 1:使用随机数 ${random.value}.${random.int}.${random.long} ${random.)}.${random.,]} 2: 占位符获取之前配置的值 ...
- Java Collection.sort 排序升序, 降序问题
不多说,记住2点, 直接上代码(下面是降序): package mall; import java.util.ArrayList; import java.util.Collections; impo ...
- python 1 面向对象基础知识
1.编码范式 编程 是程序员用特定的 语法+数据结构+算法 组成的代码来告诉计算机如何执行任务的过程 如果把编程比作习武,编程方式就是武林中的各种流派,而在编程的世界里面最常见的两大流派是:面向过程 ...
- 控制html元素的隐藏问题
控制元素隐藏的方式,有display:none.visibility:hidden以及不透明度设置. 一.display:none 被隐藏的元素,在页面中不占位,空出的位置会被相邻的元素占用. < ...
- python环境准备以及easy_install和pip的安装
python3的安装: yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline- ...
- @Repository , @Service , @Controller 和 @Component
用Spring MVC时@Controller注解的类将变成一个Spring MVC的控制器. 不用Spring MVC的情况下, 这四个注解没有区别. 根据注解的语义, 注解在类上面可以提高代码的可 ...
- Haskell语言学习笔记(22)MaybeT
Monad Transformers Monad 转换器用于将两个不同的Monad合成为一个Monad.Monad 转换器本身也是一个 Monad. MaybeT MaybeT 这个 Monad 转换 ...
- JDK、JRE和JAR区别(转载)
JDK里面的工具也是用Java编写的,它们本身运行的时候也需要一套JRE,如C:/Program Files/Java/jdk1.5.x/目录下的JRE.而C:/Program Files/Java/ ...
- MapReduce超时原因(Time out after 300 secs)
目前碰到过三种原因导致 Time out after 300 secs. 1. 死循环 这是最常见的原因.显式的死循环很容易定位,隐式的死循环就比较麻烦了,比如正则表达式.曾经用一个网上抄来的邮箱正则 ...