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. ocx注册

    (1)服务器OCX注册 (2)IE浏览器,站点加入可信任站点. internet 选项->安全->可信任站点.把“对该区域中的所有站点要求服务器验证(https:)” 前面的勾去掉 (3) ...

  2. python学习准备阶段(环境配置)python解释器的选择

    pycharm提示No Python interpreter configgured for the project ####### 1 点击configure python interpreter ...

  3. 利用JMX来监控大部分java应用

    JMX(JavaManagement Extensions,即Java管理扩展)是一个为应用程序.设备.系统等植入管理功能的框架.JMX可以跨越一系列异构操作系统平台.系统体系结构和网络传输协议,灵活 ...

  4. CSS3中伪类nth-child和nth-of-type区别

    本篇文章由:http://xinpure.com/css3-pseudo-class-difference-between-nthchild-and-nthoftype/ 首先来看看 nth-chil ...

  5. centos 基础环境配置

    1,安装EPEL的yum源 EPEL 是 Extra Packages for Enterprise Linux 的缩写(EPEL),是用于 Fedora-based Red Hat Enterpri ...

  6. Ubuntu 安装配置 JDK+Tomcat+Nginx

    安装配置JDK 下载安装 # 下载: wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=acc ...

  7. python导入模块的两种方式

    第一种 from support import * 这种方式导入后可以直接调用(有命名冲突问题)命名冲突后定义的覆盖前定义的 如果在函数导入前定义 则导入函数覆盖 否则相反 if __name__ = ...

  8. Android开发——跟随手指的小球实现

      今天要实现的是一个跟随手指的小球,说白了就是让小球按着手指滑动的轨迹运动,实现起来还是比较容易的. 用到的类是drawView,我们先自定义一个DrawView组件. DrawView.java: ...

  9. Java生成随机不反复推广码邀请码

    欢迎进入我的博客:blog.scarlettbai.com查看很多其它文章 近期接到一个需求.要批量生成推广码,首先我们知道推广码的特效有例如以下两点: 1:不可反复 2:不能够被猜測出 关于这两点, ...

  10. .net SQL分页

    1.分页SQL declare @pagesize integer,@cpage integer; --变量定义 ; --页码大小 ; --当前页 ---@cpage 为 第一页的时候 --selec ...