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. C语言函数调用栈(一)

    程序的执行过程可看作连续的函数调用.当一个函数执行完毕时,程序要回到调用指令的下一条指令(紧接call指令)处继续执行.函数调用过程通常使用堆栈实现,每个用户态进程对应一个调用栈结构(call sta ...

  2. v4l2功能列表大全【转】

    一,功能参考 目录 V4L2 close() - 关闭一个V4L2设备 V4L2 ioctl() - 创建的V4L2设备 ioctl VIDIOC_CROPCAP - 视频裁剪和缩放功能信息 ioct ...

  3. Python3学习笔记18-访问限制

    在Class内部,可以有属性和方法,而外部代码可以通过直接调用实例变量的方法来操作数据,这样,就隐藏了内部的复杂逻辑. 但是,从Student类的定义来看,外部代码还是可以自由地修改一个实例的name ...

  4. python脚本发送邮件

    #!/usr/bin/python #_*_ coding:utf-8 _*_ from email.MIMEText import MIMEText from email.MIMEMultipart ...

  5. plsql developer如何自定义快捷键

    首选项 用户界面 编辑器 自动替换 选择替换文件,文件内容: sf=select * from df=delete from

  6. angular下载安装

    1.下载安装nodejs 官方地址:https://nodejs.org/en/download/   2.验证是否安装成功 node -v npm -v   公司内网需要设置代理 npm confi ...

  7. 注解图Annotation

    该图来源于 竹子-博客(.NET/Java/Linux/架构/管理/敏捷) http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html,感 ...

  8. 关于报错stale element reference: element is not attached to the page document处理

    1.现象 在执行脚本时,有时候引用一些元素对象会抛出如下异常 org.openqa.selenium.StaleElementReferenceException: stale element ref ...

  9. [工具/PC]计算机中丢失libiconv-2.dll,丢失libintl-8.dll,无法定位程序输入点libiconv于动态链接库libiconv-2.dll上问题解决方法

    CodeBlocks 1. 背景,为了学习C语言,在win系统上下载了codeBlock,先简单介绍下:Code::Blocks 是一个开放源码的全功能的跨平台C/C++集成开发环境. Code::B ...

  10. 【mysql】MySQLdb返回字典方法

    来源:http://blog.csdn.net/zgl_dm/article/details/8710371 默认mysqldb返回的是元组,这样对使用者不太友好,也不利于维护下面是解决方法 impo ...