#include <bits/stdc++.h>
#define int long long
#define prev pre
using namespace std;
const int N = 1003;
int n;
int a[N];
bool vis[N];
vector<int> g[N];
int sz[N];
int dp[N][N][3];
int prev[N][3];
inline void relax (int& x, int y) {
x = min(x, y);
}
void dfs (int v, int p = 0) {//node, taken people, take my self
dp[v][0][0] = 0;
dp[v][0][1] = a[v];
sz[v] = 1;
vis[v] = true;
for (int u : g[v]) {
if (!vis[u]) {
dfs(u, v);
}
}
for (int i = 0; i <= n; ++i) {
dp[v][i][0] = dp[v][i][1] = dp[v][i][2] = 1e9;
}
dp[v][0][0] = 0;
dp[v][0][1] = a[v];
for (int u : g[v]) {
if (u == p) {
continue;
}
for (int i = 0; i <= n; ++i) {
prev[i][0] = dp[v][i][0];
prev[i][1] = dp[v][i][1];
prev[i][2] = dp[v][i][2];
}
for (int i = 0; i <= sz[v]; ++i) {
for (int j = 0; j <= sz[u]; ++j) {
relax(dp[v][i + j][0], min({dp[u][j][1], dp[u][j][0], dp[u][j][2]}) + prev[i][0]);
relax(dp[v][i + j][1], min(prev[i][0] + a[v], prev[i][1]) + dp[u][j][0]);
relax(dp[v][i + j][2], prev[i][2] + min({dp[u][j][2], dp[u][j][0]}));
relax(dp[v][i + j + 1][2], prev[i][2] + dp[u][j][1]);
relax(dp[v][i + j + 1][2], min(prev[i][0] + a[v], prev[i][1]) + dp[u][j][2]);
relax(dp[v][i + j + 2][2], min(prev[i][0] + a[v], prev[i][1]) + dp[u][j][1]);
}
}
sz[v] += sz[u];
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int m;
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
dfs(i);
}
}
int q;
cin >> q;
while (q--) {
int s;
cin >> s;
for (int i = n; i >= 0; --i) {
if (dp[1][i][0] <= s || dp[1][i][1] <= s || dp[1][i][2] <= s) {
cout << i << '\n';
break;
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24020 KB |
Output is correct |
2 |
Correct |
18 ms |
24088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24020 KB |
Output is correct |
2 |
Correct |
18 ms |
24088 KB |
Output is correct |
3 |
Correct |
76 ms |
25052 KB |
Output is correct |
4 |
Correct |
76 ms |
25012 KB |
Output is correct |
5 |
Correct |
191 ms |
24608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
1824 KB |
Output is correct |
2 |
Incorrect |
46 ms |
1492 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24020 KB |
Output is correct |
2 |
Correct |
18 ms |
24088 KB |
Output is correct |
3 |
Correct |
76 ms |
25052 KB |
Output is correct |
4 |
Correct |
76 ms |
25012 KB |
Output is correct |
5 |
Correct |
191 ms |
24608 KB |
Output is correct |
6 |
Correct |
29 ms |
1824 KB |
Output is correct |
7 |
Incorrect |
46 ms |
1492 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |