洛谷 P2053 [SCOI2007]修车(最小费用最大流)

题解

最小费用最大流

n和m是反着的

首先,

\[ans = \sum{cost[i][j]}*k \]

其中,\(k\)为它在当前技术人员那里,排倒数第\(k\)个修

我们可以对于每个技术人员进行拆点,

对于每个技术人员的各个点,表示倒数第几次修

然后每个人连向技术人员,显然花费是根据连的点来算的

然后就是二分图带权最小匹配了

我只会Dinic

Code

#include<bits/stdc++.h>

#define LL long long
#define RG register

using namespace std;
template<class T> inline void read(T &x) {
    x = 0; RG char c = getchar(); bool f = 0;
    while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
    while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
    x = f ? -x : x;
    return ;
}
template<class T> inline void write(T x) {
    if (!x) {putchar(48);return ;}
    if (x < 0) x = -x, putchar('-');
    int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
    for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}

int n, m;

const int N = 1010;

struct node {
    int to, nxt, w, v;
}g[2000000];
int last[N], gl = 1;
void add(int x, int y, int w, int v) {
    g[++gl] = (node) {y, last[x], w, v};
    last[x] = gl;
    g[++gl] = (node) {x, last[y], 0, -v};
    last[y] = gl;
}

int dis[N], s, t, pre[N], from[N];
bool vis[N];
queue<int> q;

bool spfa() {
    memset(dis, 127, sizeof(dis));
    q.push(s);
    dis[s] = 0;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        //	printf("u = %d\n", u);
        for (int i = last[u]; i; i = g[i].nxt) {
            int v = g[i].to;
            if (g[i].w && dis[v] > dis[u]+g[i].v) {
                dis[v] = dis[u]+g[i].v;
                from[v] = i; pre[v] = u;
                if (!vis[v]) {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
        vis[u] = 0;		
    }
//	printf("%d\n", dis[t]);
    return dis[t] != dis[0];
}

int Mcmf() {
    int ans = 0;
    while (spfa()) {
        ans += dis[t];
        for (int i = t; i != s; i = pre[i])
            g[from[i]].w--, g[from[i]^1].w++;
    }
    return ans;
}

int a[70][10];

int main() {
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    read(m), read(n);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            read(a[i][j]);
    for (int k = 1; k <= m; k++)//第k个技术人员
        for (int i = 1; i <= n; i++)//第i个顾客
            for (int j = 1; j <= n; j++)//倒数第j个修理
                add(n*m+i, (k-1)*n+j, 1, j*a[i][k]);
    s = n*m+n+1, t = s+1;
    for (int i = n*m+1; i < s; i++)
        add(s, i, 1, 0);
    for (int i = 1; i <= n*m; i++)
        add(i, t, 1, 0);
    printf("%.2lf\n", Mcmf()*1.0/n);
    return 0;
}


posted @ 2019-01-19 09:44  zzy2005  阅读(96)  评论(0编辑  收藏  举报