[USACO09JAN]Total Flow
OJ题号:
BZOJ3996、洛谷2936、SPOJ-MTOTALF、SCU3353
思路:
题目的要求是将所有边合并成一条边,求合并后的流量。
实际上相当于直接求最大流。
EdmondsKarp模板即可。
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
const int V=,E=,s=,t=,inf=0x7fffffff;
inline int idx(const char ch) {
return (ch<='Z')?(ch-'A'):(ch-'a'+);
}
struct Edge {
int from,to,remain;
};
int sz=;
Edge e[E<<];
std::vector<int> g[V];
inline void add_edge(const int u,const int v,const int c) {
e[sz]=(Edge){u,v,c};
g[u].push_back(sz);
sz++;
}
int p[V],a[V];
inline int Augment() {
memset(a,,sizeof a);
a[s]=inf;
std::queue<int> q;
q.push(s);
while(!q.empty()) {
int x=q.front();
q.pop();
for(unsigned i=;i<g[x].size();i++) {
Edge &y=e[g[x][i]];
if(!a[y.to]&&y.remain) {
p[y.to]=g[x][i];
a[y.to]=std::min(a[x],y.remain);
q.push(y.to);
}
}
if(a[t]) break;
}
return a[t];
}
inline int EdmondsKarp() {
int maxflow=;
while(int flow=Augment()) {
for(int i=t;i!=s;i=e[p[i]].from) {
e[p[i]].remain-=flow;
e[p[i]^].remain+=flow;
}
maxflow+=flow;
}
return maxflow;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin>>n;
for(int i=;i<n;i++) {
char u,v;
int c;
std::cin>>u>>v>>c;
add_edge(idx(u),idx(v),c);
add_edge(idx(v),idx(u),);
}
std::cout<<EdmondsKarp()<<std::endl;
return ;
}
[USACO09JAN]Total Flow的更多相关文章
- [USACO09JAN]Total Flow【网络流】
Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N & ...
- 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 < ...
- 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 < ...
- [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 ...
- 【luogu P2936 [USACO09JAN]全流Total Flow】 题解
题目链接:https://www.luogu.org/problemnew/show/P2936 菜 #include <queue> #include <cstdio> #i ...
随机推荐
- c语言的重构、清理与代码分析图形化浏览工具: CScout
网址: https://www.spinellis.gr/cscout/ https://www2.dmst.aueb.gr/dds/cscout/index.html https://github. ...
- git 使用https 和SSH 提交远程库小总结
一.使用https提交远程库 首先已经git commit -m “注释” 本地仓库关联远程github服务器:git remote add origin “https://XXXX.git” 提交 ...
- 转载:Linux内核参数的优化(1.3.4)《深入理解Nginx》(陶辉)
原文:https://book.2cto.com/201304/19615.html 由于默认的Linux内核参数考虑的是最通用的场景,这明显不符合用于支持高并发访问的Web服务器的定义,所以需要修改 ...
- LinkedList源码分析笔记(jdk1.8)
1.特点 LinkedList的底层实现是由一个双向链表实现的,可以从两端作为头节点遍历链表. 允许元素为null 线程不安全 增删相对ArrayList快,改查相对ArrayList慢(curd都会 ...
- Annoy 近邻算法
Annoy 随机选择两个点,以这两个节点为初始中心节点,执行聚类数为2的kmeans过程,最终产生收敛后两个聚类中心点 二叉树底层是叶子节点记录原始数据节点,其他中间节点记录的是分割超平面的信息 但是 ...
- 纯 Java 开发 WebService 调用测试工具(wsCaller.jar)
注:本文来自hacpai.com:Tanken的<纯 Java 开发 WebService 调用测试工具(wsCaller.jar)>的文章 基于 Java 开发的 WebService ...
- 两道SQL题目
1.查询省内所有城市气温都大于35度的省份(表名:Temp) SELECT province FROM Temp WHERE province NOT IN ( SELECT province FRO ...
- JQuery 插件一般方法
如今做web开发,jquery 几乎是必不可少的,就连vs神器在2010版本开始将Jquery 及ui 内置web项目里了.至于使用jquery好处这里就不再赘述了,用过的都知道.今天我们来讨论下jq ...
- PHP实现获取文件后缀名的几种常用方法
方法1: function get_file_type($filename){ $type = substr($filename, strrpos($filename, ".")+ ...
- hdu1506单调栈的宽度
很好的题目,单调栈上的宽度如何求 题解:https://blog.csdn.net/baidu_35643793/article/details/64440095 单调队列和单调栈都是去除没有用的数据 ...