[BZOJ1070]修车

题目链接BZOJ1070

题目大意
啊,好懒啊,中文题面以后就不说了吧。

分析
1. 感觉上这是一道网络流的题,却想不出来怎么建图;其实这道题的建图思路很巧妙。
2. 对于一个工作人员 i ,假设他修的倒数第k辆车是 j ,那么会导致总等待时间增加k×cost[i][j],这样以来,其花费就之和 k cost[i][j]有关了。
3. 把每个工作人员拆成 n 个点X[i][k],向所有车子 Y[j] 连边,容量为 1 ,花费为k×cost[i][j]代表其在倒数第 k 辆的时候修第j辆车。
4. S 向所有X连边,容量为 1 ,费用为0;所有 Y T连边,容量为 1 ,费用为0
5. 跑一遍费用流,答案除以 n 就好了。

上代码

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 10 + 5;
const int M = 60 + 5;
const int INF = 0x3f3f3f3f;

int n, m;
int S, T, cnt;
int C[N][M], id1[N][M], id2[M];

int head[N * M], len;
struct nodeLib {
    int to, nxt, val, flow;
    inline void add(int a, int b, int c, int d) {
        to = b, val = d, flow = c;
        nxt = head[a], head[a] = len++;
    }
} lib[N * M * M << 1];

inline int read() {
    char ch;
    int ans = 0, neg = 1;
    while (ch = getchar(), ch < '0' || ch > '9')
        if (ch == '-') neg = -1;
    while (ch >= '0' && ch <= '9')
        ans = ans * 10 + ch - '0', ch = getchar();
    return ans * neg;
}

inline void makePath(int a, int b, int c, int d) {
    lib[len].add(a, b, c, d);
    lib[len].add(b, a, 0, -d);
}
void init() {
    n = read(), m = read();
    len = 2, S = ++cnt, T = ++cnt;
    for (int j = 1; j <= m; j++)
        for (int i = 1; i <= n; i++)
            C[i][j] = read();
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            makePath(S, id1[i][j] = ++cnt, 1, 0);
    for (int i = 1; i <= m; i++)
        makePath(id2[i] = ++cnt, T, 1, 0);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            for (int k = 1; k <= m; k++)
                makePath(id1[i][j], id2[k], 1, j * C[i][k]);
}

queue <int> Q;
bool inQ[N * M];
int dist[N * M], preE[N * M], preV[N * M];
bool SPFA() {
    memset(dist, 0x3f, sizeof(dist));
    dist[S] = 0, Q.push(S), inQ[S] = true;
    while (!Q.empty()) {
        int tmp = Q.front();
        Q.pop(), inQ[tmp] = false;
        for (int p = head[tmp]; p; p = lib[p].nxt) {
            int now = lib[p].to, val = lib[p].val;
            if (lib[p].flow && dist[now] > dist[tmp] + val) {
                dist[now] = dist[tmp] + val;
                preE[now] = p, preV[now] = tmp;
                if (!inQ[now]) Q.push(now), inQ[now] = true;
            }
        }
    }
    return dist[T] != INF;
}
int MCMF() {
    int ans = 0;
    while (SPFA()) {
        int maxf = INF;
        for (int p = T; p != S; p = preV[p])
            maxf = min(maxf, lib[preE[p]].flow);
        for (int p = T; p != S; p = preV[p])
            lib[preE[p]].flow -= maxf, lib[preE[p] ^ 1].flow += maxf;
        ans += dist[T] * maxf;
    }
    return ans;
}

int main() {
    init();
    printf("%.2lf\n", (double)MCMF() / m);
    return 0;
}


我还是naive啊,没有想出来建图。
以上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值