[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 ...
随机推荐
- less新手入门(五)—— CssGuards、循环、合并
九. CssGuards 警卫也可以应用于css选择器,这是一种语法糖,用于声明mixin,然后立即调用它. 例如,在1.5.0之前,您必须这样做 .my-optional-style() when ...
- CF482C Game with Strings
题意 你和你的朋友玩一个游戏,游戏规则如下. 你的朋友创造 n 个长度均为 m 的不相同的字符串,然后他随机地选择其中一个.他选择这些字符串的概率是相等的,也就是说,他选择 n 个字符串中的每一个的概 ...
- 贪心 CodeForces 137B Permutation
题目传送门 /* 贪心:因为可以任意修改,所以答案是没有出现过的数字的个数 */ #include <cstdio> #include <algorithm> #include ...
- asp.net MVC Session 第二次加载丢失问题
在做本地调试时发现,session 加载过了对象之后,每次都是第一次加载成功,第二次再进来时候session 就是失效丢失了,究其原因:原来是因为第一次加载session 过大导致,原有其他sessi ...
- 60使用nanopim1plus查看HDMI显示分辨率的问题(分色排版)V1.0
60使用nanopim1plus查看HDMI显示分辨率的问题(分色排版)V1.0 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/5 17: ...
- Mysql5.7多源复制,过滤复制一段时间后增加复制一个库的实现方法
多源复制如果是整个实例级别的复制,那不存在下面描述的情况. 如果是对其中一个或多个主实例都是过滤复制,并且运行一段时间后,想在这个源上再增加一个库怎么实现? 主1:192.168.1.10 330 ...
- 梦想CAD控件COM接口自定义命令
在CAD软件操作中,为方便使用者,使用自定义命令发出命令,完成CAD绘图,修改,保存等操作.点击此处下载演示实例. _DMxDrawX::RegistUserCustomCommand 向CAD控件注 ...
- Spring Boot 创建hello world项目
Spring Boot 创建hello world项目 1.创建项目 最近在学习Spring Boot,这里记录使用IDEA创建Spring Boot的的过程 在1出勾选,选择2,点击Next 这里填 ...
- 02Struts2 环境搭建
Struts2 环境搭建 1.下载 Apache Struts2 类库 2.建立web工程 3.配置web.xml <?xml version="1.0" encoding= ...
- CentOS 7中firewall防火墙详解和配置以及切换为iptables防火墙--转载
最近在linux(这里用到的是Centos7的64位版本)安装nginx时,在开放80端口时用iptables设置端口 和重启服务发现提示未找到文件,在网络上收集查找后发现在Centos7中iptab ...