Drainage ditches
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 58538 |
|
Accepted: 22485 |
Description
Every time it rains on Farmer John's fields, a pond forms over Bessie ' s favorite Clover patch. This means, the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John had built a set of drainage ditches so that Bessie ' s clover Patch was never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John have also installed regulators at the beginning of all ditch, so he can control at what Rate water flows to that ditch.
Farmer John knows not only what many gallons of water each ditch can transport per minute but also the exact layout of the Ditches, which feed out of the the pond and to each other and stream in a potentially complex network.
Given All this information, determine the maximum in which water can be transported off of the pond and into the stre Am. For any given ditch, water flows on only one direction, but there might is a-a-to-a-water can flow in a circle.
Input
The input includes several cases.For each case, the first line contains the space-separated integers, n (0 <= n <=) and M (2 <= M <= 200). N is the number of ditches this 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 would flow through this ditch from Si to Ei. CI (0 <= ci <= 10,000,000) is the maximum rate at which water would flow through the ditch.
Output
For each case, output a single integer and the maximum rate at which water may emptied from the pond.
Sample Input
5 41 2 401 4 202 4 202 3 303 4 10
Sample Output
50
Test instructions: There are N roads, m points, let you find out that 1 is the source point, M is the maximum flow of the sink point;
Idea: A typical algorithm for maximum flow-grace paths. The flow at this time is the maximum flow from S to T, when and only if there is no s-t in the residual network (augmented path);
ISAP ()
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include < iostream> #include <queue> #include <set>using namespace Std;const int inf=0x3f3f3f3f;// Head stores arcs with the same starting point in front of the star, Num stores the number of remaining nodes in each layer, D stores the number of layers for each node, cur stores the current arc, the pre storage path, which is the previous node of the current node, for backtracking. int head[1010],num[1010],d[1010],cur[1010],n,m,cnt,s,t,q[100010],nv,pre[1010];int maxint=inf;struct node{int V,cap ; int next;} edge[10010];void Add (int u,int v,int cap) {//home Plus to arc edge[cnt].v=v; Edge[cnt].cap=cap; Edge[cnt].next=head[u]; head[u]=cnt++; Plus reverse arc edge[cnt].v=u; Edge[cnt].cap=0; EDGE[CNT].NEXT=HEAD[V]; head[v]=cnt++;} void BFs ()//using BFS to layer each node {//The algorithm executes the D array with BFS prior to executing the int i,j from T to S; memset (num,0,sizeof (num)); Memset (d,-1,sizeof (d)); int f1=0,f2=0; q[f1++]=t;//from D[t]=0; Num[0]=1; while (F2<=F1) {int u=q[f2++]; for (i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v; if (D[V]!=-1) CONtinue; d[v]=d[u]+1;//the number of layers of this node is the next layer of the previous node num[d[v]]++;//the number of nodes in that layer +1 q[f1++]=v; }}}void Isap () {memcpy (cur,head,sizeof (cur)); BFS (); int flow=0,u=pre[s]=s,i; while (D[S]<NV) {//if d[s] is greater than NV, it is indicated that there is a fault if (u==t) {int f=maxint,pos; for (I=S;I!=T;I=EDGE[CUR[I]].V) {//Find minimum residual traffic on the road if (f>edge[cur[i]].cap) {f=edge[cur[i]].c Ap Pos=i; }} for (I=S;I!=T;I=EDGE[CUR[I]].V) {//update, will all traffic on that road-least participate in traffic edge[cur[i]].cap-=f; Edge[cur[i]^1].cap+=f; } flow+=f;//joins the total traffic u=pos; } for (I=cur[u];i!=-1;i=edge[i].next) {if (d[edge[i].v]+1==d[u]&&edge[i].cap) {Brea K }} if (I!=-1) {///If a viable augmentation path is found, update cur[u]=i;//update the current ARC pre[edge[i].v]=u;//update path U=EDG e[i].v;//Backtracking} else{if (--num[d[u]]==0) break;//gap optimization int mind=nv; for (I=head[u];i!=-1;i=edge[i].next) {//Find minimum hierarchy if (EDGE[I].CAP&&MIND>D[EDGE[I].V]) { Cur[u]=i; MIND=D[EDGE[I].V]; }} d[u]=mind+1; num[d[u]]++; U=pre[u]; }} printf ("%d\n", Flow);} int main () {int i,a,b,c; while (~SCANF ("%d%d", &n,&m)) {memset (head,-1,sizeof (head)); cnt=0; while (n--) {scanf ("%d%d%d", &a,&b,&c);//Add edge Add (a,b,c); } S=1; T=m; nv=t+1; ISAP (); } return 0;}
EK ()
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #include < queue>using namespace std; #define INF 9999999999int flow[210][210];int maxflow[210],father[210],vis[210];int max_ Flow;int m,i;void EK (int s,int e) {queue<int >q; int u,v; while (1) {memset (maxflow,0,sizeof (Maxflow));//each time the augmented path is searched, the inflow capacity of each point is set to 0; memset (vis,0,sizeof (VIS)); maxflow[s]=inf;//source point inflow is set to positive infinity; Q.push (s);//press source point into queue; while (!q.empty ())//When queue is not empty {U=q.front () ; Q.pop (); for (v=s;v<=e;v++) {if (!vis[v]&&flow[u][v]>0) { Vis[v]=1; Father[v]=u;//records His father handy reverse update q.push (v); Maxflow[v]=min (Maxflow[u],flow[u][v]);//The capacity of the current point is the smaller of the father point capacity and edge traffic} if (maxflow[e]>0)/ /empty Queue {while (!q.empty ()) If a meeting point is found and the sink is not 0 Q.pop (); Break }} if (maxflow[e]==0)//has not been found to the meeting point of the Grace Road after the exit of the entire loop break; for (I=e;i!=s;i=father[i]) {flow[father[i]][i]-=maxflow[e];//forward update Flow[i][father[i]]+=maxflow [e];//Reverse Update} max_flow+=maxflow[e];//update maximum stream}}int main () {int n; int si,ei,ci; while (~SCANF ("%d%d", &n,&m)) {max_flow=0;//maximum flow initialization; memset (flow); for (i=0;i<n;i++) {scanf ("%d%d%d", &si,&ei,&ci); Flow[si][ei]+=ci; } EK (1,m); printf ("%d\n", Max_flow); }}
POJ 1273-drainage Ditches (network flow _ Max Stream _isap () algorithm and EK () algorithm)