题目描述

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 0x7ffffusing namespace std;char a,b;
bool vis[70];
int atlas[70][70],Answer,dis[70],n,m,v,i,j;
bool bfs()
{queue<int>q;q.push(0);memset(dis,-1,sizeof(dis));dis[0]=1;while(!q.empty() ){int f=q.front() ;q.pop() ;for(i=0;i<=57;++i){if(atlas[f][i]>0&&dis[i]==-1){dis[i]=dis[f]+1;if(i==25) return 1;else q.push(i); }}}return 0;
}
void network()
{memset(vis,0,sizeof(vis));vis[0]=1;vector<int>vec;vec.push_back(0);while(!vec.empty() ){int p=vec.back() ;if(p!=25){int l;for(l=0;l<58;++l){if(atlas[p][l]>0&&!vis[l]){vis[l]=1;vec.push_back(l);break; }}if(l>57) vec.pop_back();}else if(p==25){int k,minx=inf;for(i=1;i<vec.size() ;++i){int u=vec[i-1],v=vec[i];if(atlas[u][v]>0&&atlas[u][v]<minx){k=u;minx=atlas[u][v];}}Answer+=minx;for(i=1;i<vec.size() ;++i){int u=vec[i-1],v=vec[i];atlas[u][v]-=minx;atlas[v][u]+=minx;}while(!vec.empty() &&vec.back() !=k){vis[vec.back() ]=0;vec.pop_back();}}}
}
int main()
{scanf("%d",&n);int w;for(int i=0;i<n;++i){cin>>a>>b>>w;atlas[(int)a-65][(int)b-65]+=w;}while(bfs())network();printf("%d",Answer);return 0;
}

转载于:https://www.cnblogs.com/ruojisun/p/6502764.html

洛谷 P2936 [USACO09JAN]全流Total Flow相关推荐

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

    题意简述 给定一颗树,每次操作可以使两个点最短路上的点+1,求最大的点 题解思路 树上差分 若操作u, v,则++f[u], ++f[v], --f[lca(u, v)], --f[father(lc ...

  2. 洛谷P3376 网络最大流

    不是讲网络流,就是存个板子 另外我的Dinic跑得比EK慢一倍可还行( 附两份比较好的教程,均来自洛谷日报 EK\sf \color{blue}EKEK Dinic\sf \color{blue}Di ...

  3. 洛谷 - P4015 运输问题(费用流)

    题目链接:点击查看 题目大意:有n个卖家和m个买家,每个卖家会卖ai个物品,每个买家会买bi个物品,每个卖家向每个卖家卖东西会有一定的代价,问如何匹配才能让代价最小/最大 题目分析:和上一道题大同小异 ...

  4. 洛谷 - P4014 分配问题(费用流/KM)

    题目链接:点击查看 题目大意:给出n个工人和n个工作,每个人做每一个工作的效率都是不同的,问如何分配能让效率最低/最高 题目分析:最小费用最大流和最大费用最大流的模板题,直接套模板跑答案就行了,没有任 ...

  5. 洛谷3376 网络最大流

    题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...

  6. 洛谷 P2935 [USACO09JAN]最好的地方Best Spot

    题目传送门 . . . . . 分析 水题一道 先用Flody算出各点之间的最短路 求和点到F_i的距离 再比较谁更短即可 . . . . . 程序: #include<iostream> ...

  7. 洛谷P2472-网络最大流(点的拆分)

    题目链接-P2472 还是 点的拆分,把所有点拆为两个点,其连接容量为点的数值,然后建图求解即可 #include <iostream> #include <cstdio> # ...

  8. 洛谷—— P2934 [USACO09JAN]安全出行Safe Travel || COGS ——279|| BZOJ——1576

    https://www.luogu.org/problem/show?pid=2934 题目描述 Gremlins have infested the farm. These nasty, ugly ...

  9. 洛谷P3980 志愿者招募——费用流

    洛谷P3980 [NOI2008]志愿者招募--费用流 题目介绍 题目描述 输入格式 输出格式 测试样例 题解 代码 题目介绍 题目描述 链接: 传送门. 申奥成功后,布布经过不懈努力,终于成为奥组委 ...

  10. 洛谷P4043 费用流

    这题的建图方式可以类比洛谷P1251 我是由那个题才想到这么建的,由于每条边至少经过一次,我们又不清楚需要跑多少次,把边看成点,点与汇点相连,可是我们又不知道最大流应该是多少,直接这么连会发生错误.利 ...

最新文章

  1. 【python语言基础】疑难点整理1
  2. git diff Git查看版本改动
  3. python【蓝桥杯vip练习题库】ALGO-55 矩阵加法
  4. Linux下理解进程,fork()创建子进程
  5. JAVA常见异常种类
  6. php怎么更新多条数据,PHP中批量更新数据表中多条记录
  7. 论文浅尝 | 知识库问答中关系检测的学习表示映射
  8. Linux入门学习(十)
  9. python collections 模块中的 deque
  10. 如何从Swift调用Objective-C代码?
  11. Archlinux GRUB2 配置
  12. 面试2年经验的Java程序员面试题部分带答案
  13. 矩阵分解(5)-- 正定矩阵与半正定矩阵
  14. 基于SSH开发在线问卷调查系统
  15. Python自省机制
  16. php中划弧线,cad画弧形的快捷键是什么?如何画弧形?
  17. opencv的基本数据类型CvPoint,CvSize,CvRect和CvScalar
  18. 2019全网最全面试详解.
  19. 求生之路2rpg服务器账号跨服,求生之路2官方地图指令大全及地图文件夹位置说明...
  20. C语言:添加和显示,数据保存在文件中,下次打开可以获取之前录入的内容

热门文章

  1. matlab中的点乘与不加点的乘
  2. [.NET领域驱动设计实战系列]专题十:DDD扩展内容:全面剖析CQRS模式实现
  3. Kafka应用实践与生态集成
  4. 关于惠普hp服务器开机时F10菜单变成F10 Function Disabled的解决方法
  5. hdu1501 Zipper--DFS
  6. 51nod 1130 N的阶乘的长度 V2(斯特林近似)
  7. 反射(3)反射应用:一个插件项目
  8. 如何给main传参数
  9. SPF邮件伪造漏洞测试脚本
  10. 经典的Fisher-Yates Shuffle算法