【HDU3549】

(http://www.elijahqi.win/2017/07/11/%E3%80%90hdu3549%E3%80%91/)
Problem Description

Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.

Input

The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 < = M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)

Output

For each test cases, you should output the maximum flow from source 1 to sink N.

Sample Input
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1

Sample Output
Case 1: 1
Case 2: 2

题解:好像名字就叫网络流问题 于是用了两种方法试试
dinic:

#include<cstdio>
#include<cstring>
int S,T,map[20][20],f[20],data[20],n,m,t;
int min(int x,int y){
    return x<y?x:y;
}
bool bfs(){
    memset(f,-1,sizeof(f));
    int op=0,cl=1;data[1]=S;f[1]=0;
    bool flag=false;
    while (op<cl){
        op++;int u=data[op];
        for (int v=1;v<=T;++v){
            if (map[u][v]!=0&&f[v]==-1){
                f[v]=f[u]+1;data[++cl]=v;
                if (v==T) {
                    flag=true;
                }
            }
        }
    }
    return flag;
}
int dfs(int u,int s){
    if (u==T) return s;
    int ss=s;
    for (int v=1;v<=n;++v){
        if (map[u][v]!=0&&f[v]==f[u]+1){
            int x=dfs(v,min(map[u][v],s));
            s-=x;
            map[u][v]-=x;
            map[v][u]+=x;
            if (s==0) return ss;
        }
    }
    return ss-s;
}
int main(){
    freopen("hdu3549.in","r",stdin);
    freopen("hdu3549.out","w",stdout);
    scanf("%d",&t);
    int tmpp=t;
    while (t--){
        scanf("%d%d",&n,&m);
        memset(map,0,sizeof(map));
        for (int i=1;i<=m;++i){
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            map[x][y]+=c;
        }
        S=1;T=n;
        int ans=0,maxlong=0x7fffffff;
        while (bfs()){
            int x=dfs(S,maxlong);
            ans+=x;
        }
        printf("Case %d: %d\n",tmpp-t,ans);
    }
    return 0;
}

sap

#include<cstdio>
#include<cstring>
int S,T,t,n,m,map[20][20],h[20],f[20];
int min(int x,int y){
    return x<y?x:y;
}
int sap(int u,int ss){
    if (u==T) return ss;
    int temp=ss;
    int tmp=T-1;
    for (int v=1;v<=n;++v){
        if (map[u][v]!=0){
            if (h[u]==h[v]+1){
                int x=sap(v,min(map[u][v],ss));
                ss-=x;
                map[u][v]-=x;
                map[v][u]+=x;
                if (ss==0) return temp; 
            }
            if (h[v]<tmp) tmp=h[v];
        }

    }
    if (temp==ss){
        f[h[u]]--;
        if (f[h[u]]==0) h[1]=n;
        h[u]=tmp+1;
        f[h[u]]++;
    }
    return temp-ss;
}
int main(){
    freopen("hdu3549.in","r",stdin);
    freopen("hdu3549.out","w",stdout);
    scanf("%d",&t);
    int tmpp=t;
    while (t--){
        scanf("%d%d",&n,&m);
        memset(map,0,sizeof(map));
        for (int i=1;i<=m;++i){
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            map[x][y]+=c;
        }
        S=1;T=n;
        memset(h,0,sizeof(h));memset(f,0,sizeof(f));
        f[0]=n;
        int ans=0;
        while (h[S]<n){
            int x=sap(S,0x7fffffff);
            ans+=x;
        }
        printf("Case %d: %d\n",tmpp-t,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值