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

5
A B 3
B C 3
C D 5
D Z 4
B Z 6

Sample Output

3

HINT

Source

【BZOJ】3396: [Usaco2009 Jan]Total flow 水流 (最大流)的更多相关文章

  1. bzoj 3396: [Usaco2009 Jan]Total flow 水流【最大流】

    最大流生动形象的板子,注意数组开大点 #include<iostream> #include<cstdio> #include<queue> #include< ...

  2. 3396: [Usaco2009 Jan]Total flow 水流

    3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 179  Solved: 73[Su ...

  3. BZOJ3396: [Usaco2009 Jan]Total flow 水流

    3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 45  Solved: 27[Sub ...

  4. 【网络流】【Dinic】【最大流】bzoj3396 [Usaco2009 Jan]Total flow 水流

    #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using ...

  5. bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分

    1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 665  Solved: 227[Sub ...

  6. 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)

    P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...

  7. [BZOJ 1576] [Usaco2009 Jan] 安全路经Travel 【树链剖分】

    题目链接: BZOJ - 1576 题目分析 首先Orz Hzwer的题解. 先使用 dijikstra 求出最短路径树. 那么对于一条不在最短路径树上的边 (u -> v, w) 我们可以先沿 ...

  8. 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的距离 ...

  9. bzoj 1576 [Usaco2009 Jan]安全路经Travel(树链剖分,线段树)

    [题意] 给定一个无向图,找到1-i所有的次短路经,要求与最短路径的最后一条边不重叠. [思路] 首先用dijkstra算法构造以1为根的最短路树. 将一条无向边看作两条有向边,考察一条不在最短路树上 ...

随机推荐

  1. 如何使用angularjs实现文本框设置值

    <!DOCTYPE html> <html ng-app="myApp"> <head> <title>angularjs-setV ...

  2. UVa145 Gondwanaland Telecom

    Time limit: 3.000 seconds 限时:3.000秒 Problem 问题 Gondwanaland Telecom makes charges for calls accordin ...

  3. 機器學習基石(Machine Learning Foundations) 机器学习基石 作业三 课后习题解答

    今天和大家分享coursera-NTU-機器學習基石(Machine Learning Foundations)-作业三的习题解答.笔者在做这些题目时遇到非常多困难,当我在网上寻找答案时却找不到,而林 ...

  4. mac 查看目前哪些进程占用哪些端口

    lsof -nP  | grep TCP | grep LISTEN lsof -i :TCP

  5. Hibernate映射set与List

    1.对于set类型,如果集合中的元素是简单地类型,如字符串型,set使用另外一种映射方式: team类: import java.util.HashSet; import java.util.Set; ...

  6. resin设置jvm参数

    http://www.quiee.com.cn/archives/592/ resin resin3.0 及前期版本内存设置, 如下: 启动时设置虚拟内存: unix> bin/httpd.sh ...

  7. SqlServer报错:System.Data.SqlClient.SqlException

    在将Asp.Net MVC4项目部署到新机器上进行调试的时候,出现了如下错误: System.Data.SqlClient.SqlException 具体的内容如下: System.Data.SqlC ...

  8. js根据对象的某一属性进行排序

    function compare(property) { return function (a, b) { var value1 = new Date(a[property]); var value2 ...

  9. unity, access standard shared emission by script

    unity 5.1.1f1 personal 用下面方法在脚本中设置standard shader的emssion: gameObject.GetComponent<MeshRenderer&g ...

  10. Eclipse+maven开发环境搭建

    版本描述: Eclipse 3.2.2 Maven 2.0.7 Jdk 1.5以上,本例是在jdk1.50版本测试通过 Maven配置过程 Maven官方下载地址:http://www.apache. ...