网络流24题18. 分配问题

分配问题

Description

有 n 件工作要分配给 n 个人做。第 i 个人做第 j 件工作产生的效益为 cij 。试设计一个将n 件工作分配给 n 个人做的分配方案,使产生的总效益最大。
对于给定的 n 件工作和 n 个人,计算最优分配方案和最差分配方案。

Input

第 1 行有 1 个正整数 n,表示有 n 件工作要分配给 n 个人做。接下来的 n 行中,每行有 n 个整数 cij ,1≤i≤n,1≤j≤n,表示第 i 个人做第 j 件工作产生的效益为 cij

Output

输出计算出的最小总效益和最大总效益。

题解

二分图匹配问题。
建立附加源S,附加汇T。
建图:
1.S向每个人连接一条容量为1,费用为0的边。
2.每件工作向T连接一条容量为1,费用为0的边。
3.每个人向每件工作连接一条容量为1,费用为 cij
答案即为最小(大)费用最大流。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;

const int N = 1000 + 10, M = 1000000 + 10, inf = 0x3f3f3f3f;
struct Edge{
    int fr, to, cap, flow, cost;
}edg[M];
int hd[N], nxt[M], tot;
int n, s, t;
int d[N], a[N], p[N], q[N], inq[N];
int mp[N][N];
void insert(int u, int v, int w, int x){
    edg[tot].fr = u, edg[tot].to = v, edg[tot].cap = w, edg[tot].flow = 0, edg[tot].cost = x;
    nxt[tot] = hd[u]; hd[u] = tot;
    tot++;
    edg[tot].fr = v, edg[tot].to = u, edg[tot].cap = 0, edg[tot].flow = 0, edg[tot].cost = -x;
    nxt[tot] = hd[v]; hd[v] = tot;
    tot++;
}
bool spfa1(int &fl, int &cst){
    for(int i = s; i <= t; i++) d[i] = inf;
    d[s] = 0; p[s] = 0; a[s] = inf;
    int head = 0, tail = 1;
    q[0] = s; inq[s] = 1;
    while(head != tail){
        int u = q[head++]; if(head == 1001) head = 0;
        inq[u] = 0;
        for(int i = hd[u]; i >= 0; i = nxt[i]){
            Edge &e = edg[i];
            if(d[e.to] > d[u] + e.cost && e.cap > e.flow){
                d[e.to] = d[u] + e.cost;
                p[e.to] = i;
                a[e.to] = min(a[u], e.cap - e.flow);
                if(!inq[e.to]){
                    q[tail++] = e.to; if(tail == 1001) tail = 0;
                    inq[e.to] = 1;
                }
            }
        }
    }
    if(d[t] == inf) return false;
    fl += a[t];
    cst += a[t] * d[t];
    int u = t;
    while(u != s){
        edg[p[u]].flow += a[t];
        edg[p[u]^1].flow -= a[t];
        u = edg[p[u]].fr;
    }
    return true;
}
bool spfa2(int &fl, int &cst){
    for(int i = s; i <= t; i++) d[i] = -inf;
    d[s] = 0; p[s] = 0; a[s] = inf;
    int head = 0, tail = 1;
    q[0] = s; inq[s] = 1;
    while(head != tail){
        int u = q[head++]; if(head == 1001) head = 0;
        inq[u] = 0;
        for(int i = hd[u]; i >= 0; i = nxt[i]){
            Edge &e = edg[i];
            if(d[e.to] < d[u] + e.cost && e.cap > e.flow){
                d[e.to] = d[u] + e.cost;
                p[e.to] = i;
                a[e.to] = min(a[u], e.cap - e.flow);
                if(!inq[e.to]){
                    q[tail++] = e.to; if(tail == 1001) tail = 0;
                    inq[e.to] = 1;
                }
            }
        }
    }
    if(d[t] == -inf) return false;
    fl += a[t];
    cst += a[t] * d[t];
    int u = t;
    while(u != s){
        edg[p[u]].flow += a[t];
        edg[p[u]^1].flow -= a[t];
        u = edg[p[u]].fr;
    }
    return true;
}
void init(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= n; j++)
        scanf("%d", &mp[i][j]);
}
void build(){
    memset(hd, -1, sizeof(hd));
    tot = 0;
    s = 0, t = n * 2 + 1;
    for(int i = 1; i <= n; i++){
        insert(s, i, 1, 0);
        insert(n + i, t, 1, 0);
    }
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            insert(i, n + j, inf, mp[i][j]);
}
void work(){
    int flow = 0, cost = 0;
    build();
    while(spfa1(flow, cost));
    printf("%d\n", cost);
    flow = cost = 0;
    build();
    while(spfa2(flow, cost));
    printf("%d\n", cost);
}
int main(){
    freopen("prog818.in", "r", stdin);
    freopen("prog818.out", "w", stdout);
    init();
    work();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值