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.

思路:建立源点为A,汇点为Z的网络跑最大流

注意读入格式

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm> using namespace std; const int maxn=1e4 + ;
const int inf=0x3f3f3f3f; template<class T>inline void read(T &res)
{
char c;T flag=;
while((c=getchar())<''||c>'')if(c=='-')flag=-;res=c-'';
while((c=getchar())>=''&&c<='')res=res*+c-'';res*=flag;
} struct edge{int from,to,cap,flow;}; struct isap
{
int n,s,t,p[maxn],d[maxn],cur[maxn],num[maxn];
bool vis[maxn];
vector<int>g[maxn];
vector<edge>edges;
void init(int n,int s,int t) {
this->n = n;
this->s = s;
this->t = t;
for(int i = ;i <= n;i++) g[i].clear();
edges.clear();
}
void addegde(int from,int to,int cap) {
edges.push_back((edge){from, to, cap, });
edges.push_back((edge){to, from, , });
int m = edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
} int augment() {///找增广路
int x = t,a = inf;
while(x!=s) {
a = min(a, edges[p[x]].cap - edges[p[x]].flow);
x = edges[p[x]].from;
}
x=t;
while(x != s) {
edges[p[x]].flow += a;
edges[p[x]^].flow = -a;
x = edges[p[x]].from;
}
return a;
}
int maxflow() {///更新最大流
int flow = ;
memset(num, , sizeof(num));
memset(cur, , sizeof(cur));
for(int i = ; i <= n; i++) num[d[i]]++;
int x = s;
while(d[s] < n) {///最长的一条链上,最大的下标是nv-1,如果大于等于nv说明已断层
if(x == t) {
flow += augment();
x = s;//回退
}
bool ok = ;
for(int i = cur[x]; i < g[x].size(); i++) {
edge &e = edges[g[x][i]];
if(d[x] == d[e.to] + && e.cap > e.flow) {
p[e.to] = g[x][i];
cur[x] = i;x = e.to;
ok = ;
break;
}
}
if(!ok) {
int m = n-;
for(int i = ; i < g[x].size();i++) {
edge &e=edges[g[x][i]];
if(e.cap>e.flow) m=min(m,d[e.to]);
}
num[d[x]]--;
if(!num[d[x]]) break;
d[x] = m+;
num[d[x]]++;
cur[x] = ;
if(x != s) x = edges[p[x]].from;
}
}
return flow;
}
}ISAP; int main()
{
int n,m,s,t,u,v,w;
n = ;
s = ,t = ;
cin >> m;
ISAP.init(n,s,t);
for(int i = ; i <= m; i++) {
char u,v;
int c;
cin >> u >> v >> c;
ISAP.addegde(u - 'A' + ,v - 'A' + ,c);
}
printf("%d\n",ISAP.maxflow());
return ;
}

最新文章

  1. console.log(&quot;A&quot;-&quot;B&quot;+&quot;3&quot;)=?
  2. jQuery Scroll Follow
  3. Signatures of Mutational Processes in Human Cancer
  4. BZOJ4134 : ljw和lzr的hack比赛
  5. JAVA 静态成员 static
  6. unity区分点击在3D物体还是2D UI上
  7. 奇葩app大盘点,你知道几个
  8. 壮美大山包-2017中国大山包国际超百公里ITRA积分赛赛记
  9. EXT 设置编辑框为只读
  10. 关于Xcode9.0版本模拟器Reset重置操作变更
  11. js 位运算符
  12. 醉汉随机行走/随机漫步问题(Random Walk Randomized Algorithm Python)
  13. 部署showdoc
  14. StringUtils学习
  15. ParseFloat有超长的小数位数的解决
  16. leetcode110
  17. MySQL GTID (三)
  18. 【转载】如何选择MySQL存储引擎
  19. 一张图入门QT设计器
  20. python中注释的书写

热门文章

  1. Python字符串字母大小写变换
  2. go实现java虚拟机01
  3. absoulue与relative配合定位盒子居中问题
  4. element-ui 组件 el-calendar 农历显示问题
  5. 1、SSH无密码访问
  6. 如何利用dokcer提交我的比赛代码
  7. java自学:从零基础到入门系列--java环境变量的配置JDK的下载安装图解
  8. 消息队列(三)Apache ActiveMQ
  9. Redis 安装 (未)
  10. springboot打成war包并携带第三方jar包