#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. linux 条件判断式

    1.利用if ...then if [ 判断条件 ];then 指令 fi 实例一 Y/N: #!/bin/bash #Program: # This program shows "Hell ...

  2. 从零开始学习MXnet(二)之dataiter

    MXnet的设计结构是C++做后端运算,python.R等做前端来使用,这样既兼顾了效率,又让使用者方便了很多,完整的使用MXnet训练自己的数据集需要了解几个方面.今天我们先谈一谈Data iter ...

  3. HDU3605:Escape(状态压缩+最大流)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  4. BZOJ1191:超级英雄(二分图匹配)

    [HNOI2006]超级英雄Hero 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1191 Description: 现在电视台有一种节 ...

  5. nodejs 喜欢报cannot find module .....的简单解决方案

    在安装nodejs后使用命令npm install <package_name>一直喜欢报cannot find module........ 因为我之前在我的电脑上安装过nodejs,当 ...

  6. MySQL rpm 版本安装

     准备: [root@localhost moudles]# ls MySQL-client-5.6.36-1.linux_glibc2.5.x86_64.rpm MySQL-server-5.6.3 ...

  7. 前端面试:css预处理

    css预处理定义: 定义了一种新的语言,其基本思想是用一种专门编程语言,为css增加了一些编程的特性,将css作为目标生成文件,然后开发者就只要使用这种语言进行编码工作. 几种预处理语言 sass l ...

  8. flume高级组件及各种报错

    1,one source two channel 创建conf文件,内容如下: #定义agent名, source.channel.sink的名称 access.sources = r1 access ...

  9. [bzoj3884]上帝与集合的正确用法——欧拉函数

    题目大意 题解 出题人博客 代码 #include <bits/stdc++.h> using namespace std; const int M = 10001000; int phi ...

  10. [bzoj3597][scoi2014]方伯伯运椰子——分数规划,负环

    题解 目标就是 \[Maximize\ \lambda = \frac{X-Y}{k}\] 按照分数规划的一般规律, 构造: \[g(\lambda) = \lambda k + Y - X\] 由于 ...