题目描述

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. 1.1- 1.2 hive入门

    一.hive是什么 由Facebook开源用于解决海量结构化日志的数据统计: Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射成一张表, 并提供类SQL查询功能: 构建在Had ...

  2. PXE与cobbler实现系统自动安装

    安装操作系统的流程事实上并不复杂,如果你要给三五台服务器安装系统那么我们手工去安装即可.但是当我们要部署的是大型机房的操作系统的时候显然手动安装去一台一台的点,是不现实的.尤其现在互联网行业高速发展, ...

  3. Angular6在自定义指令中使用@HostBingDing() 和@HostListener()

    emmm,,,最近在为项目的第二阶段铺路,偶然看到directive,想想看因为项目已经高度集成了第三方组件,所以对于自定义指令方面的经验自己实在知之甚少,后面经过阅读相关资料,总结一篇关于在自定义指 ...

  4. beans.xml中的头部配置

    Spring配置文件beans.xml头部配置解释 关于在beans.xml要使用哪些功能,官网上已经提供了每个功能说明和标准的头文件信息,当我们在开发使用时要哪些功能,都可以上官网去定位. http ...

  5. perl C/C++ 扩展(三)

    第三讲扩展库使用c++实现,在调用函数后,返回对象变量,perl 能正确使用所有对象成员 使用h2xs 命令生成初始文件 h2xs -A -n three_test 登录目录 cd three_tes ...

  6. E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))

    #include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...

  7. Java - 安装jdk并设置环境变量

    前言 双十一买了台新的笔记本,需要重新安装下Java,这里记录下安装的过程,毕竟万事开头难,就算是老手也不一定能一次就把Java安装成功. 安装jdk 作为一名Java开发,当然是要安装jdk了,如果 ...

  8. @PathVariable注解的使用

    带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义. 通过 @PathVariable 可以将 URL 中占位符参数绑 ...

  9. queue模块

    queue队列 :使用import queue,用法与进程Queue一样 queue is especially useful in threaded programming when informa ...

  10. UVA11988:悲剧文本(模拟链表)

    You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem wi ...