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 #include #include 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 #include #include 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 #include #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 (henod]==-1)&&(p->val)) { que[ta++]=p->nod; low=lowval?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 #include #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 (headg[path[i]][path[i+1]]) {low=g[path[i]][path[i+1]];cut=i;} maxflow+=low; for (i=0;i #include #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 (heval)&&(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;inod!=path[i+1]) p=p->next; if (p->valval;cut=i;} } maxflow+=low; for (i=0;inod!=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; }

你可能感兴趣的:(解题报告,图论)