poj1273 Drainage Ditches

蒟蒻的blog

POJ 1273

Drainage Ditches

Time Limit: 1000MSMemory Limit: 10000K
Memory Limit: 10000KTotal Submissions: 72327 Accepted: 28143

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

代码:

Dinic

  • 邻接矩阵无优化
#include<cstdio>
#include<cstring>
#define N 220
int map[N][N],h[N],s,t,e,c,data[N],n,m;
int min(int x,int y){
    return x<y?x:y;
}
bool bfs(){
    memset(h,-1,sizeof(h));
    //从源点编号 
    data[1]=s;int op=0,cl=1;h[s]=0;
    bool flag=false;
    while (op<cl){
        ++op;int u=data[op];
        for (int i=1;i<=n;++i)
            if (map[u][i]>0) 
                if (h[i]==-1){
                    h[i]=h[u]+1;data[++cl]=i;
                    if (i==t) flag=true;
                }
    }
    return flag;
}
int dfs(int u,int s){
    //初值
    if (u==n) return s;
    int tmp=s;
    for (int i=1;i<=n;++i){
        if ((map[u][i]>0)&&(h[u]+1==h[i])) {
            int x=dfs(i,min(s,map[u][i]));
            map[u][i]-=x;
            map[i][u]+=x;
            s-=x;
            if (s==0) break;
        }
    } 
    return tmp-s;
}
int main(){
    freopen("poj1273.in","r",stdin);
    freopen("poj1273.out","w",stdout);
    int maxlong=0x7fffffff;
    while (scanf("%d%d",&m,&n)>0){

        memset(map,0,sizeof(map));
        for (int i=1;i<=m;++i){
            scanf("%d%d%d",&s,&e,&c);
            map[s][e]+=c;
        }
        //源点1  汇点1 
        s=1;t=n;
        int ans=0,x=0;
        while (bfs()){
            x=dfs(s,maxlong);
            ans+=x;
        }
        printf("%d\n",ans);
    }

    return 0;
}
  • 邻接矩阵当前弧优化
#include<cstdio>
#include<cstring>
#include<iostream>
//#include<algorithm>
using namespace std;
int n,m,map[220][220],h[220],data[220],cur[220];
bool bfs(){
    memset(h,-1,sizeof(h));
    data[1]=1;h[1]=0;
    int op=0,cl=1;
    while (op<cl){
        ++op;int u=data[op];
        for (int v=1;v<=n;++v){
            if (map[u][v]!=0&&h[v]==-1){
                data[++cl]=v;h[v]=h[u]+1;
                if (v==n) return true;
            }

        }
    }
    return false;

}
//int min(int x,int y){
//    return x<y?x:y;
//}
int dfs(int u,int s){
    if (u==n) return s;
    int ss=s;int y=1;
    for (int v=cur[u];v<=n;++v){
        if (map[u][v]>0&&h[v]==h[u]+1){
            int x=dfs(v,min(map[u][v],s));
            s-=x;
            map[u][v]-=x;
            map[v][u]+=x;
            y=v;
            if (s==0) {

                cur[u]=v;
                return ss;
            }
        }
    }
    cur[u]=y;
    return ss-s;
}
int main(){
    freopen("poj1743.in","r",stdin);
    freopen("poj1743.out","w",stdout);
    while (scanf("%d%d",&m,&n)>0){
        memset(map,0,sizeof(map));
        for (int i=1;i<=m;++i){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            map[x][y]+=z;
        }
        int ans=0;
        while (bfs()){
            for (int i=1;i<=n;++i) cur[i]=1;
            int xx=dfs(1,0x7fffffff);
            ans+=xx;
        }
        printf("%d\n",ans);
    }

    return 0;
}
  • 邻接表无优化
