// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 1e3+5;
int n, m, q, d[maxn], cnt[maxn], res[maxn], tmp[3][maxn], dp[maxn][3][maxn];
bool mark[maxn];
vector <int> r, adj[maxn];
void dfs(int v, int p) {
mark[v] = 1;
cnt[v] = 1;
dp[v][0][0] = 0, dp[v][0][1] = inf;
dp[v][1][0] = dp[v][1][1] = inf;
dp[v][2][0] = inf, dp[v][2][1] = d[v];
for (int u : adj[v]) {
if (u == p) continue;
dfs(u, v);
for (int i = 0; i <= cnt[v]; i++) {
for (int j = 0; j <= cnt[u]; j++) {
smin(tmp[0][i+j], dp[v][0][i]+min(dp[u][0][j], dp[u][1][j]));
smin(tmp[1][i+j], dp[v][1][i]+min(min(dp[u][0][j], dp[u][1][j]), dp[u][2][j]));
smin(tmp[1][i+j], dp[v][2][i]+min(dp[u][1][j], dp[u][2][j]));
smin(tmp[2][i+j], dp[v][2][i]+dp[u][0][j]);
}
}
cnt[v] += cnt[u];
for (int i = 0; i <= cnt[v]; i++) {
dp[v][0][i] = tmp[0][i], dp[v][1][i] = tmp[1][i], dp[v][2][i] = tmp[2][i];
tmp[0][i] = tmp[1][i] = tmp[2][i] = inf;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < maxn; j++) tmp[i][j] = inf;
}
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> d[i];
for (int i = 0; i < m; i++) {
int v, u; cin >> v >> u;
adj[--v].pb(--u);
adj[u].pb(v);
}
for (int i = 0; i < n; i++) {
if (!mark[i]) {
dfs(i, i);
r.pb(i);
}
}
int w = 0;
for (int i = 0; i < r.size(); i++) {
for (int j = 0; j <= w; j++) {
for (int k = 0; k <= cnt[r[i]]; k++) {
smin(tmp[0][k+j], res[j]+min(dp[r[i]][0][k], dp[r[i]][1][k]));
}
}
w += cnt[r[i]];
for (int j = 0; j <= w; j++) {
res[j] = tmp[0][j];
tmp[0][j] = inf;
}
}
cin >> q;
while (q--) {
int s, ans = 0; cin >> s;
for (int i = 0; i <= n; i++) {
if (res[i] <= s) ans = i;
}
cout << ans << '\n';
}
return 0;
}
Compilation message
dzumbus.cpp: In function 'int32_t main()':
dzumbus.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 0; i < r.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12380 KB |
Output is correct |
2 |
Correct |
6 ms |
12380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12380 KB |
Output is correct |
2 |
Correct |
6 ms |
12380 KB |
Output is correct |
3 |
Correct |
140 ms |
14452 KB |
Output is correct |
4 |
Correct |
142 ms |
15200 KB |
Output is correct |
5 |
Correct |
141 ms |
14768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
4688 KB |
Output is correct |
2 |
Correct |
41 ms |
4436 KB |
Output is correct |
3 |
Correct |
40 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12380 KB |
Output is correct |
2 |
Correct |
6 ms |
12380 KB |
Output is correct |
3 |
Correct |
140 ms |
14452 KB |
Output is correct |
4 |
Correct |
142 ms |
15200 KB |
Output is correct |
5 |
Correct |
141 ms |
14768 KB |
Output is correct |
6 |
Correct |
37 ms |
4688 KB |
Output is correct |
7 |
Correct |
41 ms |
4436 KB |
Output is correct |
8 |
Correct |
40 ms |
4948 KB |
Output is correct |
9 |
Correct |
138 ms |
14396 KB |
Output is correct |
10 |
Correct |
144 ms |
14840 KB |
Output is correct |
11 |
Correct |
145 ms |
14608 KB |
Output is correct |