bzoj3396[Usaco2009 Jan]Total flow 水流*

bzoj3396[Usaco2009 Jan]Total flow 水流

题意:

求无环图的最大流。边数≤700。

题解:

管它有没有环。注意本题的节点标号既有大写字母,也有小写字母。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 #define maxn 100
 7 #define INF 0x3fffffff
 8 using namespace std;
 9
10 struct e{int t,c,n;}es[maxn*80]; int g[maxn],ess;
11 void pe(int f,int t,int c){
12     es[++ess]=(e){t,c,g[f]}; g[f]=ess; es[++ess]=(e){f,0,g[t]}; g[t]=ess;
13     es[++ess]=(e){f,c,g[t]}; g[t]=ess; es[++ess]=(e){t,0,g[f]}; g[f]=ess;
14 }
15 queue<int>q; int h[maxn];
16 bool bfs(int s,int t){
17     while(!q.empty())q.pop(); memset(h,-1,sizeof(h)); q.push(s); h[s]=0;
18     while(!q.empty()){
19         int x=q.front(); q.pop();
20         for(int i=g[x];i;i=es[i].n)if(es[i].c&&h[es[i].t]==-1){h[es[i].t]=h[x]+1; q.push(es[i].t);}
21     }
22     return h[t]!=-1;
23 }
24 int dfs(int x,int t,int f){
25     if(x==t)return f; int u=0;
26     for(int i=g[x];i;i=es[i].n)if(es[i].c&&h[es[i].t]==h[x]+1){
27         int w=dfs(es[i].t,t,min(f,es[i].c)); f-=w; u+=w;
28         es[i].c-=w; es[i^1].c+=w; if(f==0)return u;
29     }
30     if(u==0)h[x]=-1; return u;
31 }
32 int dinic(int s,int t){
33     int f=0; while(bfs(s,t))f+=dfs(s,t,INF); return f;
34 }
35 int s,t,n;
36 int main(){
37     scanf("%d",&n); ess=1; s=0; t=‘Z‘-‘A‘;
38     inc(i,1,n){char x[3],y[3]; int z; scanf("%s%s%d",x,y,&z); pe(x[0]-‘A‘,y[0]-‘A‘,z);}
39     printf("%d",dinic(s,t)); return 0;
40 }

20160908

时间: 2024-10-14 03:13:36

bzoj3396[Usaco2009 Jan]Total flow 水流*的相关文章

【网络流】【Dinic】【最大流】bzoj3396 [Usaco2009 Jan]Total flow 水流

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 #define INF 2147483647 7 #define MAXN 201 8 #define MAXM 1501 9 int v[MAXM],cap[MAXM],en,first[MAXN],next[MAXM]; 10 int d[MAXN]

3396: [Usaco2009 Jan]Total flow 水流

3396: [Usaco2009 Jan]Total flow 水流 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 179  Solved: 73[Submit][Status] Description Input 第1行输入N,之后N行每行描述一条水管,前两个英文字母表示水管的两端(大小写字母是不一样的),后一个整数表示水管的流量,流量不会超过1000. Output 一个整数,表示总流量. Sample Input 5 A B 3 B C 3

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

最大流生动形象的板子,注意数组开大点 #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; const int N=100,inf=1e9; int n=26,m,h[N],cnt=1,s=1,t=26,le[N],x; char s1[5],s2[5]; struct qwe { int ne,to,va; }e[N*N*2];

[USACO09JAN]全流Total Flow

题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an appa

洛谷 P2936 [USACO09JAN]全流Total Flow

题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an appa

BZOJ3394: [Usaco2009 Jan]Best Spot 最佳牧场

3394: [Usaco2009 Jan]Best Spot 最佳牧场 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 11  Solved: 9[Submit][Status] Description Input 第1行输入三个整数P,F C.之后F行每行输入一个整数表示一个贝茜喜欢的牧场.之后C行每行输入三个整数ai,bi,Ti,描述一条路. Output 一个整数,满足题目要求的最佳牧场.如果有多个答案,输出编号最小的 Sample Input

bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分

1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 665  Solved: 227[Submit][Status] Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数:从牛棚_1到牛棚_i+1并且避免从牛棚1到牛棚i+1最短路经上最后一条牛

【BZOJ 1576】 [Usaco2009 Jan]安全路经Travel

1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 676  Solved: 231 [Submit][Status] Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数:从牛棚_1到牛棚_i+1并且避免从牛棚1到牛棚i+1最短路经上最后一条

解题报告 之 SOJ3353 Total Flow

解题报告 之 SOJ3353 Total Flow Description Time Limit: 2000 MS Memory Limit: 65536 K The Problem PROBLEM NAME: flow Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that conne