码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3549网络流之最大流

时间:2017-05-21 16:51:20      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:blog   false   long   lan   flow   namespace   sed   names   矩阵   

Ford-Fulkerson方法:dfs实现

dfs  140ms

技术分享
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 10007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-9;
const int N=50+5,maxn=20+5,inf=999999;

int t,n,m;
int vis[maxn];//判断是否访问过
int c[maxn][maxn];//邻接矩阵存图
int dfs(int u,int low)//深搜找增广路
{
    int i,flow;
    if(u==t)return low;//到达汇点
    if(vis[u])return 0;//节点访问过
    vis[u]=1;
    for(i=1;i<=n;i++)
        if(c[u][i]&&(flow=dfs(i,low<c[u][i]?low:c[u][i])))
        {
            c[u][i]-=flow;
            c[i][u]+=flow;
            return flow;
        }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
 //   cout<<setiosflags(ios::fixed)<<setprecision(2);
    int T,cnt=0;
    cin>>T;
    while(T--){
        cin>>n>>m;
        memset(c,0,sizeof(c));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=m;i++)
        {
            int u,v,w;
            cin>>u>>v>>w;
            c[u][v]+=w;
        }
        t=n;//t是汇点
        int maxflow=0,flow;
        while(flow=dfs(1,inf))//当增广路还存在时
        {
           /* for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                    cout<<c[i][j]<<" ";
                cout<<endl;
            }*/
            maxflow+=flow;
            memset(vis,0,sizeof(vis));
        }
        cout<<"Case "<<++cnt<<": "<<maxflow<<endl;
    }
    return 0;
}
View Code

Edmonds_Karp算法:bfs实现

bfs 156ms

技术分享
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 10007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-9;
const int N=50+5,maxn=20+5,inf=999999;

int t,n,m,pre[N];
bool vis[maxn];//判断是否访问过
int c[maxn][maxn];//邻接矩阵存图
bool bfs()//判断是否存在增广路,并求出
{
    memset(vis,0,sizeof vis);
    memset(pre,0,sizeof pre);
    vis[1]=1;
    queue<int>q;
    q.push(1);
    while(!q.empty()){
        int x=q.front();
        q.pop();
        if(x==t)return 1;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&c[x][i])
            {
                q.push(i);
                pre[i]=x;//记录前驱
                vis[i]=1;
            }
        }
    }
    return 0;
}
int max_flow()
{
    int ans=0;
    while(1){
        if(!bfs())return ans;//找不到增广路了
        int minn=999999;
        for(int i=t;i!=1;i=pre[i])
            minn=min(minn,c[pre[i]][i]);//找出增广路中最小的节点
        for(int i=t;i!=1;i=pre[i])
        {
            c[pre[i]][i]-=minn;//更新残余网络
            c[i][pre[i]]+=minn;
        }
        ans+=minn;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
 //   cout<<setiosflags(ios::fixed)<<setprecision(2);
    int k,cnt=0;
    cin>>k;
    while(k--){
        cin>>n>>m;
        memset(c,0,sizeof(c));
        for(int i=1;i<=m;i++)
        {
            int u,v,w;
            cin>>u>>v>>w;
            c[u][v]+=w;
        }
        t=n;//t是汇点
        cout<<"Case "<<++cnt<<": "<<max_flow()<<endl;
    }
    return 0;
}
View Code

 

hdu3549网络流之最大流

标签:blog   false   long   lan   flow   namespace   sed   names   矩阵   

原文地址:http://www.cnblogs.com/acjiumeng/p/6885103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!