【poj 1273】Drainage Ditches (最大流裸题)

来源:互联网 发布:java线程注入service 编辑:程序博客网 时间:2024/06/16 02:27

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 65780 Accepted: 25402
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
Source
USACO 93

。。。。。。。。。。。最大流第一题
题意:求从源点S到汇点T的最大流。

     -----------1.Dinic算法---------------O(n^2*m) 16MS

1.find the better ,route (bfs 找增广路&&预处理!层次图!d【】);

2.dfs。在层次图中一条线路沿增广路更新残余网络;

#include<iostream>#include<stdio.h>#include<vector>#include<queue>#include<string.h>#define clr(p)  memset(p,0,sizeof(p))using namespace std;int n,m;struct Edge{    int from,to,cap,flow;    Edge(int x,int y,int v,int now)    {        from=x,to=y,cap=v,flow=now;    }Edge(){}};vector<Edge> edge;vector<int> G[205];int en,st;int tot=0;int d[205];int INF=10000009;void add(int a,int b,int v){    edge.push_back(Edge(a,b,v,0));    edge.push_back(Edge(b,a,0,0));    G[a].push_back(tot++);    G[b].push_back(tot++);}bool bfs(){    memset(d,-1,sizeof(d));    queue<int> q;    d[st]=0;    q.push(st);    while(!q.empty())    {        int now=q.front();q.pop();        for(int i=0;i<G[now].size();i++)        {            Edge e=edge[G[now][i]];            if(d[e.to]==-1&&e.cap>e.flow)            {                d[e.to]=d[now]+1;                q.push(e.to);            }        }    }    return d[en]!=-1;}int dfs(int x,int a){    if(a==0||x==en) return a;    int flow=0,f;    for(int i=0;i<G[x].size();i++)    {        Edge& e=edge[G[x][i]];        if(d[e.to]==d[x]+1&&(f=dfs(e.to,min(e.cap-e.flow,a)))>0)        {            e.flow+=f;            edge[G[x][i]^1].flow-=f;            a-=f;            flow+=f;            if(!a) break;        }    }    return flow;}int Dinic(){    int ret=0;    while(bfs())    {        clr(cur);        ret+=dfs(st,INF);    }    return ret;}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        tot=0;        st=1;        en=m;        int aa,bb,cc;        for(int i=1;i<=m;i++)        G[i].clear();        edge.clear();        for(int i=1;i<=n;i++)        {            scanf("%d%d%d",&aa,&bb,&cc);            add(aa,bb,cc);        }        printf("%d\n",Dinic());    }}
 ------------2.isap-----------------O(?) 32MS?? 多路分流增广思想, 1.sap上gap【】优化(如果一次重标号时,出现距离断层,则可以证明无可行流) 2.允许弧??
#include<iostream>#include<stdio.h>#include<string.h>#define INF 1000000#define clr(p)  memset(p,0,sizeof(p));#define log(p)  memset(p,-1,sizeof(p));using namespace std;//--------------闭合图 int head[65000],next[360500],len[360500],to[360500],n,m,nn,d[360500],gap[360500];int ans=0,sum=0;int tot,st,en;void add(int a,int b,int c){    next[tot]=head[a];    to[tot]=b;    len[tot]=c;    head[a]=tot++;}int isap(int x,int aug){    if(x==en)  return aug;    int flow=0;    int mind=nn-1;    for(int i=head[x];i!=-1;i=next[i])    {        int y=to[i];        int v=len[i];        if(v)        {               if(d[x]==d[y]+1)            {                int t=isap(y,min(aug-flow, v));                len[i]-=t;                len[i^1]+=t;                flow+=t;                }            mind=min(mind,d[y]);        }           if(d[st]==nn) return flow;        if(flow==aug) return flow;    }    if(!flow)    {           if(--gap[d[x]]==0)  d[st]=nn;        d[x]=mind+1;        gap[d[x]]++;    }    return flow;}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {               st=1;        log(head);        clr(d);        clr(gap);        en=m;        nn=m;        int aa,bb,cc;        for(int i=1;i<=n;i++)        {            scanf("%d%d%d",&aa,&bb,&cc);            add(aa,bb,cc);            add(bb,aa,0);        }        int ans=0;          while(d[st]<nn) ans+=isap(st,INF);        printf("%d\n",ans);    }}
1 0
原创粉丝点击