【网络流】【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],cur[MAXN];
11 queue<int>q;
12 int m,S,T,W;
13 char U[1],V[1];
14 void Init_Dinic(){memset(first,-1,sizeof(first)); en=0; S=‘A‘; T=‘Z‘;}
15 void AddEdge(const int &U,const int &V,const int &W)
16 {v[en]=V; cap[en]=W; next[en]=first[U]; first[U]=en++;
17 v[en]=U; next[en]=first[V]; first[V]=en++;}
18 bool bfs()
19 {
20     memset(d,-1,sizeof(d)); q.push(S); d[S]=0;
21     while(!q.empty())
22       {
23         int U=q.front(); q.pop();
24         for(int i=first[U];i!=-1;i=next[i])
25           if(d[v[i]]==-1 && cap[i])
26             {
27               d[v[i]]=d[U]+1;
28               q.push(v[i]);
29             }
30       }
31     return d[T]!=-1;
32 }
33 int dfs(int U,int a)
34 {
35     if(U==T || !a) return a;
36     int Flow=0,f;
37     for(int &i=cur[U];i!=-1;i=next[i])
38       if(d[U]+1==d[v[i]] && (f=dfs(v[i],min(a,cap[i]))))
39         {
40           cap[i]-=f; cap[i^1]+=f;
41           Flow+=f; a-=f; if(!a) break;
42         }
43     if(!Flow) d[U]=-1;
44     return Flow;
45 }
46 int max_flow()
47 {
48     int Flow=0,tmp=0;
49     while(bfs())
50       {
51         memcpy(cur,first,sizeof(cur));
52         while(tmp=dfs(S,INF)) Flow+=tmp;
53       }
54     return Flow;
55 }
56 int main()
57 {
58     scanf("%d",&m); Init_Dinic();
59     for(int i=1;i<=m;++i)
60       {
61           scanf("%s%s%d",U,V,&W);
62           AddEdge(U[0],V[0],W);
63       }
64     printf("%d\n",max_flow());
65     return 0;
66 }
时间: 2024-11-03 09:10:28

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

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 #de

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

hdu 3572 Task Schedule(网络流 dinic算法)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412    Accepted Submission(s): 1197 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

洛谷 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

hdu2732 Leapin&#39; Lizards (网络流dinic)

D - Leapin' Lizards Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasur

poj 1149 PIGS(网络流dinic)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16054   Accepted: 7185 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

洛谷——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