이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'
const int MXN = 10010;
int n, k;
vector<vector<pair<int, int>>> adj;
int dp[MXN+1][2][MXN+1];
int sub[MXN+1];
const int INF = 1e9;
void dfs(int p, int parent) {
sub[p] = 1;
dp[p][0][1] = dp[p][1][1] = 0;
for (auto i: adj[p]) if (i.first != parent) {
dfs(i.first, p);
for_(j, 1, sub[i.first]+1) dp[p][0][sub[p]+j] = dp[p][1][sub[p]+j] = INF;
for (int ct = sub[p]; ct >= 0; ct--) for (int t = sub[i.first]; t >= 1; t--) {
dp[p][1][ct+t] = min(dp[p][1][ct+t], dp[p][1][ct] + dp[i.first][1][t] + 2*i.second);
dp[p][0][ct+t] = min(dp[p][0][ct+t], dp[p][0][ct] + dp[i.first][1][t] + 2*i.second);
dp[p][0][ct+t] = min(dp[p][0][ct+t], dp[p][1][ct] + dp[i.first][0][t] + i.second);
}
sub[p] += sub[i.first];
}
}
int main() {
#ifndef ONLINE_JUDGE
//freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int x; cin >> n >> k >> x;
adj.resize(n);
for_(i, 0, n-1) {
int a, b, w; cin >> a >> b >> w;
a -= 1; b -= 1;
adj[a].push_back({b, w});
adj[b].push_back({a, w});
}
x -= 1;
dfs(x, x);
cout << dp[x][0][k] << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
museum.cpp:2: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
2 | #pragma GCC optimization ("O3")
|
museum.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |