网络流最大费用流整理

https://blog.csdn.net/jianxingzhang/article/details/81208814 dinic算法

https://www.cnblogs.com/graytido/p/10809211.html why dinic算法分层能优化时间

HDU 3549

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define inf (0x3f3f3f3f)
using namespace std;
typedef long long i64;
const int maxn = 32;
int Grape[maxn][maxn],deep[maxn];
int n;
bool bfs()
{
    memset(deep,-1,sizeof(deep));
    queue<int> q;
    q.push(1);  deep[1] = 0;
    while(!q.empty())
    {
        int u = q.front();    q.pop();
        for(int v=1;v<=n;++v)
        {
            if(deep[v]==-1&&Grape[u][v])
            {
                deep[v] = deep[u] + 1;
                q.push(v);
            }
        }
    }
    if(deep[n] == -1)
        return false;//不存在 s 到 e 的路径
    return true;
}
int dfs(int cur,int exp)//cur 当前访问的节点,exp
{
    if(cur == n)
        return exp;//返回流量
    int cnt;
    for(int v=1;v<=n;++v)
    {
        if(Grape[cur][v]>0&&deep[v]==deep[cur]+1&&(cnt = dfs(v,min(Grape[cur][v],exp))))//当前剩余流量大于0且层数等于+1
        {
            Grape[cur][v] -= cnt;
            Grape[v][cur] += cnt;
            return cnt; 
        }
    }
    return 0;
}
int main()
{
    int t,m,u,v,c,kase = 0;  scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(Grape,0,sizeof(Grape));
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&c);
            Grape[u][v] += c;
        }
        i64 sum = 0,value = 0;
        while(bfs())
        {
            while(value = dfs(1,inf))
            {
                sum += value;
            }
        }
        cout<<"Case "<<++kase<<": "<<sum<<'\n';
    }
}

 多源点多汇点最大网络流,当存在多个源点和多个汇点时,可以假设一个可流出无限流量的源点,连接每个源点,流量为每个源点自身能流出的流量,假设一个可以接收无限流量的汇点,连接每个汇点,权值为汇点最大能接收的流量

POJ 1459

/*
 * @Author: CY__HHH
 * @Date: 2019-10-25 10:05:14
 * @LastEditTime: 2019-10-25 19:33:27
 */
#include<iostream>//多源点汇点最大网络流
#include<cstdio>
#include<cstring>
#include<vector>
#include<sstream>
#include<queue>
#include<algorithm>
#define inf (0x3f3f3f3f)
using namespace std;
typedef long long i64;
const int maxn = 128;
int Grape[maxn][maxn],deep[maxn];
int n,np,nc,m,u,v,w,s,t;
bool bfs()
{
    memset(deep,-1,sizeof(deep));
    queue<int> q;   q.push(s);
    deep[s] = 0;
    while(!q.empty())
    {
        int cur = q.front();
        q.pop();
        for(int i=0;i<=t;++i)
        {
            if(Grape[cur][i] > 0&& deep[i] == -1)
            {
                deep[i] = deep[cur] + 1;
                q.push(i);
            }
        }
    }
    if(deep[t]==-1)
        return false;
    return true;
}
int dfs(int cur,int exp)
{
    if(cur == t)
        return exp;
    int tmp;
    for(int i=0;i<=t;++i)
    {
        if(deep[i]==deep[cur] + 1 && Grape[cur][i] >0 && (tmp = dfs(i,min(exp,Grape[cur][i]))))
        {
            Grape[cur][i] -= tmp;
            Grape[i][cur] += tmp;
            return tmp;
        }
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);    cin.tie(0),cout.tie(0);
    char ch1,ch2,ch3;
    while(cin>>n>>np>>nc>>m)
    {
        memset(Grape,0,sizeof(Grape));
        while(m--)
        {
            cin>>ch1>>u>>ch2>>v>>ch3>>w;
            ++u,++v;
            Grape[u][v] = w;
        }
        s = 0,t = n + 1;
        while(np--)
        {
            cin>>ch1>>v>>ch2>>w;
            ++v;
            Grape[s][v] = w;
        }
        while(nc--)
        {
            cin>>ch1>>v>>ch2>>w;
            ++v;
            Grape[v][t] = w;
        }
        int sum = 0,cnt;
        while(bfs())
        {
            while(cnt = dfs(s,inf))
                sum += cnt;
        }
        cout<<sum<<'\n';
    }
}

 

posted on 2019-10-23 00:01  chengyulala  阅读(123)  评论(0编辑  收藏  举报

导航