( 图论专题 )【 最小费用最大流 】

( 图论专题 )【 最小费用最大流 】

推荐视频理解:https://www.bilibili.com/video/av65039892?from=search&seid=1822150581423158194

费用流算法

现在我们想象假如我们有一个流量网络,现在每个边除了流量,现在还有一个单位费用,这条边的费用相当于它的单位费用乘上它的流量,我们要保持最大流的同时,还要保持边权最小,这就是最小费用最大流问题。 因为在一个网络流图中,最大流量只有一个,但是“流法”有很多种,每种不同的流法所经过的边不同因此费用也就不同,所以需要用到最短路算法。 总增广的费用就是最短路总流量

SFPA 代码

就是把Dinic代码中,找增光路的代码换成了找最短路,顺便求一下流量  。 有许多初始化的地方容易漏易错; flow和dis容易混易错;

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
 
using namespace std;
 
const int maxn = 1e5+10;
struct node {
    int to,w,f,nxt;
} e[maxn];
int n,m,s,t,maxflow,mincost;
int dis[maxn],flow[maxn],via[maxn],pre[maxn],last[maxn];
int head[maxn],cnt=0;
 
void addedge( int u, int v, int f, int w )
{
    e[cnt].to = v;
    e[cnt].f = f;
    e[cnt].w = w;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}
 
int spfa()
{
    memset(dis,inf,sizeof(dis));
    memset(flow,inf,sizeof(flow));
    memset(via,0,sizeof(via));
    queue <int> Q;
    Q.push(s); via[s]=1; dis[s]=0; pre[t]=-1;
    while ( !Q.empty() ) {
        int x = Q.front(); Q.pop(); via[x]=0;
        for ( int i=head[x]; i!=-1; i=e[i].nxt ) {
            int y = e[i].to, f=e[i].f, w=e[i].w;
            if ( f && dis[y]>dis[x]+w ) { // 只要最短流能更新就更新
                dis[y] = dis[x] + w;
                pre[y] = x; // y 的父节点是x
                last[y] = i; // y点连接其父节点的边,编号为i
                flow[y] = min(flow[x],f); // 源点到y点的最大流量。会被最小的一个分支限制住
                if ( via[y]==0 ) {    // 只有队列中没有当前值才往队列里加。
                    Q.push(y); via[y]=1;
                }
            }
        }
    }
    return pre[t]!=-1; // 判断汇点是否有点连入,即还存不存在增广路。初始化pre[t]=-1. 
}
 
void MCMF()
{
    maxflow = mincost = 0;
    while ( spfa() ) { // 还存在增广路就进入
        int x = t;
        maxflow += flow[t]; // 源点到t点的最大流量
        mincost += flow[t]*dis[t];
        while ( x!=s ) {  // 递归改变边的流量
            e[last[x]].f -= flow[t];
            e[last[x]^1].f += flow[t];
            x = pre[x];
        }
    }
}
 
int main()
{
    int x,y,f,w;
    memset(head,-1,sizeof(head));
    cin >> n >> m >> s >> t;
    for ( int i=0; i<m; i++ ) {
        cin>>x>>y>>f>>w;
        addedge( x,y,f,w );  //建立正向边
        addedge( y,x,0,-w );  // 反向边,流量为0, 权值为-w
    }
    MCMF();
    cout << maxflow << " " << mincost << endl;
 
    return 0;
}

 


例题1:  Going Home    POJ - 2195

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.

Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point.


You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

Sample Input

2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

Sample Output

2
10
28

题意:m代表人, H代表房子, 每个m都要到达H,每个H只能容纳一个m, 求所有m到达H的最小花费( 走过的距离 )。

思路:最小费用最大流, 设超级源点连接所有m, 流量为1, 权重为0。 所有H连接超级汇点,流量为1,权重为0。 所有m和所有H连接, 流量为1,权重为距离。

注:需要将两维地图抽象为一个图,首先需要将m和H编号, 题意说m和H在2~100内, 所以m从1编号, H从102开始编号,这样就不会冲突。

代码:

#include <iostream>
#include <math.h>
#include <cmath>
#include <queue>
#include <cstring>
#define inf 0x3f3f3f3f

using namespace std;

const int maxn = 1e5+10;
int n,m,s,t,maxflow,mincost;
int pre[maxn],via[maxn],dis[maxn],last[maxn],flow[maxn];
int head[maxn];
char a[105][105];
struct node {
    int to,w,f,nxt;
} e[maxn];
struct nod {
    int x,y;
} M[maxn],H[maxn];
int cnt1,cnt2,cnt;

void addage( int u, int v, int f, int w )
{
    e[cnt].to = v;
    e[cnt].f = f;
    e[cnt].w = w;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

int spfa()
{
    memset(dis,inf,sizeof(dis));
    memset(flow,inf,sizeof(flow));
    memset(via,0,sizeof(via));
    queue <int> Q;
    Q.push(s); dis[s]=0; via[s]=1; pre[t] = -1;
    while ( !Q.empty() ) {
        int x = Q.front(); Q.pop(); via[x] = 0;
        for ( int i=head[x]; i!=-1; i=e[i].nxt ) {
            int y=e[i].to, f=e[i].f, w=e[i].w;
            if ( f && dis[y]>dis[x]+w ) {
                dis[y] = dis[x] + w;
                pre[y] = x;
                flow[y] = min( flow[x], f );
                last[y] = i;
                if ( via[y]==0 ) {
                    Q.push(y); via[y] = 1;
                }
            }
        }
    }
    return pre[t]!=-1;
}

void MFML()
{
    maxflow = mincost = 0;
    while ( spfa() ) {
        maxflow += flow[t];
        mincost += flow[t]*dis[t];
        int x = t;
        while ( x!=s ) {
            e[last[x]].f -= flow[x];
            e[last[x]^1].f += flow[x];
            x = pre[x];
        }
    }
    cout << mincost << endl;
}

int dist( nod x, nod y )
{
    return ( abs(x.x-y.x) + abs(x.y-y.y) );
}

int main()
{
    int i,j;
    while ( cin>>n>>m ) {
        if ( n==0&&m==0 ) break ;
        cnt1=1; cnt2 = 102; cnt = 0;
        s = 0, t = 210;
        memset(head,-1,sizeof(head));
        for ( i=0; i<n; i++ ) {
            for ( j=0; j<m; j++ ) {
                cin >> a[i][j];
                if ( a[i][j]=='m' ) {
                    M[cnt1].x = i;
                    M[cnt1].y = j;
                    addage(s,cnt1,1,0);
                    addage(cnt1,s,0,0);
                    cnt1 ++;
                }
                if ( a[i][j]=='H' ) {
                    H[cnt2].x = i;
                    H[cnt2].y = j;
                    addage(t,cnt2,0,0);
                    addage(cnt2,t,1,0);
                    cnt2 ++;
                }
            }
        }
        for ( i=1; i<cnt1; i++ ) {
            for ( j=102; j<cnt2; j++ ) {
                addage( i,j, 1, dist(M[i],H[j]) );
                addage( j, i, 0, -dist(M[i],H[j]) );
            }
        }
        MFML();
    }

    return 0;
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值