洛谷——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 ...
随机推荐
- SQL Server 将Id相同的字段合并,并且以逗号隔开
例如:有表MO_Cook,字段:FoodRoom,name 有如下数据: 要求:将FoodRoom中值相同的字段合并成一行,并将name的值用逗号隔开. 需要用到:STUFF函数. 查询语句如下: ...
- js中call与apply的区别以及使用~
今天看了一下call与apply的区别~~ <!DOCTYPE html> <html> <head> <title>testCall</titl ...
- 【多视图几何】TUM 课程 第3章 透视投影
课程的 YouTube 地址为:https://www.youtube.com/playlist?list=PLTBdjV_4f-EJn6udZ34tht9EVIW7lbeo4 .视频评论区可以找到课 ...
- JS事件用法
1.常用事件理解
- Focal Loss笔记
论文:<Focal Loss for Dense Object Detection> Focal Loss 是何恺明设计的为了解决one-stage目标检测在训练阶段前景类和背景类极度不均 ...
- 二十、springboot之jpa开发@MappedSuperclass 注解说明
@MappedSuperclass使用条件: 当我们进行开发项目时,我们经常会用到实体映射到数据库表的操作,此时我们经常会发现在我们需要映射的几个实体类中,有几个共同的属性,例如编号ID,创建者,创建 ...
- 【Android开发】之MediaPlayer的错误分析
最近在做媒体播放器,使用了Android自带的MediaPlayer,经常性会碰到MediaPlayer报错的情况,找过网上的,感觉总结的不是很好或者比较散.下面,我来总结一下使用MediaPlaye ...
- Linux_僵尸进程、挂载、block块、inode号
僵尸进程: 基本概念: 进程分为父进程和子进程 父进程一死 子进程都会死 杀死主进程的时候 子进程也会被杀死 僵尸进程:主进程被杀死了 但是子进程还活着 子进程不会占用cpu但还是会占着内存 ...
- Sublime Text 3 web 开发常用配置
前沿 Sublime Text 是一个代码编辑器(Sublime Text 2是收费软件,但可以无限期试用),也是HTML和散文先进的文本编辑器.Sublime Text是由程序员Jon Skinne ...
- echarts一些笔记
console.log(); 浏览器显示 $.ajax({ url : "ajax/echartWelcome.action", type : "post", ...