[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 ...
随机推荐
- mysql百万数据分页查询速度
百万数据测试 ,; 受影响的行: 时间: .080ms ,; 受影响的行: 时间: .291ms ,; 受影响的行: 时间: .557ms ,; 受影响的行: 时间: .821ms ,; 受影响的行: ...
- 构造 Codeforces Round #107 (Div. 2) B. Phone Numbers
题目传送门 /* 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( */ #include <cstdio> #include <algorithm> ...
- 一个JavaScript贷款计算器
通过本案例,将会学到: . 如何在文档中查找元素 . 如何通过表单input元素来获取用户的输入数据 . 如何通过文档元素来设置HTML内容 . 如何将数据存储在浏览器中 . 如何使用脚本发起HTTP ...
- lua_protobuf
http://www.cocoachina.com/bbs/read.php?tid-227404.html
- 扩展Snackbar 使其支持居中显示
https://github.com/nispok/snackbar 默认Snackbar支持底部或者顶部显示,不支持居中显示 查看Snackbar.java的源码可以看到createMarginLa ...
- javascript按钮点击事件问题
今天弄了个小测试,结果发现了点问题. 就是有一个按钮的点击事件,页面加载时候没反应,只有F12启用开发人员工具的时候才有反应.后来反复测试发现名字起的不太合理 function onclick(){ ...
- dutacm.club_1094_等差区间_(线段树)(RMQ算法)
1094: 等差区间 Time Limit:5000/3000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/Others)Total ...
- Tomcat环境的搭建
一.Tomcat的简单介绍 大家应该知道平时所说的C/S和B/S系统架构:C/S架构是基于客户端C和服务端S的,B/S架构是基于浏览器B和S服务端的,B/S架构中的server就是web服务器. To ...
- CAD使用DeleteXData删除数据(网页版)
主要用到函数说明: MxDrawEntity::DeleteXData 删除扩展数据,详细说明如下: 参数 说明 pzsAppName 删除的扩展数据名称,如果为空,删除所有扩展数据 js代码实现如下 ...
- 梦想CAD控件网页版搜索图面上的文字
在网页中查找到CAD控件图纸上的文字.点击此处在线演示. 主要用到函数说明: _DMxDrawX::NewSelectionSet 实例化一个构造选择集进行过滤,该类封装了选择集及其处理函数. _DM ...