P2053 [SCOI2007] 修车
题意
有 \(n\) 个工人,\(m\) 个工作。
每个人给每个工作有 \(t_{i, j}\) 的花费。
求每个工作的最小平均花费。
Sol
直接连边跑费用流不好搞。
考虑将每种工人在不同时间做的工作暴力建点。
枚举 \(k\) 表示第 \(i\) 个工人在倒数第 \(k\) 个做 \(j\) 工作。
这样仍然不好考虑贡献,可以使用类似于贡献提前的思想。
将朴素的贡献改为当前工作的等待时间。
也就是:\(t_{i, j} \times k\)。
直接建边跑费用流即可。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <queue>
#include <bitset>
#define int long long
#define pii pair <int, int>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10);
}
#define fi first
#define se second
const int N = 3005, M = 3e5 + 5, inf = 1e18;
namespace G {
array <int, N> fir;
array <int, M> nex, to, len, cap;
int cnt = 1;
void add(int x, int y, int z, int w) {
cnt++;
nex[cnt] = fir[x];
to[cnt] = y;
cap[cnt] = z;
len[cnt] = w;
fir[x] = cnt;
}
void _add(int x, int y, int z, int w) {
add(x, y, z, w);
add(y, x, 0, -w);
}
}
namespace Mfl {
array <int, N> dis, cur;
queue <int> q;
bitset <N> vis;
bool spfa(pii st) {
dis.fill(inf), vis = 0;
dis[st.fi] = 0, vis[st.fi] = 1;
q.push(st.fi);
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 0;
for (int i = G::fir[u]; i; i = G::nex[i]) {
if (!G::cap[i] || dis[G::to[i]] <= dis[u] + G::len[i]) continue;
dis[G::to[i]] = dis[u] + G::len[i];
if (!vis[G::to[i]]) q.push(G::to[i]), vis[G::to[i]] = 1;
}
}
return dis[st.se] != inf;
}
int dfs(int x, int augcd, pii st) {
if (x == st.se) return augcd;
vis[x] = 1;
int augc = augcd;
for (int &i = cur[x]; i; i = G::nex[i]) {
if (vis[G::to[i]] || !G::cap[i] || dis[G::to[i]] != dis[x] + G::len[i]) continue;
int flow = dfs(G::to[i], min(augc, G::cap[i]), st);
augc -= flow;
G::cap[i] -= flow, G::cap[i ^ 1] += flow;
if (!augc) break;
}
vis[x] = 0;
return augcd - augc;
}
pii dinic(pii st) {
pii ans(0, 0);
while (spfa(st)) {
copy(G::fir.begin(), G::fir.end(), cur.begin());
vis = 0;
int flow = dfs(st.fi, inf, st);
ans.fi += flow, ans.se += flow * dis[st.se];
}
return ans;
}
}
signed main() {
int m = read(), n = read();
pii st = make_pair(n + n * m + 1, n + n * m + 2);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int x = read();
for (int k = 1; k <= n; k++)
G::_add(i, j * n + k, 1, k * x);
}
}
for (int i = 1; i <= n; i++) G::_add(st.fi, i, 1, 0);
for (int i = 1; i <= n * m; i++) G::_add(i + n, st.se, 1, 0);
printf("%.2lf\n", (double)Mfl::dinic(st).se / n);
return 0;
}