#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 30013;
const ll LOGN = 21;
const ll MAXN = 1100;
#define int long long
vector<vector<int>> graph;
vector<int> D, dp[MAXN][3];
vector<int> add(const vector<int> &A, const vector<int> &B) {
vector<int> res(A.size() + B.size() - 1, (ll)1e16);
for (int i = 0; i < A.size(); i++) {
for (int j = 0; j < B.size(); j++)
res[i+j] = min(res[i+j], A[i] + B[j]);
}
return res;
}
vector<int> minimize(vector<int> A, const vector<int> &B) {
while (A.size() < B.size())
A.push_back((ll)1e16);
for (int i = 0; i < B.size(); i++)
A[i] = min(A[i], B[i]);
return A;
}
bool vis[MAXN];
int T = 0;
void dfs(int node) {
vis[node] = 1;
dp[node][0] = {0};
dp[node][1] = {(ll)1e16, D[node]};
for (auto u : graph[node]) {
if (vis[u])
continue ;
dfs(u);
dp[node][0] = add(dp[node][0], minimize(dp[u][0], dp[u][2]));
dp[node][2] = minimize(add(dp[node][1], minimize(dp[u][1], dp[u][2])),
add(dp[node][2], minimize(minimize(dp[u][0], dp[u][1]), dp[u][2])));
dp[node][1] = add(dp[node][1], dp[u][0]);
}
}
signed main() {
fast
int N, M, a, b;
cin >> N >> M;
graph = vector<vector<int>>(N+1, vector<int>());
D = vector<int>(N+1);
for (int i = 1; i <= N; i++)
cin >> D[i];
for (int i = 0; i < M; i++) {
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
vector<int> res = {0};
for (int i = 1; i <= N; i++) {
if (!vis[i]) {
dfs(i);
res = add(res, minimize(dp[i][0], dp[i][2]));
}
}
for (int i = (int)res.size() - 2; i >= 0; i--)
res[i] = min(res[i+1], res[i]);
int Q, q, val;
cin >> Q;
while (Q--) {
cin >> val;
q = (upper_bound(res.begin(), res.end(), val) - res.begin()) - 1;
cout << q << endl;
}
}
Compilation message
dzumbus.cpp: In function 'std::vector<long long int> add(const std::vector<long long int>&, const std::vector<long long int>&)':
dzumbus.cpp:18:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for (int i = 0; i < A.size(); i++) {
| ~~^~~~~~~~~~
dzumbus.cpp:19:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for (int j = 0; j < B.size(); j++)
| ~~^~~~~~~~~~
dzumbus.cpp: In function 'std::vector<long long int> minimize(std::vector<long long int>, const std::vector<long long int>&)':
dzumbus.cpp:28:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int i = 0; i < B.size(); i++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7772 KB |
Output is correct |
2 |
Correct |
10 ms |
10764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7772 KB |
Output is correct |
2 |
Correct |
10 ms |
10764 KB |
Output is correct |
3 |
Correct |
270 ms |
13380 KB |
Output is correct |
4 |
Correct |
257 ms |
11504 KB |
Output is correct |
5 |
Correct |
246 ms |
10036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
248 ms |
2676 KB |
Output is correct |
2 |
Correct |
236 ms |
2180 KB |
Output is correct |
3 |
Correct |
244 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7772 KB |
Output is correct |
2 |
Correct |
10 ms |
10764 KB |
Output is correct |
3 |
Correct |
270 ms |
13380 KB |
Output is correct |
4 |
Correct |
257 ms |
11504 KB |
Output is correct |
5 |
Correct |
246 ms |
10036 KB |
Output is correct |
6 |
Correct |
248 ms |
2676 KB |
Output is correct |
7 |
Correct |
236 ms |
2180 KB |
Output is correct |
8 |
Correct |
244 ms |
2648 KB |
Output is correct |
9 |
Correct |
241 ms |
3704 KB |
Output is correct |
10 |
Correct |
241 ms |
3372 KB |
Output is correct |
11 |
Correct |
243 ms |
3224 KB |
Output is correct |