[BZOJ1070]修车

题目链接BZOJ1070

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

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

发布了33 篇原创文章 · 获赞 2 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章