码迷,mamicode.com
首页 > 编程语言 > 详细

【最大流】hihocoder 1369 : 网络流一·Ford-Fulkerson算法

时间:2018-06-19 21:32:01      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:log   coder   bfs   scan   map   .net   ++   flow   math   

http://hihocoder.com/problemset/problem/1369?sid=1328132

参考 https://blog.csdn.net/a1799342217/article/details/73195243

https://blog.csdn.net/a519781181/article/details/51908303

【AC1】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
int n,m;
const int maxn=5e2+2;
const int maxm=2e4+2;
const int inf=0x3f3f3f3f;
int maxflow;
struct edge{
    int to;
    int nxt;
    int w;
}e[2*maxm];
int head[maxn];
int tot;
int fa[maxn];
int mp[maxn][maxn];
bool vis[maxn];
void init(){
    memset(mp,-1,sizeof(mp));
    memset(head,-1,sizeof(head));
    tot=0;
    maxflow=0;
}
void add(int u,int v){
    e[tot].to=v;
    e[tot].nxt=head[u];
    head[u]=tot++;
}
bool bfs(int s,int t){
    memset(vis,false,sizeof(vis));
    memset(fa,-1,sizeof(fa));
    queue<int> Q;
    Q.push(s);
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        if(u==t) return true;
        if(vis[u]) continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=e[i].nxt){
            int v=e[i].to;
            if(!vis[v]&&mp[u][v]){
                fa[v]=u;
                Q.push(v);    
            }
        }
    }
    return false;
}
int max_flow(int s,int t){
    int flow=0;
//    int cnt=0;
    while(bfs(s,t)){
//        for(int i=1;i<=n;i++){
//            for(int j=1;j<=n;j++){
//                cout<<mp[i][j]<<" "; 
//            }
//            cout<<endl;
//        }
//        cout<<"**********************"<<endl;
    //    cnt++;
        int u=t;
        int delta=inf;
        while(fa[u]!=-1){
    //        cout<<u<<" ";
            delta=min(delta,mp[fa[u]][u]);
            u=fa[u];
        }
    //    cout<<endl;
    //    cout<<delta<<endl;
        flow+=delta;
        u=t;
        while(fa[u]!=-1){
            mp[fa[u]][u]-=delta;
            mp[u][fa[u]]+=delta;
            u=fa[u];
        }

    }
    return flow;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        init();
        int u,v,c;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&u,&v,&c);
            if(mp[u][v]==-1){
                add(u,v);
                add(v,u);
                mp[u][v]=c;
                mp[v][u]=0;
            }else{
                mp[u][v]+=c;
            }
        }
        int ans=max_flow(1,n);
        printf("%d\n",ans);
    }
    return 0;
}

【AC2】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
int n,m;
const int maxn=5e2+2;
const int maxm=2e4+2;
const int inf=0x3f3f3f3f;
struct edge{
    int to;
    int nxt;
    int w;
}e[maxm<<1];
int head[maxn];
struct node{
    int x;
    int e;
}fa[maxn]; 


bool vis[maxn];
int tot;
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
void add(int u,int v,int c){
    e[tot].to=v;
    e[tot].w=c;
    e[tot].nxt=head[u];
    head[u]=tot++;
}
bool bfs(int s,int t){
    memset(vis,false,sizeof(vis));
    queue<int> Q;
    Q.push(s);
    vis[s]=true;
    while(!Q.empty()){
        int u=Q.front();
        Q.pop();
        if(u==t) return true;
        for(int i=head[u];i!=-1;i=e[i].nxt){
            int v=e[i].to;
            int w=e[i].w;
            if(!vis[v]&&w){
                Q.push(v);
                vis[v]=true;
                fa[v].x=u;
                fa[v].e=i;
            }
        }
    }
    return false;
}
int work(){
    int s=1,t=n;
    for(int i=1;i<=n;i++){
        fa[i].e=-1;
        fa[i].x=-1;
    }
    int ans=0;
    while(bfs(s,t)){
        int delta=inf;
        int u=t;
        while(fa[u].x!=-1){
            delta=min(delta,e[fa[u].e].w);
            u=fa[u].x;
        }
        ans+=delta;
        u=t;
        while(fa[u].x!=-1){
            int i=fa[u].e;
            e[i].w-=delta;
            e[i^1].w+=delta;
            u=fa[u].x;
        }
        for(int i=1;i<=n;i++){
            fa[i].e=-1;
            fa[i].x=-1;
        }
    }
    return ans;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        init();
        int u,v,c;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&u,&v,&c);
            add(u,v,c);
            add(v,u,0);
        }
        int ans=work();
        printf("%d\n",ans);
    }
    return 0;
}

 

【最大流】hihocoder 1369 : 网络流一·Ford-Fulkerson算法

标签:log   coder   bfs   scan   map   .net   ++   flow   math   

原文地址:https://www.cnblogs.com/itcsl/p/9200994.html

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