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 < ...
随机推荐
- (转)Maven实战(五)坐标详解
1.为什么要定义Maven坐标 在我们开发Maven项目的时候,需要为其定义适当的坐标,这是Maven强制要求的.在这个基础上,其他Maven项目才能应用该项目生成的构件. 2.Maven坐 ...
- 字符串转化为json的三种方法
1,eval方式解析,恐怕这是最早的解析方式了.如下: function strToJson(str){ var json = eval('(' + str + ')'); return json; ...
- Error prompt:“xxx is not in the sudoers file”----Solution
//Situation System prompts "xxx is not in the sudoers file"(xxx equals the user name) w ...
- mockServer学习
mockServer学习 很喜欢mockserver官方主页的背景颜色和格式 官方主页如下: http://www.mock-server.com/
- Examples_08_04
- 单点登录CAS使用记(三):实现自定义验证用户登录
问题: CAS自带的用户验证逻辑太过简单,如何像正常网站一样,通过验证DB中的用户数据,来验证用户以及密码的合法性呢? 方案1:CAS默认的JDBC扩展方案: CAS自带了两种简单的通过JDBC方式验 ...
- 【转】解读Qt 事件处理机制(上篇)
[转自]:http://mobile.51cto.com/symbian-272812.htm 在Qt中,事件被封装成一个个对象,所有的事件均继承自抽象类QEvent. 接下来依次谈谈Qt中有谁来产生 ...
- golang byte转string 字节数组转字符串的问题
golang语言本身就是c的工具集,开发c的程序用到的大部分结构体,内存管理,携程等,golang基本都有,他只是在这个基础上又加了一些概念这里说一个很小的问题,就是字节数组转string的问题,网上 ...
- javascript将form表单中的元素值封装成json格式
把form表单中的元素值封装成json function toJSON(form)//form是要进行封装的form表单对象,dom对象,可以通过document.mainForm获得,mainFor ...
- java Socket 长连接 心跳包 客户端 信息收发 demo
今天写了个socket的测试小程序,代码如下 import java.io.IOException; import java.io.InputStream; import java.io.Output ...