1. 程式人生 > >【模板歸納】網路流及費用流

【模板歸納】網路流及費用流

首先是網路流及最小費用最大流的兩種最基礎演算法
這兩種網路流演算法的思想核心都是尋找增廣路+沿增廣路擴充套件新流
首先是Dinic 演算法
使用bfs尋找增廣路,記錄增廣路中節點層數,
而在dfs中沿著層數+1的方向不斷遞推
直到無法再找到新的增廣路為止

程式碼

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include
<cstring>
using namespace std; #define N 105 #define next nico int head[N],to[N*N*2],next[N*N*2],dis[N*N*2],tot=1,d[N],s=0,t,n,m; void add(int x,int y, int z) { to[++tot]=y; dis[tot]=z; next[tot]=head[x]; head[x]=tot; } int bfs() { memset(d,0,sizeof(d)); queue <int>
q; d[s]=1; q.push(s); while(!q.empty()) { int x = q.front(); q.pop(); for(int i = head[x]; i; i = next[i]) { int des = to[i]; if(dis[i]&&!d[des]) { d[des]=d[x]+1; q.push(des);
} } } return d[t]; } int dfs(int x,int v) { if(x==t||v==0)return v; int ans = 0; for(int i = head[x]; i ; i = next[i]) { int des = to[i]; if(d[des]==d[x]+1) { int f = dfs(des,min(dis[i],v)); v -= f; dis[i]-=f; dis[i^1]+=f; ans += f; } } return ans; } int dinic() { int ans = 0; while(bfs()) { ans +=dfs(s,0x7f7f7f7f); } return ans ; }

接下來是最小費用最大流
存圖時注意反向邊的費用是正向邊的相反數

同dinic演算法的區別在於一次只有一條增廣路,且這條增廣路是費用最短路
可以使用spfa來尋找增廣路
然而

眾所周知 spfa 它死了

所以我採用了dijkstra
但是dijkstra不能處理負邊權,怎麼辦?
一種方法是每條邊權加上一個足夠大的數最後再減去
但是有著溢位的風險!
另一種方法
我們採用陣列h[i]表示上一次增廣路時的最短路
w[i][j]表示連線i,j邊的權值
那麼有h[i]+w[i][j]>=h[j]
所以h[i]-h[j]+w[i][j]>=0
記儲存當前最短路的陣列為f[i]
而我們可以證明最後求出的最短路長度即為f[i]-h[i]
最後為了維護h
只需要每個h[i]+=f[i]
程式碼

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <algorithm>
#define N 5003
#define next nico
using namespace std;
int head[N],to[N*20],v[N*20],cost[N*20],from[N],flow[N],next[N*20],f[N],path[N],tot=1,h[N],n,m;
int sumflow,sumcost;
int s,t;
void add(int x,int y,int a,int b)
{
    to[++tot]=y;
    cost[tot]=a;
    v[tot]=b;
    next[tot]=head[x];
    head[x]=tot;
}
struct dat
{
    int val,p;
    bool operator < (const dat &b )const
    {
        return val > b.val;
    }
}buf;
int dijkstra()
{
    memset(f,0x7f,sizeof(f));
    memset(flow,0x7f,sizeof(flow));
    memset(from,0,sizeof(from));
    memset(path,0,sizeof(path));
    int inf = f[0];
    f[s]=0;
    priority_queue <dat> p;
    p.push(dat{0,s});
    while(!p.empty())
    {
        dat t = p.top();
        p.pop(); 
        int x = t.p;
        if(t.val==f[x])
        for(int i = head[t.p];i;i=next[i])
        { 
            int des = to[i];
            if(v[i]&&f[x]+cost[i]+h[x]-h[des]<f[des])
            {
                f[des]=f[x]+cost[i]-h[des]+h[x];
                flow[des]=min(flow[x],v[i]);
                from[des]=x;
                path[des]=i;
                p.push(dat{f[des],des});
            }
        }
    }
    return f[t]!=inf;
}
void mcmf()
{
    while(dijkstra())
    {
        sumflow+=flow[t];
        sumcost+=flow[t]*(f[t]+h[t]);
        for(int i = t; i != s; i = from[i])
        {
            v[path[i]]-=flow[t];
            v[path[i]^1]+=flow[t];
        }
        for(int i = 1; i <= n; i ++)
        {
            h[i]+=f[i];
        }
    }
}
int main()
{
    scanf("%d%d%d%d",&n,&m,&s,&t);
    for(int i = 1; i <= m ; i ++)
    {
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        add(a,b,d,c);
        add(b,a,-d,0);
    }
    mcmf();
    printf("%d %d",sumflow,sumcost);
}