题目描述

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 <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define maxn 500 using namespace std; struct EdgeType {
int v,next,flow;
};
struct EdgeType edge[maxn*maxn*]; int if_z,cnt=,head[maxn],deep[maxn],n; char Cget; inline void in(int &now)
{
now=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} inline void edge_add(int u,int v,int w)
{
edge[++cnt].v=v,edge[cnt].flow=w,edge[cnt].next=head[u],head[u]=cnt;
edge[++cnt].v=u,edge[cnt].flow=,edge[cnt].next=head[v],head[v]=cnt;
} bool BFS()
{
queue<int>que;que.push('A');
memset(deep,-,sizeof(deep));
deep['A']=;
while(!que.empty())
{
int pos=que.front();que.pop();
for(int i=head[pos];i;i=edge[i].next)
{
if(deep[edge[i].v]<&&edge[i].flow>)
{
deep[edge[i].v]=deep[pos]+;
if(edge[i].v=='Z') return true;
que.push(edge[i].v);
}
}
}
return false;
} int flowing(int now,int flow)
{
if(flow==||now=='Z') return flow;
int oldflow=;
for(int i=head[now];i;i=edge[i].next)
{
if(deep[edge[i].v]!=deep[now]+||edge[i].flow==) continue;
int pos=flowing(edge[i].v,min(flow,edge[i].flow));
flow-=pos;
oldflow+=pos;
edge[i].flow-=pos;
edge[i^].flow+=pos;
if(flow==) return oldflow;
}
return oldflow;
} int dinic()
{
int pos=;
while(BFS()) pos+=flowing('A',0x7ffffff);
return pos;
} int main()
{
in(n);char u,v;int w;
while(n--)
{
cin>>u>>v;in(w);
edge_add(u,v,w);
}
printf("%d\n",dinic());
return ;
}

最新文章

  1. zookeeper工作原理、安装配置、工具命令简介
  2. 大熊君大话NodeJS之------Connect中间件模块(第一季)
  3. GoF--观察者模式
  4. BZOJ1036——树的统计count
  5. Android学习笔记----解决“com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536”问题
  6. [iOS]关于状态栏(UIStatusBar)的若干问题
  7. Web应用程序系统的多用户权限控制设计及实现-总述【1】
  8. codevs 1200 同余方程 (Extend_Eulid)
  9. Wormholes(SPFA+Bellman)
  10. ZOJ3784 String of Infinity 高大上的AC自动机 数据原来这么水啊!不算输入输出只有5-7行
  11. oracle创建user具体指示
  12. Error:No such property: GROUP for class: org.gradle.api.publication.maven.internal.deployer.DefaultGroovyMavenDeployer
  13. 记录一次网站漏洞修复过程(三):第二轮处理(拦截SQL注入、跨站脚本攻击XSS)
  14. MongoDB 关系
  15. 面试(三)---volatile
  16. 关于socket.io的使用
  17. Sql Server 复制数据库
  18. poj 2356 (抽屉原理)
  19. IIS错误提示:另一个程序正在使用此文件 进程无法访问
  20. React 同构思想

热门文章

  1. python3和Python2的区别
  2. nginx访问日志(access_log)
  3. Python正则表达式详解——re库
  4. AHB 总线问答(转)
  5. Python 变量作用域 LEGB (上)—— Local,Global,Builtin
  6. LeetCode(152) Maximum Product Subarray
  7. Jack Straws POJ - 1127 (几何计算)
  8. Nastya Studies Informatics CodeForces - 992B (大整数)
  9. Docker容器技术的核心原理
  10. hdu 5441