BZOJ3396: [Usaco2009 Jan]Total flow 水流
3396: [Usaco2009 Jan]Total flow 水流
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 45 Solved: 27
[Submit][Status]
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
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 200
#define maxm 1500
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,m,s,t,maxflow,tot=,head[maxn],cur[maxn],h[maxn],q[maxn];
struct edge{int go,next,v;}e[maxm];
void ins(int x,int y,int z){e[++tot].go=y;e[tot].v=z;e[tot].next=head[x];head[x]=tot;}
void insert(int x,int y,int z){ins(x,y,z);ins(y,x,);}
bool bfs()
{
for(int i=;i<=;i++)h[i]=-;
int l=,r=;q[]=s;h[s]=;
while(l<r)
{
int x=q[++l];
for(int i=head[x];i;i=e[i].next)
if(e[i].v&&h[e[i].go]==-)
{
h[e[i].go]=h[x]+;q[++r]=e[i].go;
}
}
return h[t]!=-;
}
int dfs(int x,int f)
{
if(x==t) return f;
int tmp,used=;
for(int i=head[x];i;i=e[i].next)
if(e[i].v&&h[e[i].go]==h[x]+)
{
tmp=dfs(e[i].go,min(e[i].v,f-used));
e[i].v-=tmp;if(e[i].v)cur[x]=i;
e[i^].v+=tmp;used+=tmp;
if(used==f)return f;
}
if(!used) h[x]=-;
return used;
}
void dinic()
{
maxflow=;
while(bfs())
{
for (int i=s;i<=t;i++)cur[i]=head[i];maxflow+=dfs(s,inf);
}
}
inline int get()
{
char ch=' ';
while((ch<'a'||ch>'z')&&(ch<'A'||ch>'Z'))ch=getchar();
return ch;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)
{
int x=get(),y=get(),z=read();
insert(x,y,z);
}
s=(int)'A';t=(int)'Z';
dinic();
printf("%d\n",maxflow);
return ;
}
BZOJ3396: [Usaco2009 Jan]Total flow 水流的更多相关文章
- 【网络流】【Dinic】【最大流】bzoj3396 [Usaco2009 Jan]Total flow 水流
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using ...
- 3396: [Usaco2009 Jan]Total flow 水流
3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 179 Solved: 73[Su ...
- 【BZOJ】3396: [Usaco2009 Jan]Total flow 水流 (最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=3396 裸的最大流跑过.. #include <cstdio> #include < ...
- bzoj 3396: [Usaco2009 Jan]Total flow 水流【最大流】
最大流生动形象的板子,注意数组开大点 #include<iostream> #include<cstdio> #include<queue> #include< ...
- bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MB Submit: 665 Solved: 227[Sub ...
- BZOJ3394: [Usaco2009 Jan]Best Spot 最佳牧场
3394: [Usaco2009 Jan]Best Spot 最佳牧场 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 11 Solved: 9[Sub ...
- 1574: [Usaco2009 Jan]地震损坏Damage
1574: [Usaco2009 Jan]地震损坏Damage Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 425 Solved: 232[Subm ...
- 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)
P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...
- 洛谷——P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
随机推荐
- EMV/PBOC 解析(一) 卡片文件结构
刚到公司老大便发我一份文档<智能卡ISO7816-4规范(中文版)>,然后就让我研究下IC智能卡数据读取和支付.身为一直做.NET开发的我对硬件啥的一无所知,各种无头绪啊,研究了两天后,稍 ...
- [C#技术] .NET平台开源JSON库LitJSON的使用方法
一个简单示例: String str = "{’name’:’cyf’,’id’:10,’items’:[{’itemid’:1001,’itemname’:’hello’},{’itemi ...
- 一起来写2048(160行python代码)
前言: Life is short ,you need python. --Bruce Eckel 我与2048的缘,不是缘于一个玩家,而是一次,一次,重新的ACM比赛.四月份校赛初赛,第一次碰到20 ...
- Android dp和px之间转换 及 获取坐标
dp.px.sp转换 public class DensityUtil { /** * 将px值转换为dip或dp值,保证尺寸大小不变 * * @param pxValue * @param scal ...
- linux crontab 定时命令
一直认为Timer是比较好的实现定时器的方法,后来遇到在linux下的命令制定定时任务才发现,Timer的劣势所在,在Timer的时候很可能你的任务会被当做一个死程序被杀掉等等......上次一个同事 ...
- tomcat7源代码Bootstrap
tomcat的启动从bootstrap的main方法開始,在main方法中主要是做了三件事,调用init方法初始化自己.调用catalinaDaemon对象 的setAwait方法设置它的await属 ...
- [转] Python正则表达式指南
本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例.本文的内容不包括如何编写高效的正则表达式.如何优化正则表达式,这些主题请查看其他教程 ...
- javascript基础之for循环
1.数组定义声名 var arry = [1,2,3,4,5] //相当与var arry = Array(1,2,3,4,5) 2.数据的增删改查 var arry = [1,2,3,4,5] ...
- php的mq客户端获取队列方法改造
获取mq中消息然后处理失败重试机制: 下面的代码是php连接mq客户端的获取queue队列中的消息代码: public function createDurableSubscriber($queue, ...
- (转)SQL流程控制语句学习(一):变量及控制语句种类
1.局部变量 用户自己定义的,称局部变量,以@标识. 作用范围:定义局部变量的批处理.存储过程.触发器和语句块 局部变量的定义: declare @局部变量名 数据类型 注意:变量的类型不能是text ...