洛谷——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')
输入输出样例
网络流模板题,注意字母存在大小写!
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 1001000 #define inf 9999999 using namespace std; char ch; queue<int>q; ,ans; int to[N],cap[N],cnt[N],lev[N],head[N],nextt[N]; int read() { ,f=; char ch=getchar(); ;ch=getchar();} +ch-',ch=getchar(); return x*f; } int add(int x,int y,int z) { tot++;to[tot]=y;cap[tot]=z;nextt[tot]=head[x];head[x]=tot; tot++;to[tot]=x;cap[tot]=;nextt[tot]=head[y];head[y]=tot; } int bfs() { while(!q.empty()) q.pop(); for(int i=s;i<=n;i++) { lev[i]=-; cnt[i]=head[i]; } q.push(s),lev[s]=; while(!q.empty()) { x=q.front();q.pop(); for(int i=head[x];i;i=nextt[i]) { int t=to[i]; &&cap[i]>) { lev[t]=lev[x]+; q.push(t); if(t==e) return true; } } } return false; } int dinic(int x,int flow) { if(x==e) return flow; ; for(int &i=cnt[x];i;i=nextt[i]) { int t=to[i]; &&lev[t]>lev[x]) { delta=dinic(t,min(cap[i],flow-rest)); if(delta) { cap[i]-=delta; cap[i^]+=delta; rest+=delta; if(rest==flow) break; } } } ; return rest; } int main() { n=read(); s=;e=; ;i<=n;i++) { cin>>ch;x=ch-; cin>>ch;y=ch-; z=read(); add(x,y,z); } n=; while(bfs()) ans+=dinic(s,inf); printf("%d",ans); ; }
洛谷——P2936 [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 ...
- 洛谷 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 ...
- 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 < ...
- [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- P2936(BZOJ3396) [USACO09JAN]全流Total Flow[最大流]
题 裸题不多说,在网络流的练习题里,你甚至可以使用暴力. #include<bits/stdc++.h> using namespace std; typedef long long ll ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow
P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
随机推荐
- 一段鬼畜风格的JavaScript解密
在CSDN上看到有人提问一段JS怎么解密,虽然已经是四年前的问题了,还是解一下. 原问题地址: 这段JS怎样解密? [问题点数:40分,结帖人seo2014] 这是楼主发出的原JS: /*ZlQEIn ...
- Framebuffer 驱动学习总结(一) ---- 总体架构及关键结构体
一.Framebuffer 设备驱动总体架构 帧缓冲设备为标准的字符型设备,在Linux中主设备号29,定义在/include/linux/major.h中的FB_MAJOR,次设备号定义帧缓冲的个数 ...
- [转载]Windows服务编写原理及探讨(4)
(四)一些问题的讨论 前面几章的内容都是服务的一些通用的编写原理,但里面隐含着一些问题,编写简单的服务时看不出来,但遇到复杂的应用就会出现一些问题,所以本章就是用来分析.解决这些问题的,适用于高级应用 ...
- Windows 10安装uWSGI:不可行、失败了
Windows 10家庭中文版,Python 3.6.4,uwsgi-2.0.17.tar.gz,压缩工具-7-zip 提示:请不要和我一样尝试,浪费时间,去Linux上玩吧! 几个小时的安装经历 昨 ...
- Java项目打war包的方法
我们可以运用DOS命令来手工打war包: 首先,打开DOS命令行,敲入“jar”,我们发现它提示不是内部或外部的命令这样的错误,这时八成是你的JAVA环境没有配置好,我们可以用JAVA_HOME方式或 ...
- Go 的package
一.包的一些基本的概念 1.在同一个目录下的所有go文件中,只能有一个main函数.如果存在多个main函数,则在编译的时候会报错 那么,在同一个目录下的两个go文件究竟是什么关系? 为什么会彼此影响 ...
- excl筛选求和
Excel中的筛选,是一个很常用的功能.但不知道是有意还是疏忽,Excel没有直接提供在筛选后的一些统计功能,例如求和.平均值等.而由于筛选的主要功能之一就是可以方便快捷的进行变换,所普通的以直接在数 ...
- IntelliJ IDEA 修改IDE字体、代码字体。
IntelliJ IDEA 默认的 IDE 菜单字体太小,看着不舒服 ,我们调节下: ==============以上修改 仅仅针对的IDE字体,对代码的字体不生效. 所以如果代码 你觉得小 还得修改 ...
- Codeforces 552C Vanya and Scales(进制转换+思维)
题目链接:http://codeforces.com/problemset/problem/552/C 题目大意:有101个砝码重量为w^0,w^1,....,w^100和一个重量为m的物体,问能否在 ...
- 如何修改SQL Server 2000的数据库逻辑与物理名称
在项目中使用SQL Server 2000创建了一个数据库,发现名称与另一个数据库太相似,于是决定更改名称,包括: 在企业管理器中看到的数据库名,也是实际应用程序中连接用的数据库名称: 在磁盘上看的物 ...