Submission #1106508

# Submission time Handle Problem Language Result Execution time Memory
1106508 2024-10-30T13:41:32 Z Neco_arc Museum (CEOI17_museum) C++17
100 / 100
478 ms 310436 KB
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "migration"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 1e4 + 7

using namespace std;

const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
    return uniform_int_distribution<ll> (l, r)(rng);
}

int n, k, rt;
vector<ll> dp[maxn][2];
vector<pair<int, int>> edges[maxn];

vector<ll> Min(vector<ll> &A, vector<ll> &B) {
    while(A.size() < B.size()) A.push_back(1e18);
    fi(i, 0, B.size() - 1) A[i] = min(A[i], B[i]);
    return A;
}

vector<ll> cb(const vector<ll> &A, const vector<ll> &B, ll w) {
    vector<ll> ans(A.size() + B.size() - 1, 1e18);
    fi(i, 0, A.size() - 1) fi(j, 0, B.size() - 1) {
        ans[i + j] = min(ans[i + j], A[i] + B[j] + (j > 0) * w);
    }
    return ans;
}

void dfs(int u, int par) {
    fi(i, 0, 1) dp[u][i] = {0, 0};

    for(auto [v, w] : edges[u]) if(v != par) {
        dfs(v, u);
        vector<ll> tmp1 = cb(dp[u][0], dp[v][1], w);
        vector<ll> tmp2 = cb(dp[u][1], dp[v][0], 2 * w);

        dp[u][1] = Min(tmp1, tmp2);
        dp[u][0] = cb(dp[u][0], dp[v][0], 2 * w);
    }

}

void solve()
{

    cin >> n >> k >> rt;
    fi(i, 1, n - 1) {
        int x, y, w; cin >> x >> y >> w;
        edges[x].push_back({y, w}), edges[y].push_back({x, w});
    }

    dfs(rt, 0);
//    fi(i, 1, n) cout << dp[1][1][i] << "\n";
    cout << dp[rt][1][k];
}


int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if(fopen(Neco".inp", "r")) {
        freopen(Neco".inp", "r", stdin);
        freopen(Neco".out", "w", stdout);
    }

    int nTest = 1;
//    cin >> nTest;

    while(nTest--)
    {
        solve();
    }


    return 0;
}

Compilation message

museum.cpp: In function 'std::vector<long long int> Min(std::vector<long long int>&, std::vector<long long int>&)':
museum.cpp:12:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define fi(i, a, b) for(int i = a; i <= b; i++)
......
   32 |     fi(i, 0, B.size() - 1) A[i] = min(A[i], B[i]);
      |        ~~~~~~~~~~~~~~~~~~             
museum.cpp:32:5: note: in expansion of macro 'fi'
   32 |     fi(i, 0, B.size() - 1) A[i] = min(A[i], B[i]);
      |     ^~
museum.cpp: In function 'std::vector<long long int> cb(const std::vector<long long int>&, const std::vector<long long int>&, long long int)':
museum.cpp:12:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define fi(i, a, b) for(int i = a; i <= b; i++)
......
   38 |     fi(i, 0, A.size() - 1) fi(j, 0, B.size() - 1) {
      |        ~~~~~~~~~~~~~~~~~~             
museum.cpp:38:5: note: in expansion of macro 'fi'
   38 |     fi(i, 0, A.size() - 1) fi(j, 0, B.size() - 1) {
      |     ^~
museum.cpp:12:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 | #define fi(i, a, b) for(int i = a; i <= b; i++)
......
   38 |     fi(i, 0, A.size() - 1) fi(j, 0, B.size() - 1) {
      |                               ~~~~~~~~~~~~~~~~~~
museum.cpp:38:28: note: in expansion of macro 'fi'
   38 |     fi(i, 0, A.size() - 1) fi(j, 0, B.size() - 1) {
      |                            ^~
museum.cpp: In function 'int main()':
museum.cpp:81:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         freopen(Neco".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
museum.cpp:82:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         freopen(Neco".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1104 KB Output is correct
2 Correct 1 ms 1104 KB Output is correct
3 Correct 1 ms 1104 KB Output is correct
4 Correct 1 ms 1104 KB Output is correct
5 Correct 1 ms 1104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 5960 KB Output is correct
2 Correct 155 ms 6876 KB Output is correct
3 Correct 444 ms 310436 KB Output is correct
4 Correct 232 ms 96996 KB Output is correct
5 Correct 171 ms 24392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 142 ms 5960 KB Output is correct
2 Correct 155 ms 6876 KB Output is correct
3 Correct 444 ms 310436 KB Output is correct
4 Correct 232 ms 96996 KB Output is correct
5 Correct 171 ms 24392 KB Output is correct
6 Correct 146 ms 4780 KB Output is correct
7 Correct 346 ms 180808 KB Output is correct
8 Correct 469 ms 2964 KB Output is correct
9 Correct 346 ms 3400 KB Output is correct
10 Correct 178 ms 4488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1104 KB Output is correct
2 Correct 1 ms 1104 KB Output is correct
3 Correct 1 ms 1104 KB Output is correct
4 Correct 1 ms 1104 KB Output is correct
5 Correct 1 ms 1104 KB Output is correct
6 Correct 142 ms 5960 KB Output is correct
7 Correct 155 ms 6876 KB Output is correct
8 Correct 444 ms 310436 KB Output is correct
9 Correct 232 ms 96996 KB Output is correct
10 Correct 171 ms 24392 KB Output is correct
11 Correct 146 ms 4780 KB Output is correct
12 Correct 346 ms 180808 KB Output is correct
13 Correct 469 ms 2964 KB Output is correct
14 Correct 346 ms 3400 KB Output is correct
15 Correct 178 ms 4488 KB Output is correct
16 Correct 146 ms 7496 KB Output is correct
17 Correct 148 ms 7112 KB Output is correct
18 Correct 277 ms 117152 KB Output is correct
19 Correct 380 ms 3068 KB Output is correct
20 Correct 153 ms 5056 KB Output is correct
21 Correct 328 ms 171324 KB Output is correct
22 Correct 165 ms 6472 KB Output is correct
23 Correct 370 ms 3092 KB Output is correct
24 Correct 154 ms 4868 KB Output is correct
25 Correct 478 ms 307444 KB Output is correct