#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 2147483647
#define MAXN 201
#define MAXM 1501
int v[MAXM],cap[MAXM],en,first[MAXN],next[MAXM];
int d[MAXN],cur[MAXN];
queue<int>q;
int m,S,T,W;
char U[],V[];
void Init_Dinic(){memset(first,-,sizeof(first)); en=; S='A'; T='Z';}
void AddEdge(const int &U,const int &V,const int &W)
{v[en]=V; cap[en]=W; next[en]=first[U]; first[U]=en++;
v[en]=U; next[en]=first[V]; first[V]=en++;}
bool bfs()
{
memset(d,-,sizeof(d)); q.push(S); d[S]=;
while(!q.empty())
{
int U=q.front(); q.pop();
for(int i=first[U];i!=-;i=next[i])
if(d[v[i]]==- && cap[i])
{
d[v[i]]=d[U]+;
q.push(v[i]);
}
}
return d[T]!=-;
}
int dfs(int U,int a)
{
if(U==T || !a) return a;
int Flow=,f;
for(int &i=cur[U];i!=-;i=next[i])
if(d[U]+==d[v[i]] && (f=dfs(v[i],min(a,cap[i]))))
{
cap[i]-=f; cap[i^]+=f;
Flow+=f; a-=f; if(!a) break;
}
if(!Flow) d[U]=-;
return Flow;
}
int max_flow()
{
int Flow=,tmp=;
while(bfs())
{
memcpy(cur,first,sizeof(cur));
while(tmp=dfs(S,INF)) Flow+=tmp;
}
return Flow;
}
int main()
{
scanf("%d",&m); Init_Dinic();
for(int i=;i<=m;++i)
{
scanf("%s%s%d",U,V,&W);
AddEdge(U[],V[],W);
}
printf("%d\n",max_flow());
return ;
}

【网络流】【Dinic】【最大流】bzoj3396 [Usaco2009 Jan]Total flow 水流的更多相关文章

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

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

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

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

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

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

  4. 【BZOJ】3396: [Usaco2009 Jan]Total flow 水流 (最大流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3396 裸的最大流跑过.. #include <cstdio> #include < ...

  5. 网络流之最大流Dinic算法模版

    /* 网络流之最大流Dinic算法模版 */ #include <cstring> #include <cstdio> #include <queue> using ...

  6. 网络流之最大流Dinic --- poj 1459

    题目链接 Description A power network consists of nodes (power stations, consumers and dispatchers) conne ...

  7. UVA 10480 Sabotage (网络流,最大流,最小割)

    UVA 10480 Sabotage (网络流,最大流,最小割) Description The regime of a small but wealthy dictatorship has been ...

  8. [USACO09JAN]全流Total Flow

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

  9. 【Luogu2711】小行星(网络流,最大流)

    [Luogu2711]小行星(网络流,最大流) 题面 题目描述 星云中有n颗行星,每颗行星的位置是(x,y,z).每次可以消除一个面(即x,y或z坐标相等)的行星,但是由于时间有限,求消除这些行星的最 ...

随机推荐

  1. [bzoj 2818]欧拉函数

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818 枚举最大公约数,对于每一个质数p,只需要求出1<=x,y<=(n/p)范 ...

  2. [poj 3261]后缀数组+滑窗最小值

    题目链接:http://poj.org/problem?id=3261 这个是可以交叉的重复串,所以用height就可以了,但是题目说让重复k次以上,也就是直接做一个k-1长度的滑窗最小值,从这些最小 ...

  3. HDU 1698 Just a Hook(线段树

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. POJ 1050 To the Max 二维最大子段和

    To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...

  5. LwIP - 打开keepalive功能

    在服务器端打开keepalive功能 1.保证LWIP_TCP_KEEPALIVE被定义为1,(这样TCP_KEEPIDLE.TCP_KEEPINTVL和TCP_KEEPCNT 设置才有效) 2. i ...

  6. php魔术方法的使用

    本文测试环境为 php5.5.12 一.__get .__set 将对象的属性进行接管. 一般来说,总是把类的属性定义为private,但是对属性的读取和赋值操作非常频繁,在php5+,预定义__se ...

  7. canvas知识02:图片放大镜效果

    效果截图: JS代码: <script> // 初始化canvas01和上下文环境 var cav01 = document.getElementById('cav01'); var cx ...

  8. [洛谷P1040] 加分二叉树

    洛谷题目链接:加分二叉树 题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,-,n),其中数字1,2,3,-,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di ...

  9. 关于flume的几道题

    1,要求:监听一个tcp,udp端口41414将数据打印在控制台 # example.conf: A single-node Flume configuration # Name the compon ...

  10. 优化IDEA启动速度,快了好多。后面有什么优化点,会继续往里面添加

    1.优化启动 修改bin/idea.exe.vmoptions文件如下: -Xms256m   初始堆大小-Xmx384m   最大堆大小 -XX:+UseParNewGC   使用并行收集算法 2. ...