# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1135283 | draguk | Museum (CEOI17_museum) | C++20 | 477 ms | 784980 KiB |
#include <bits/stdc++.h>
using namespace std;
const int MAX_NODES = 10005;
int dp[MAX_NODES][MAX_NODES][2], subtree_size[MAX_NODES];
vector<pair<int, int>> adjacency_list[MAX_NODES];
void depth_first_search(int current_node, int parent_node = -1) {
dp[current_node][0][0] = dp[current_node][1][0] = dp[current_node][1][1] = dp[current_node][0][1] = 0;
subtree_size[current_node] = 1;
for (auto [neighbor, weight] : adjacency_list[current_node]) {
if (neighbor == parent_node) continue;
depth_first_search(neighbor, current_node);
for (int i = subtree_size[current_node]; i >= 1; --i) {
for (int j = subtree_size[neighbor]; j >= 1; --j) {
dp[current_node][i + j][0] = min(dp[current_node][i + j][0], dp[current_node][i][0] + dp[neighbor][j][0] + 2 * weight);
dp[current_node][i + j][1] = min(dp[current_node][i + j][1], dp[current_node][i][1] + dp[neighbor][j][0] + 2 * weight);
dp[current_node][i + j][1] = min(dp[current_node][i + j][1], dp[current_node][i][0] + dp[neighbor][j][1] + weight);
}
}
subtree_size[current_node] += subtree_size[neighbor];
}
}
int main() {
if (fopen("file.inp", "r")) {
freopen("file.inp", "r", stdin);
freopen("file.out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int num_nodes, num_rooms_to_visit, start_node;
cin >> num_nodes >> num_rooms_to_visit >> start_node;
for (int i = 1; i < num_nodes; ++i) {
int node_u, node_v, weight;
cin >> node_u >> node_v >> weight;
adjacency_list[node_u].push_back({node_v, weight});
adjacency_list[node_v].push_back({node_u, weight});
}
memset(dp, 0x3f, sizeof(dp));
depth_first_search(start_node);
cout << min(dp[start_node][num_rooms_to_visit][0], dp[start_node][num_rooms_to_visit][1]) << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |