[USACO09JAN]Total Flow【网络流】
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【网络流】的更多相关文章
- [USACO09JAN]Total Flow
OJ题号: BZOJ3996.洛谷2936.SPOJ-MTOTALF.SCU3353 思路: 题目的要求是将所有边合并成一条边,求合并后的流量. 实际上相当于直接求最大流. EdmondsKarp模板 ...
- 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)
P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...
- 洛谷——P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- 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 < ...
- 洛谷 P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- BZOJ3396: [Usaco2009 Jan]Total flow 水流
3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 45 Solved: 27[Sub ...
- 3396: [Usaco2009 Jan]Total flow 水流
3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 179 Solved: 73[Su ...
- P2936(BZOJ3396) [USACO09JAN]全流Total Flow[最大流]
题 裸题不多说,在网络流的练习题里,你甚至可以使用暴力. #include<bits/stdc++.h> using namespace std; typedef long long ll ...
随机推荐
- 曹工说Spring Boot源码(20)-- 码网灰灰,疏而不漏,如何记录Spring RedisTemplate每次操作日志
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 浅析word2vec(一)
1 word2vec 在自然语言处理的大部分任务中,需要将大量文本数据传入计算机中,用以信息发掘以便后续工作.但是目前计算机所能处理的只能是数值,无法直接分析文本,因此,将原有的文本数据转换为数值数据 ...
- C#XML文件操作随笔
以为公司一直没有电源屏厂家协议解析为DevVars相关的软件,手写费时费力还容易出错,用了2天时间做了一个.txt协议文件筛选加并解析成xml文件的工具,总结一下用到的几个知识点 1.LINQ 是一个 ...
- python基础扩展
一.垃圾回收机制 垃圾回收机制是自动帮助我们管理内存,清理垃圾的一种工具 1.引用计数 当一个对象的引用被创建或者复制时,对象的引用计数加1: 当一个对象的引用被销毁时,对象的引用计数减1: 当对象的 ...
- vue history模式 ios微信分享 踩过的坑
背景:教育项目,整体依赖于微信环境,涉及到微信分享.微信二次分享 问题:vue使用history模式在iso微信下分享设置出错(签名认证错误.分享设置失败) 问题发现路径 1.按照微信公众号官方文档设 ...
- 阿里巴巴Java开发手册之并发处理注意事项
1. [强制]获取单例对象需要保证线程安全,其中的方法也要保证线程安全.说明:资源驱动类.工具类.单例工厂类都需要注意.2. [强制]创建线程或线程池时请指定有意义的线程名称,方便出错时回溯.正例:p ...
- cat - EOF标志的使用
前提 cat命令是用于连接文件并输出到标准输出设备或指定文件中. EOF为标志,可以替换为其他字符串 代码块 ``` 将文件内容作为标准输出也就是将文件内容输出到屏幕中,也可写作 cat filena ...
- 剑指offer-面试题52-两个链表的第一个公共节点-链表
/* 题目: 求两个链表的第一个公共节点. */ /* 思路: 见代码. */ #include<iostream> #include<cstring> #include< ...
- comTest.json文件中内容,被NewsList.vue文件引入
本文目标:就是把扩散名为.json文件中数据,传递给NewsList.vue文件.主要通过导出,并传递给data(){}变紧 新建文件名为:commTest.json { "schoolNa ...
- 【python基础语法】数据类型:数值、字符串 (第2天课堂笔记)
""" 数据类型: 一.数值类型:整数 浮点数 布尔值 二.序列类型:字符串.列表 元祖 三.散列类型:字典 集合 可变数据类型: 列表 字典 集合,可以改动内存地址数据 ...