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的更多相关文章

  1. [USACO09JAN]Total Flow【网络流】

    Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N & ...

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

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

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

  6. [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. 【luogu P2936 [USACO09JAN]全流Total Flow】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2936 菜 #include <queue> #include <cstdio> #i ...

随机推荐

  1. windows系统中搭建Jenkins服务器

    1       须知 100.126.36.232等Jenkins服务器是通过设置代理访问外网,管理Jenkins和插件升级站点的,本地安装受黄区网络限制需要特殊配置,且有些插件无法下载. 前提条件: ...

  2. vmware下centos克隆功能对网络的设置

    centos完成克隆功能后需要对网络进行设置 # cd /etc/udev/rules.d/ vim 70-persistent-net.rules 删除eth0的配置,将eth1该为eth0 编辑网 ...

  3. 解析神奇的 Object.defineProperty

    这个方法了不起啊..vue.js是通过它实现双向绑定的..而且Object.observe也被草案发起人撤回了..所以defineProperty更有必要了解一下了. 几行代码看他怎么用 var a= ...

  4. raindi python魔法函数(一)之__repr__与__str__

    __repr__和__str__都是python中的特殊方法,都是用来输出实例对象的,如果没有定义这两个方法在打印的时候只会输出实例所在的内存地址 这种方式的输出没有可读性,并不能直观的体现实例.py ...

  5. LeetCode(59):螺旋矩阵 II

    Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...

  6. PHP: POST Content-Length of xxx bytes exceeds the limit of 8388608 bytes

    用户上传了 4 个附件,每个小于 5M,但是总大小超过了 15 M. 在 Nginx 日志中找到了如下错误信息,还没有到 Laravel 日志那一层. 2018/08/13 10:14:38 [err ...

  7. cf789d 图论计数,自环闭环

    一开始没有思路,以为要判联通块. 其实不是判断联通块,而是判断边是否连在一起,没有连边的点可以忽略不计 /* 分情况讨论: 1.忽略自环,那么要取出两条相连的普通变作为只经过一次的边 2.一条自环,一 ...

  8. poj1511,线段树扫描线面积

    经典题,线段树扫描线其实类似区间更新,一般的做法是想象一根扫描线从上扫到下或者从左扫到右,本题的做法是从上扫到下 只要扫到了一根水平线,就将其更新到线段树对应区间中,区间和它的子区间是独立更新的 #i ...

  9. 步步为营-30-AES加密与解密

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. 《转》利用cxf实现webservice

    首先下载cxf包,目前最新的版本是apache-cxf-2.1.,下栽地址http://cxf.apache.org/download.html. 1. 首先新建一个web工程CxfService,倒 ...