题目描述

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')

输入输出样例

输入样例#1:

5
A B 3
B C 3
C D 5
D Z 4
B Z 6
输出样例#1:

3 
最大流问题
屠龙宝刀点击就送
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#define inf 0x7ffff using namespace std; char a,b;
bool vis[];
int atlas[][],Answer,dis[],n,m,v,i,j;
bool bfs()
{
queue<int>q;
q.push();
memset(dis,-,sizeof(dis));
dis[]=;
while(!q.empty() )
{
int f=q.front() ;q.pop() ;
for(i=;i<=;++i)
{
if(atlas[f][i]>&&dis[i]==-)
{
dis[i]=dis[f]+;
if(i==) return ;
else q.push(i);
}
}
}
return ;
}
void network()
{
memset(vis,,sizeof(vis));vis[]=;
vector<int>vec;
vec.push_back();
while(!vec.empty() )
{
int p=vec.back() ;
if(p!=)
{
int l;
for(l=;l<;++l)
{
if(atlas[p][l]>&&!vis[l])
{
vis[l]=;
vec.push_back(l);
break;
}
}
if(l>) vec.pop_back();
}
else if(p==)
{
int k,minx=inf;
for(i=;i<vec.size() ;++i)
{
int u=vec[i-],v=vec[i];
if(atlas[u][v]>&&atlas[u][v]<minx)
{
k=u;
minx=atlas[u][v];
}
}
Answer+=minx;
for(i=;i<vec.size() ;++i)
{
int u=vec[i-],v=vec[i];
atlas[u][v]-=minx;
atlas[v][u]+=minx;
}
while(!vec.empty() &&vec.back() !=k)
{
vis[vec.back() ]=;
vec.pop_back();
}
}
}
}
int main()
{
scanf("%d",&n);int w;
for(int i=;i<n;++i)
{
cin>>a>>b>>w;
atlas[(int)a-][(int)b-]+=w;
}
while(bfs())
network();
printf("%d",Answer);
return ;
}


洛谷 P2936 [USACO09JAN]全流Total Flow的更多相关文章

  1. 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)

    P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...

  2. 洛谷——P2936 [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  3. 【luogu P2936 [USACO09JAN]全流Total Flow】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2936 菜 #include <queue> #include <cstdio> #i ...

  4. 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 < ...

  5. [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  6. P2936(BZOJ3396) [USACO09JAN]全流Total Flow[最大流]

    题 裸题不多说,在网络流的练习题里,你甚至可以使用暴力. #include<bits/stdc++.h> using namespace std; typedef long long ll ...

  7. 洛谷P3128 [USACO15DEC]最大流Max Flow

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of N-1N−1 pipes to transpo ...

  8. 洛谷P3128 [USACO15DEC]最大流Max Flow [树链剖分]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

  9. 洛谷P3128 [USACO15DEC]最大流Max Flow [倍增LCA]

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

随机推荐

  1. 谈谈Spring Ioc的理解

    原文:http://blog.csdn.net/qq_22654611/article/details/52606960 学了几天Ioc了,但是对它的理解还是模模糊糊,看了这篇博客感觉对Ioc有了更深 ...

  2. JS正则表达式(一)

    正则表达常用符号 /..../  开始结束 ^ 开始 $ 结束 /s 任何非空字符  /S 非空 /d  匹配一个数字=[0-9] /D  匹配一个非数字=[^0-9] /w   匹配一个数字,下划线 ...

  3. Axure RP 7.0 标准教程(1)

    一. Axure RP 标准教程 1. 为什么学习 增加沟通效率

  4. C# in查询

    一.前言 在做项目中,我们会经常使用到 in 查询语句.那么如果我们用 EF 和 Linq 怎么写?接下来看代码 二.实例 我使用的是区域查询的例子,基本的 sql 语句如下: SELECT * FR ...

  5. HDU3065【AC自动机-AC感言】

    Fourth AC zi dong ji(Aho-Corasick Automation) of life 9A(其实不止交了10发...) 感言: 一开始多组数据这种小数据还是...无伤大局,因为改 ...

  6. uoj#267. 【清华集训2016】魔法小程序(乱搞)

    传送门 感觉很像FFT的过程的说-- 先来考虑\(b\)如何转化成\(c\),那么只要通过它的逆过程就可以了 首先,我们称"魔法"为比较两个数的字典序,记\(x=a_0\),那么把 ...

  7. builder模式的新学习

    builder模式的新学习 静态工厂和构造器有个共同的局限性:他们不能很好的扩展到大量的可选参数.大多数产品在牧歌可选与中都会有非零的值 对于这种类,应该使用哪种构造器或者静态方法来进行编写?程序员一 ...

  8. PostgreSQL-12-数据导入与导出

    1.数据导入 - COPY FROM CREATE DATABASE c04_datacleaning; \c c04_datacleaning 创建新的数据库 COPY table_name [ ( ...

  9. [題解](二分答案/單調隊列)luogu_P1419尋找段落

    果然又抄的題解... 顯然答案具有單調性,而對于平均數計算的式子我們移一下項, 若s[l..r]>mid*(r-l+1)无解, 於是我們把每個數都減去一個mid,看和的正負即可,如果為正就可能有 ...

  10. 洛谷 P1593 因子和 || Sumdiv POJ - 1845

    以下弃用 这是一道一样的题(poj1845)的数据 没错,所有宣称直接用逆元/快速幂+费马小定理可做的,都会被hack掉(包括大量题解及AC代码) 什么原因呢?只是因为此题的模数太小了...虽然990 ...