HDU1532 最大流 Ford-Fulkerson(邻接矩阵/邻接表) Dinic(邻接矩阵/邻接表)

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1507    Accepted Submission(s): 701

Problem Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.

 

 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

 

 

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

 

 

Sample Input

5 4

1 2 40

1 4 20

2 4 20

2 3 30

3 4 10

 

 

Sample Output

50

 

 

此题纯粹是最大流的模板题 , 给最大流的初学者练习的题 .

本人写了 Ford-Fulkerson 邻接矩阵版本的 BFS DFS 的模板和 Ford-Fulkerson 邻接表版本的 BFS 模板 , 还有 Dinic 的邻接矩阵和邻接表模板 , 5 个模板解此题 , 以下为 5 份模板的 AC 代码 :

//Ford-Fulkerson //邻接矩阵BFS #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define MAXN 205 #define inf 2100000000 int c[MAXN][MAXN]; int pass[MAXN]; int bfs_max_flow(int n,int s,int t) { int pre[MAXN],low[MAXN],head,tail,que[1000],i,maxflow=0; while (1) { memset(pre,-1,sizeof(pre)); head=tail=0; low[s]=inf;que[tail++]=s; pre[s]=0; while (head<tail) { int x=que[head++]; for (i=1;i<=n;++i) if ((c[x][i])&&(pre[i]==-1)) { que[tail++]=i; low[i]=low[x]<c[x][i]?low[x]:c[x][i]; pre[i]=x; } if (pre[t]!=-1) { x=t; while (x!=s) { c[x][pre[x]]+=low[t]; c[pre[x]][x]-=low[t]; x=pre[x]; } break; } } if (pre[t]!=-1) maxflow+=low[t]; else return maxflow; } } int main() { int n,m,i,a,b,d; while (scanf("%d%d",&n,&m)!=EOF) { memset(c,0,sizeof(c)); for (i=1;i<=n;++i) { scanf("%d%d%d",&a,&b,&d); c[a][b]+=d; } printf("%d/n",bfs_max_flow(m,1,m)); } } //Ford-Fulkerson //邻接矩阵DFS #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define MAXN 205 #define inf 2100000000 int c[MAXN][MAXN]; int pass[MAXN]; int dfs(int n,int s,int t,int low) { int i,flow; if (s==t) return low; if (pass[s]) return 0; pass[s]=1; for (i=1;i<=n;++i) { if ((c[s][i])&&(flow=dfs(n,i,t,low<c[s][i]?low:c[s][i]))) { c[s][i]-=flow; c[i][s]+=flow; return flow; } } return 0; } int dfs_max_flow(int n,int s,int t) { memset(pass,0,sizeof(pass)); int maxflow=0,flow; while (flow=dfs(n,s,t,inf)) { memset(pass,0,sizeof(pass)); maxflow+=flow; } return maxflow; } int main() { int n,m,i,a,b,d; while (scanf("%d%d",&n,&m)!=EOF) { memset(c,0,sizeof(c)); for (i=1;i<=n;++i) { scanf("%d%d%d",&a,&b,&d); c[a][b]+=d; } printf("%d/n",dfs_max_flow(m,1,m)); } } //Ford-Fulkerson //邻接表BFS #include<stdio.h> #include<string.h> #define MAXN 205 #define inf 2100000000 struct link { int nod,val; link *next; }head[MAXN]; int bfs_max_flow(int n,int s,int t) { int que[1000],he,ta,maxflow=0,low,pre[MAXN];link *p; while (1) { memset(pre,-1,sizeof(pre)); he=ta=0;low=inf; que[ta++]=s; while (he<ta) { int x=que[he++]; p=head[x].next; while (p!=NULL) { if ((pre[p->nod]==-1)&&(p->val)) { que[ta++]=p->nod; low=low<p->val?low:p->val; pre[p->nod]=x; } p=p->next; } if (pre[t]!=-1) { x=t; while (x!=s) { p=head[pre[x]].next; while (p->nod!=x) p=p->next; p->val-=low; x=pre[x]; } break; } } if (pre[t]!=-1) maxflow+=low; else return maxflow; } } int main() { link *p,*s;int n,m,i,a,b,d,find; while (scanf("%d%d",&n,&m)!=EOF) { for (i=1;i<=m;++i) {head[i].nod=head[i].val=0;head[i].next=NULL;} for (i=1;i<=n;++i) { scanf("%d%d%d",&a,&b,&d); if (head[a].next!=NULL) { find=0; p=head[a].next; do { if (p->nod==b) {p->val+=d;find=1;break;} p=p->next; }while (p!=NULL); if (!find) { s=new link; s->nod=b;s->val=d; s->next=head[a].next; head[a].next=s; } } else { s=new link; s->nod=b;s->val=d; s->next=NULL; head[a].next=s; } } printf("%d/n",bfs_max_flow(m,1,m)); } } //Dinic //邻接矩阵 #include<stdio.h> #include<string.h> #define MAXN 205 #define oo 2100000000 int g[MAXN][MAXN],level[MAXN]; int bfs(int n,int s,int t) { int que[1000],head,tail,i; head=tail=0; que[tail++]=s; memset(level,-1,sizeof(level)); level[s]=1; while (head<tail) { int x=que[head++]; for (i=1;i<=n;++i) if ((g[x][i])&&(level[i]==-1)) { level[i]=level[x]+1; que[tail++]=i; } } return level[t]+1; } int dinic(int n,int s,int t) { int maxflow=0,i; while (bfs(n,s,t)) { int pos,path[MAXN],cut=0,low,find; pos=0; path[pos++]=s;find=1; while (1) { find=1;low=oo; while ((path[pos-1]!=t)&&(find)) { find=0; for (i=1;i<=n;++i) if ((level[i]==level[path[pos-1]]+1)&&(g[path[pos-1]][i])) { path[pos++]=i; find=1; break; } else if (i==n) break; } if (path[pos-1]==t) { for (i=0;i<pos-1;++i) if (low>g[path[i]][path[i+1]]) {low=g[path[i]][path[i+1]];cut=i;} maxflow+=low; for (i=0;i<pos-1;++i) { g[path[i]][path[i+1]]-=low; g[path[i+1]][path[i]]+=low; } pos=cut+1; } else { if (pos==1) break; else {level[path[pos-1]]=-1;pos--;} } } } return maxflow; } int main() { int n,m,i,a,b,d; while (scanf("%d%d",&n,&m)!=EOF) { memset(g,0,sizeof(g)); for (i=1;i<=n;++i) { scanf("%d%d%d",&a,&b,&d); g[a][b]+=d; } printf("%d/n",dinic(m,1,m)); } return 0; } //Dinic //邻接表 #include<stdio.h> #include<string.h> #define MAXN 205 #define oo 2100000000 struct link { int nod,val; link *next; }head[MAXN]; int level[MAXN],g[MAXN][MAXN]; int bfs(int n,int s,int t) { int que[MAXN],he,ta,v; link *p; he=ta=0; que[ta++]=s; memset(level,-1,sizeof(level)); level[s]=0; while (he<ta) { v=que[he++]; p=head[v].next; while (p!=NULL) { if ((p->val)&&(level[p->nod]==-1)) { level[p->nod]=level[v]+1; que[ta++]=p->nod; } p=p->next; } } return level[t]+1; } int dinic(int n,int s,int t) { int path[MAXN],pos,low,i,maxflow=0,cut; link *p; while (bfs(n,s,t)) { pos=0;cut=0; path[pos++]=s; while (1) { p=head[path[pos-1]].next; while ((p!=NULL)&&(path[pos-1]!=t)) { if ((p->val)&&(level[p->nod]==level[path[pos-1]]+1)) { path[pos++]=p->nod; p=head[path[pos-1]].next; continue; } else if (p->next==NULL) break; if (p!=NULL) p=p->next; } if (path[pos-1]==t) { low=oo; for (i=0;i<pos-1;++i) { p=head[path[i]].next; while (p->nod!=path[i+1]) p=p->next; if (p->val<low) {low=p->val;cut=i;} } maxflow+=low; for (i=0;i<pos-1;++i) { p=head[path[i]].next; while (p->nod!=path[i+1]) p=p->next; p->val-=low; } pos=cut+1; } else if (pos==1) break; else {level[path[pos-1]]=-1;pos--;} } } return maxflow; } int main() { link *p,*s;int m,n,a,b,d;int i; while (scanf("%d%d",&n,&m)!=EOF) { for (i=1;i<=n;++i) head[i].next=NULL; for (i=1;i<=n;++i) { scanf("%d%d%d",&a,&b,&d); if (head[a].next!=NULL) { p=head[a].next; while (p!=NULL) { if (p->nod==b) { p->val+=d; break; } p=p->next; } if (p==NULL) { s=new link; s->nod=b; s->val=d; s->next=head[a].next; head[a].next=s; } } else { s=new link; s->nod=b; s->val=d; s->next=NULL; head[a].next=s; } } printf("%d/n",dinic(m,1,m)); } return 0; }

你可能感兴趣的:(struct,null,Integer,Path,each,output)