[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')
输入输出样例
5
A B 3
B C 3
C D 5
D Z 4
B Z 6
3
#include<cstdio>
#include<cstring>
#define inf 100000000
int n,s,t,tw;
int a,b,c;
char ch[],cn[];
int h[],hs=;
struct edge{int s,n,w;}e[];
int d[],q[],head,tail;
inline int min(int x,int y){return x<y?x:y;}
void bfs(){
memset(d,,sizeof(d));
head=tail=;
d[s]=,q[head++]=s;
while(head>tail){
a=q[tail++];
for(int i=h[a];i;i=e[i].n)
if(!d[e[i].s]&&e[i].w){
d[e[i].s]=d[a]+;
if(e[i].s==t) return;
q[head++]=e[i].s;
}
}
}
int ap(int k,int w){
if(k==t) return w;
int uw=w;
for(int i=h[k];i&&uw;i=e[i].n)
if(e[i].w&&d[e[i].s]==d[k]+){
int wt=ap(e[i].s,min(uw,e[i].w));
if(wt) e[i].w-=wt,e[i^].w+=wt,uw-=wt;
else d[e[i].s]=;
}
return w-uw;
}
bool Dinic(){
bfs();
if(!d[t]) return ;
tw+=ap(s,inf);
return ;
}
int main(){
scanf("%d",&n);
s='A',t='Z';
while(n--){
scanf("%s%s%d",ch,cn,&c);
a=ch[],b=cn[];
e[++hs]=(edge){b,h[a],c},h[a]=hs;
e[++hs]=(edge){a,h[b],c},h[b]=hs;
}
while(Dinic());
printf("%d\n",tw);
return ;
}
我终于能顺手的,顺手A了,网络流真心好实现。
题目来源:洛谷
[USACO09JAN]全流Total Flow的更多相关文章
- 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)
P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...
- AC日记——[USACO09JAN]全流Total Flow 洛谷 P2936
题目描述 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 < ...
- 洛谷 P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- 【luogu P2936 [USACO09JAN]全流Total Flow】 题解
题目链接:https://www.luogu.org/problemnew/show/P2936 菜 #include <queue> #include <cstdio> #i ...
- P2936(BZOJ3396) [USACO09JAN]全流Total Flow[最大流]
题 裸题不多说,在网络流的练习题里,你甚至可以使用暴力. #include<bits/stdc++.h> using namespace std; typedef long long ll ...
- [USACO09JAN]Total Flow【网络流】
Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N & ...
- BZOJ3396: [Usaco2009 Jan]Total flow 水流
3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 45 Solved: 27[Sub ...
- Openvswitch原理与代码分析(5): 内核中的流表flow table操作
当一个数据包到达网卡的时候,首先要经过内核Openvswitch.ko,流表Flow Table在内核中有一份,通过key查找内核中的flow table,即可以得到action,然后执行acti ...
随机推荐
- Java多线程(十)线程间通信 join
若果主线程想等待子线程执行完成之后再结束,可以用join方法 join 和sleep区别 join内部有wait实现,所以当执行join方法后,当前线程的锁被释放,那么其他线程就可以调用此线程的同步方 ...
- Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)
题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...
- 设计模式("大话设计模式"读书笔记 C#实现)
前言:毫无疑问 ,学习一些设计模式,对我们的编程水平的提高帮助很大.写这个博客的时候自己刚开始学习设计模式,难免有错,欢迎评论指正. 我学设计模式的第一本书是“大话设计模式”. 1.为什么要学设计模式 ...
- 重新学习Java——对象和类(一)
之前通过记笔记的方法,对于<Java核心技术>这本书的前一章进行了重新的复习,感觉效果很好,比单独看书带来了更好的复习效果,了解了很多以前不是很注意的一些细节,但是在一些自己较为熟悉的地方 ...
- poj1778 All Discs Considered
思路: 拓扑排序.贪心. 实现: #include <bits/stdc++.h> using namespace std; vector<]; int n1, n2; inline ...
- ElasticSearch学习笔记--一些规范,会持续更新
我们在ElasticSearch中存储的数据一般是采用json的格式存储,所以ElasticSearch中有一个叫Mapper的东西用来定义jsonschema来规范这个json 但是这个mapper ...
- 【译】x86程序员手册26-7.5任务切换
7.5 Task Switching 任务切换 The 80386 switches execution to another task in any of four cases: 80386在以下四 ...
- Spartan6系列之Spartan6系列之芯片时钟资源深入详解
1. 时钟资源概述 时钟设施提供了一系列的低电容.低抖动的互联线,这些互联线非常适合于传输高频信号.最大量减小时钟抖动.这些连线资源可以和DCM.PLL等实现连接. 每一种Spartan-6芯片提 ...
- 使用python获得N个区分度较高的RGB颜色值
获得任意N个区分度最高的RGB颜色值是一个经典的问题,之前在做一些可视化的东西时需要解决这个问题.首先去网上找了一些方法,未果,于是想自己来搞,心里的想法是,先给出一个距离函数用来度量两个RGB颜色值 ...
- 扩增子分析解读2提取barcode 质控及样品拆分 切除扩增引物
本节课程,需要完成扩增子分析解读1质控 实验设计 双端序列合并 先看一下扩增子分析的整体流程,从下向上逐层分析 分析前准备 # 进入工作目录 cd example_PE250 上一节回顾:我们拿到了双 ...