Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P2053 [SCOI2007]修车

P2053 [SCOI2007]修车

对于一个工人,修一个车,后面排队的人都得一起等着,将一个工人拆为 $n$ 个点,第 $k$ 个点表示使 $k$ 个人等着的,对于工人的每个点,向每个车连边,记修该车时间为 $t$ ,那么有 $k$ 个人等着的总时间为 $kt$ ,这样就转化为了一个带权匹配问题。因为为了最小代价,所以如果该工人使更少人等待的点没有被匹配到,一定不会选择使更多人等待的点,从而保证了答案符合实际情况(即不会出现一个工人使 $3$ 个人等待的点没有匹配,却匹配了使 $4$ 个人等待的点匹配了,不符合事实)。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cstdio>
#include <queue>
#include <climits>
#define INF INT_MAX
using namespace std;
const int N = 610, M = 8e4 + 10;
bool vis[N];
int n, m, st, ed, d[N], pre[N], incf[N];
int idx = -1, hd[N], nxt[M], edg[M], wt[M], f[M];
bool spfa()
{
for (int i = st; i <= ed; i++)
incf[i] = 0;
for (int i = st; i <= ed; i++)
d[i] = INF;
incf[st] = INF;
d[st] = 0;
queue <int> q;
q.push(st);
vis[st] = true;
while (!q.empty())
{
int t = q.front();
q.pop();
vis[t] = false;
for (int i = hd[t]; ~i; i = nxt[i])
if (d[t] + f[i] < d[edg[i]] && wt[i])
{
d[edg[i]] = d[t] + f[i];
pre[edg[i]] = i;
incf[edg[i]] = min(wt[i], incf[t]);
if (!vis[edg[i]])
{
vis[edg[i]] = true;
q.push(edg[i]);
}
}
}
return incf[ed] > 0;
}
int ek()
{
int res = 0;
while (spfa())
{
int t = incf[ed];
res += t * d[ed];
for (int i = ed; i != st; i = edg[pre[i] ^ 1])
{
wt[pre[i]] -= t;
wt[pre[i] ^ 1] += t;
}
}
return res;
}
void add(int a, int b, int c, int d)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
f[idx] = d;
}
int main()
{
cin >> m >> n;
st = 0;
ed = n + n * m + 1;
for (int i = st; i <= ed; i++)
hd[i] = -1;
for (int i = 1; i <= n; i++)
{
add(st, i, 1, 0);
add(i, st, 0, 0);
}
for (int i = 1, a; i <= n; i++)
for (int j = 1; j <= m; j++)
{
cin >> a;
for (int k = 1; k <= n; k++)
{
add(i, n + (j - 1) * n + k, 1, k * a);
add(n + (j - 1) * n + k, i, 0, - k * a);
}
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
{
add(n + (i - 1) * n + j, ed, 1, 0);
add(ed, n + (i - 1) * n + j, 0, 0);
}
printf("%.2lf", (double)ek() / (double)n);
return 0;
}