【BZOJ】3396: [Usaco2009 Jan]Total flow 水流 (最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=3396
裸的最大流跑过。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=105, Q=N*100, oo=~0u>>1;
int ihead[N+10], cnt=1, n, gap[N+10], d[N+10], cur[N+10], p[N+10];
struct ED { int from, to, next, cap; }e[Q];
void add(int u, int v, int c) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].from=u; e[cnt].cap=c;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].from=v; e[cnt].cap=0;
}
int get() {
int t=0;
char ch=getchar(); while(!(ch>='A'&&ch<='Z')&&!(ch>='a'&&ch<='z')) ch=getchar();
if(ch>='A'&&ch<='Z') t=27+ch-'A';
else t=ch-'a'+1;
return t;
} int isap() {
for1(i, 1, N) cur[i]=ihead[i];
gap[0]=N;
int s=27, t=27+'Z'-'A', u=s, i, flow=0, f;
while(d[s]<N) {
for(i=cur[u]; i; i=e[i].next) if(d[u]==d[e[i].to]+1 && e[i].cap) break;
if(i) {
p[e[i].to]=i; cur[u]=i; u=e[i].to;
if(u==t) {
for(f=oo; u!=s; u=e[p[u]].from) f=min(f, e[p[u]].cap);
for(u=t; u!=s; u=e[p[u]].from) e[p[u]].cap-=f, e[p[u]^1].cap+=f;
flow+=f;
}
}
else {
if(!(--gap[d[u]])) break;
d[u]=N;
cur[u]=ihead[u];
for(i=ihead[u]; i; i=e[i].next) if(e[i].cap && d[u]>d[e[i].to]+1) d[u]=d[e[i].to]+1;
++gap[d[u]];
if(u!=s) u=e[p[u]].from;
}
}
return flow;
} int main() {
read(n);
for1(i, 1, n) {
int u=get(), v=get(), c=getint();
add(u, v, c);
}
int ans=isap();
if(ans==oo) puts("-1");
else print(ans);
return 0;
}
Description
Input
第1行输入N,之后N行每行描述一条水管,前两个英文字母表示水管的两端(大小写字母是不一样的),后一个整数表示水管的流量,流量不会超过1000.
Output
一个整数,表示总流量.
Sample Input
A B 3
B C 3
C D 5
D Z 4
B Z 6
Sample Output
HINT
Source
【BZOJ】3396: [Usaco2009 Jan]Total flow 水流 (最大流)的更多相关文章
- bzoj 3396: [Usaco2009 Jan]Total flow 水流【最大流】
最大流生动形象的板子,注意数组开大点 #include<iostream> #include<cstdio> #include<queue> #include< ...
- 3396: [Usaco2009 Jan]Total flow 水流
3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 179 Solved: 73[Su ...
- BZOJ3396: [Usaco2009 Jan]Total flow 水流
3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 45 Solved: 27[Sub ...
- 【网络流】【Dinic】【最大流】bzoj3396 [Usaco2009 Jan]Total flow 水流
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using ...
- bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MB Submit: 665 Solved: 227[Sub ...
- 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)
P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...
- [BZOJ 1576] [Usaco2009 Jan] 安全路经Travel 【树链剖分】
题目链接: BZOJ - 1576 题目分析 首先Orz Hzwer的题解. 先使用 dijikstra 求出最短路径树. 那么对于一条不在最短路径树上的边 (u -> v, w) 我们可以先沿 ...
- BZOJ.1576.[Usaco2009 Jan]安全路经Travel(树形DP 并查集)
题目链接 BZOJ 洛谷 先求最短路树.考虑每一条非树边(u,v,len),设w=LCA(u,v),这条边会对w->v上的点x(x!=w)有dis[u]+dis[v]-dis[x]+len的距离 ...
- bzoj 1576 [Usaco2009 Jan]安全路经Travel(树链剖分,线段树)
[题意] 给定一个无向图,找到1-i所有的次短路经,要求与最短路径的最后一条边不重叠. [思路] 首先用dijkstra算法构造以1为根的最短路树. 将一条无向边看作两条有向边,考察一条不在最短路树上 ...
随机推荐
- Android TabHost控件 右侧留空并增加按钮
涉及公司内部程序,部分地方进行模糊处理. 公司Android程序的一个子程序UI要进行改版,最初的UI添加按钮是在内容区,而且TabHost空间是正常的标题平均分布.如下图(其实这是改版的第一版,没有 ...
- FastDFS安装配置过程中出现错误提示"/home/yuqing/fastdfs" can't be accessed, error info: No such file or directory
解决: mkdir -p /home/yuqing/fastdfs 这回正常了,查看启动成功与否 [root@localhost FastDHT]# ps -ef |grep track |grep ...
- 有些类库(node.js版)
做项目经常会用到好些类库,大的还好说,用的多了自然记住了名字.如express. 但也有些小而精湛的类库,不仅提供了良好的功能,本身的实现也值得研究.暂记于此. 1.web类 request 简 ...
- JMeter 二:执行顺序 & 支持的协议
执行顺序 参考:http://jmeter.apache.org/usermanual/test_plan.html#executionorder 不同种类元素之间,执行顺序如下: Configura ...
- vue - utils for extract-text-webpack-plugin
描述:将包或包中的文本提取到单独的文件中, 点击查看官网详情: https://www.npmjs.com/package/extract-text-webpack-plugin
- 《The Story of My Life》Introductiom - The Life and Work of Helen Keller
Helen Keller was born on June 27,1880, in Tuscumabia, Alabama, to Captain Arthur Henry Keller, a Con ...
- win10下Visual Studio 2015,C++ x64编译zmq
PS.本人编译过程踩得坑,记录备忘 下载:(1)官网:http://zeromq.org/intro:get-the-software,有简明的编译方式,cmake的,这里不多赘述 (2)到GitHu ...
- ROC
# -*- coding: utf-8 -*- # __author__ = "JieYao" from biocluster.agent import Agent from bi ...
- 【MySQL】设置字符集UTF-8(解决)
1.检查你需要的字符集,GBK或者UTF8 2.数据库是否是UTF8, 3.数据表是否是UTF8 4.模板是否设置了UTF-8 以上都没设置错误,那么继续往下看: 进入目标数据库,使用语句: SHOW ...
- jrebel 7免费激活(非破解) 和 IntelliJ Idea 2017 免费激活方法
转自http://www.cnblogs.com/suiyueqiannian/p/6734412.html http://www.cnblogs.com/suiyueqiannian/p/67540 ...