Blog of RuSun

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

P3324 [SDOI2015]星际战争

P3324 [SDOI2015]星际战争

不知道为什么不能实数上的费用流,但是确实很奇怪。

求最小值,考虑二分。对于每一个激光武器,其攻击力此时等于其攻击力乘上时间,做最大流,看是否可以跑慢流量。

好像有点卡精度。

查看代码
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cfloat>
#define inf DBL_MAX
using namespace std;
const double eps = 1e-5;
const int N = 110, M = 1e4 + 10;
bool path[N][N];
int d[N], cur[N];
int n, m, st, ed, A[N], B[M];
int idx = -1, hd[N], nxt[M], edg[M];
double tot, wt[M];
bool bfs ()
{
for (int i = st; i <= ed; i++)
d[i] = -1;
d[st] = 0;
cur[st] = hd[st];
queue <int> q;
q.push(st);
while (!q.empty())
{
int t = q.front();
q.pop();
for (int i = hd[t]; ~i; i = nxt[i])
if (d[edg[i]] == -1 && wt[i])
{
cur[edg[i]] = hd[edg[i]];
d[edg[i]] = d[t] + 1;
if (edg[i] == ed)
return true;
q.push(edg[i]);
}
}
return false;
}
double exploit (int x, double limit)
{
if (x == ed)
return limit;
double res = 0;
for (int i = cur[x]; ~i && res < limit; i = nxt[i])
{
cur[x] = i;
if (d[edg[i]] == d[x] + 1 && wt[i])
{
double t = exploit(edg[i], min(wt[i], limit - res));
if (abs(t) < eps)
d[edg[i]] = -1;
wt[i] -= t;
wt[i ^ 1] += t;
res += t;
}

}
return res;
}
double dinic ()
{
double res = 0, flow;
while (bfs())
while (flow = exploit(st, inf))
res += flow;
return res;
}
void add (int a, int b, double c)
{
nxt[++idx] = hd[a];
hd[a] = idx;
edg[idx] = b;
wt[idx] = c;
}
bool check (double x)
{
idx = -1;
for (int i = st; i <= ed; i++)
hd[i] = -1;
for (int i = 1; i <= n; i++)
{
add(st, i, A[i]);
add(i, st, 0);
}
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
if (path[i][j])
{
add(j, i + n, inf);
add(i + n, j, 0);
}
for (int i = 1; i <= m; i++)
{
add(i + n, ed, x * B[i]);
add(ed, i + n, 0);
}
return abs(dinic() - tot) < eps;
}
int main ()
{
cin >> n >> m;
st = 0;
ed = n + m + 1;
for (int i = 1; i <= n; i++)
{
cin >> A[i];
tot += A[i];
}
for (int i = 1; i <= m; i++)
cin >> B[i];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
cin >> path[i][j];
double l = 0, r = 1e6, mid;
while (r - l > eps)
{
mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid;
}
cout << l;
return 0;
}