#include<cstdio>
#include<cstring>
#include<iostream>
//#include<algorithm>
using namespace std;
struct node{
    int x,y,z,next;
};
node data1[440];
int n,m,hh[220],h[220],data[220];
bool bfs(){
    memset(hh,-1,sizeof(hh));
    data[1]=1;hh[1]=0;
    int op=0,cl=1;
    while (op<cl){
        ++op;int u=data[op];
        for (int i=h[u];i!=-1;i=data1[i].next){
            int v=data1[i].y;
            if (data1[i].z>0&&hh[v]==-1){
                data[++cl]=v;hh[v]=hh[u]+1;
                if (v==n) return true;
            }   
        }
    }
    return false;

}
//int min(int x,int y){
//  return x<y?x:y;
//}
int dfs(int u,int s){
    if (u==n) return s;
    int ss=s;
    for (int i=h[u];i!=-1;i=data1[i].next){
        int v=data1[i].y;
        if (data1[i].z>0&&hh[v]==hh[u]+1){
            int x=dfs(v,min(data1[i].z,s));
            s-=x;
            data1[i].z-=x;
            data1[i^1].z+=x;
            if (s==0) return ss;
        }
    }
    return ss-s;
}
int main(){
//  freopen("poj1273.in","r",stdin);
//  freopen("poj1273.out","w",stdout);
    while (scanf("%d%d",&m,&n)>0){
        //memset(map,0,sizeof(map));
        memset(h,-1,sizeof(h));
        int num=-1;
        for (int i=1;i<=m;++i){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            data1[++num].x=x;data1[num].y=y;data1[num].z=z;
            data1[num].next=h[x];h[x]=num;
            data1[++num].x=y;data1[num].y=x;
            data1[num].next=h[y];h[y]=num;
            data1[num].z=0;
            //map[x][y]+=z;
        }
        int ans=0;
        while (bfs()){
            int xx=dfs(1,0x7fffffff);
            //printf("%d\n",xx);
            ans+=xx;
        }
        printf("%d\n",ans);
    }

    return 0;
}
  • 邻接表当前弧优化
#include<cstdio>
#include<cstring>
struct node{
    int x,y,z,next;
};
node data[440];int cur[220],f[220],m,n,h[220],st[220];
int min(int x,int y){
    return x<y?x:y;
} 
bool bfs(){
    memset(f,-1,sizeof(f));
    st[1]=1;f[1]=0;
    int op=0,cl=1;
    while(op<cl){
        op++;int u=st[op];
        for (int i=h[u];i!=-1;i=data[i].next){
            int v=data[i].y;
            if (data[i].z>0&&f[v]==-1){
                f[v]=f[u]+1;
                st[++cl]=v;
                if (v==n)return true;
            }
        }
    }
    return false;
}
int dfs(int u,int s){
    if (u==n) return s;
    int y=h[u];int tmp=s;
    for (int i=cur[u];i!=-1;i=data[i].next){

        int v=data[i].y;
        if (data[i].z>0&&f[v]==f[u]+1){
            int x=dfs(v,min(s,data[i].z));
            s-=x;
            y=i;
            data[i].z-=x;
            data[i^1].z+=x;
            if (s==0){
                cur[u]=i;
                return tmp;
            }
        }
    }
    cur[u]=y; 
    return tmp-s;
}
int main(){
    freopen("poj1273.in","r",stdin);
    freopen("poj1273.out","w",stdout);
    while (scanf("%d%d",&m,&n)>0){
        int num=-1;
        memset(h,-1,sizeof(h));
        for (int i=1;i<=m;++i){
            int s,e,c;
            scanf("%d%d%d",&s,&e,&c);
            data[++num].x=s;data[num].y=e;data[num].z=c;data[num].next=h[s];h[s]=num;
            data[++num].x=e;data[num].y=s;data[num].next=h[e];h[e]=num;
        }
        int ans=0;
        for (int i=1;i<=n;++i){
                cur[i]=h[i];
        }
        while(bfs()){
            int x=dfs(1,0x7fffffff);
            ans+=x;
            for (int i=1;i<=n;++i){
                    cur[i]=h[i];
            }   
        }
        printf("%d\n",ans);
    }
    return 0;
} 

sap

想法

Int sap
到u是t的时候返回水量
先备份
给所有子边灌水
1 需要有边,并且只差1
2 求可以给下一个的送水量
image

如果该点无法送水 找所有有关联的h的最小值(不能饱和,也必须有边)

否则返回s1-s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值