제출 #1135283

#제출 시각아이디문제언어결과실행 시간메모리
1135283dragukMuseum (CEOI17_museum)C++20
100 / 100
477 ms784980 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) 메시지

museum.cpp: In function 'int main()':
museum.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         freopen("file.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
museum.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen("file.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...