[题解] PowerOJ 1753 分配问题 (最大/小费用最大流)

- 传送门 -

 https://www.oj.swust.edu.cn/problem/show/1753

# 1753: 分配问题

Time Limit: 1000 MS Memory Limit: 65536 KB
Total Submit: 85 Accepted: 62 Page View: 437

Description

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

Input

由文件input.txt提供输入数据。文件的第1 行有1 个正整数n,表示有n件工作要分配 给n 个人做。接下来的n 行中,每行有n 个整数Cij ,1≤i≤n,1≤j≤n(n<=100),表示第i 个人做 第j件工作产生的效益为Cij。

Output

程序运行结束时,将计算出的最小总效益和最大总效益输出到文件output.txt中。

5
2 2 2 1 2
2 3 1 2 4
2 0 1 1 1
2 3 4 3 3
3 2 1 2 1

5
14

Source

线性规划与网络流24题
 

- 思路 -

 愉悦的最小/最大费用最大流.
 人为 X 集, 工作为 Y 集, 连边容量都为0, 俩集合间的边把费用算上.
 
 细节见代码.
 

- 代码 -

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
 
const int N = 205;
const int M = 1e4 + 500;
const int inf = 0x3f3f3f3f;
 
int TO[M], CST[M], V[M], NXT[M];
int PRE[N], HD[N], DIS[N], VIS[N];
int n, ss, tt, sz, ans;
queue<int> q;
 
void add(int x, int y, int z, int c) {
    TO[sz] = y; V[sz] = z; CST[sz] = c;
    NXT[sz] = HD[x]; HD[x] = sz++;
    TO[sz] = x; V[sz] = 0; CST[sz] = -c;
    NXT[sz] = HD[y]; HD[y] = sz++;
}
 
bool min_spfa() {
    memset(DIS, 0x3f, sizeof (DIS));
    DIS[ss] = 0;
    q.push(ss);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        VIS[u] = 0;
        for (int i = HD[u]; i != -1; i = NXT[i]) {
            int v = TO[i];
            if (V[i] && DIS[v] > DIS[u] + CST[i]) {
                DIS[v] = DIS[u] + CST[i];
                PRE[v] = i;
                if (!VIS[v]) {
                    VIS[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return DIS[tt] != inf;
}
 
bool max_spfa() {
    memset(DIS, 0xc0, sizeof (DIS)); //一开始初始化了-1, 然后发现输入中是有负数的, 于是又愉快的wa了一发
    DIS[ss] = 0;
    q.push(ss);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        VIS[u] = 0;
        for (int i = HD[u]; i != -1; i = NXT[i]) {
            int v = TO[i];
            if (V[i] && DIS[v] < DIS[u] + CST[i]) {
                DIS[v] = DIS[u] + CST[i];
                PRE[v] = i;
                if (!VIS[v]) {
                    VIS[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return DIS[tt] > 0 ;
}
 
int micmf() {
    int cost = 0;
    while (min_spfa()) {
        for (int i = tt; i != ss; i = TO[PRE[i]^1]) {
            V[PRE[i]] -= 1;
            V[PRE[i] ^ 1] += 1;
            cost += CST[PRE[i]];
        }
    }
    return cost;
}
 
int macmf() {
    int cost = 0;
    while (max_spfa()) {
        for (int i = tt; i != ss; i = TO[PRE[i]^1]) {
            V[PRE[i]] -= 1;
            V[PRE[i] ^ 1] += 1;
            cost += CST[PRE[i]];
        }
    }
    return cost;
}
 
void init() {
    for (int i = 0; i < sz; ++i)
        V[i] = (i % 2) ^ 1;
}
 
int main() {
    memset(HD, -1, sizeof (HD));
    scanf("%d", &n);
    ss = 0, tt = n * 2 + 1;
    for (int i = 1; i <= n; ++i) {
        add(ss, i, 1, 0);
        add(n + i, tt, 1, 0);
        for (int j = 1, x; j <= n; ++j) {
            scanf("%d", &x);
            add(i, n + j, 1, x);
        }
    }
    printf("%d\n", micmf());
    init(); //注意初始化
    printf("%d\n", macmf());
    return 0;
}
posted @ 2017-08-24 11:12  lstttt  阅读(240)  评论(0编辑  收藏  举报