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 ;
}

[USACO09JAN]Total Flow【网络流】的更多相关文章

  1. [USACO09JAN]Total Flow

    OJ题号: BZOJ3996.洛谷2936.SPOJ-MTOTALF.SCU3353 思路: 题目的要求是将所有边合并成一条边,求合并后的流量. 实际上相当于直接求最大流. EdmondsKarp模板 ...

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

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

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

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

  4. [USACO09JAN]全流Total Flow

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

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

  6. 洛谷 P2936 [USACO09JAN]全流Total Flow

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

  7. BZOJ3396: [Usaco2009 Jan]Total flow 水流

    3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 45  Solved: 27[Sub ...

  8. 3396: [Usaco2009 Jan]Total flow 水流

    3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 179  Solved: 73[Su ...

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

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

随机推荐

  1. [译]Android Studio 3.6 新特性概览

    设计 设计编辑器 设计编辑器(比如布局编辑器和导航编辑器)现在提供了一个拆分视图模式,能够同时查看 UI 界面的 Design 视图和 Code 视图.拆分视图取代并改进了早期的预览窗口,并且可以对每 ...

  2. C#设计模式学习笔记:(16)观察者模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7928521.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第四个模式--观 ...

  3. UML之三、建模元素(2)

    本章继续介绍建模元素 https://blog.csdn.net/bit_kaki/article/details/78471760 1:边界 任何一个对象都有一个边界,外界只能通过这个边界来认识对象 ...

  4. MySql数据库精简与绿色启动

    1.下载MYSQL的zip包,解压ZIP包 版本低的相对需要的空间少,最好能在mysql-5.6以下,我测试的最高5.6版本为mysql-5.6.46,主要是里面有my.ini文件,高于5.6的版本里 ...

  5. mysql 查询语句的执行顺序(重重点)

    一 SELECT语句关键字的定义顺序 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOI ...

  6. 这 100 道 Python 题,拿去刷!!!

    2020年,学 Python 还有价值吗? 根据 2020 年 2 月的 TIOBE 编程语言排行榜显示,Python仍然稳居第三位. 此排行榜排名基于互联网上有经验的程序员. 课程和第三方厂商的数量 ...

  7. Android应用第一次启动时的欢迎界面制作

    原理是这样,我们在SharedPreferences中存储一个int型数据,用来代表第几次登录,每次启动时都读取出来判断是不是第一次启动,然后依次判断是否要显示欢迎界面, 具体实现如下: 设置一个欢迎 ...

  8. 三星正在改善1Gb MRAM寿命问题

    据报道三星已经成功研发出有望替代嵌入式闪存存储器(eFlash)的嵌入式磁阻随机访问内存(eMRAM),容量为1Gb,测试芯片的优良率已达90%. 随着5G物联网时代的来临,存储器领域发展快速,而在这 ...

  9. Excel_单元格格式_查找替换、定位

    不重复! 显示格式:Ctrl+1 1,合并后居中,填充颜色,设置单元格边框,划斜线,格式刷(单击,双击) 2,单元格数字格式,格式不会改变值!自定义(编码规则) 4个 a :只显示星期:周+aaa:周 ...

  10. 战“疫”背后的AI身影丨曼孚科技

    近期新型冠状病毒肺炎的疫情,牵动着全国上下人民的心. 截止2月11日上午10点,全国确诊人数已达42708人,疑似病例21675人. 突发的疫情让部分地区的快速诊疗能力出现了结构性的缺失,为了打赢这场 ...