Submission #115135

# Submission time Handle Problem Language Result Execution time Memory
115135 2019-06-05T11:36:47 Z oolimry Race (IOI11_race) C++14
100 / 100
820 ms 79552 KB
#include "race.h"
#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> ii;

static vector<ii> adj[200005];
static bool vis[200005];
unordered_map<int,int> dp[200005];
ii offset[200005]; ///values in dp[u] are actually offset[u] more than what is in the maps, first->first, second->second
int k;
int ans = 102345678;
void dfs(int u){
    vis[u] = true;
    for(int i = 0;i < adj[u].size();i++){
        int v = adj[u][i].first;
        int w = adj[u][i].second;

        if(vis[v]) continue;
        dfs(v);
        vector<ii> updates;
        bool isSwap = false;
        if(dp[v].size() > dp[u].size()){
            isSwap = true;
            swap(dp[u],dp[v]);
            swap(offset[u], offset[v]);
            offset[u].first += w;
            offset[u].second++;
        }
        for(ii x : dp[v]){

            int value = x.first + offset[v].first; ///w/o offsets
            int cost = x.second + offset[v].second; ///w/o offsets
            if(!isSwap){
                value += w;
                cost++;
            }
            //if(i == 0) cout << value << " " << cost << " " << offset[v].second << "\n";
            if(value <= k){
                if(dp[u].find(k - value - offset[u].first) != dp[u].end()){
                    ans = min(ans, dp[u][k - value - offset[u].first] + offset[u].second + cost);
                }
                updates.push_back({value,cost});
            }
        }
        dp[v].clear();

        for(ii x : updates){
            if(dp[u].find(x.first - offset[u].first) == dp[u].end()){
                dp[u][x.first - offset[u].first] = x.second - offset[u].second;
            }
            else{
                dp[u][x.first - offset[u].first] = min(x.second - offset[u].second,dp[u][x.first - offset[u].first]);
            }
        }
    }
    /*
    cout << u << " " << offset[u].first << " " << offset[u].second << ": ";
    for(ii x : dp[u]){
        cout << "(" << x.first + offset[u].first << ", " << x.second + offset[u].second << ") ";
    }
    cout << "\n";
    */
}
int best_path(int N, int K, int H[][2], int L[])
{
    k = K;
    int n = N;
    for(int i = 0;i < n;i++) dp[i][0] = 0;
    for(int i = 0;i < n-1;i++){
        int a = H[i][0];
        int b = H[i][1];
        int c = L[i];
        adj[a].push_back({b,c});
        adj[b].push_back({a,c});
    }

    dfs(0);
    if(ans > n) return -1;
    return ans;
}

Compilation message

race.cpp: In function 'void dfs(int)':
race.cpp:16:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 0;i < adj[u].size();i++){
                   ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 16000 KB Output is correct
2 Correct 15 ms 16000 KB Output is correct
3 Correct 15 ms 16000 KB Output is correct
4 Correct 15 ms 16000 KB Output is correct
5 Correct 15 ms 15992 KB Output is correct
6 Correct 15 ms 16000 KB Output is correct
7 Correct 15 ms 16000 KB Output is correct
8 Correct 15 ms 16080 KB Output is correct
9 Correct 15 ms 16000 KB Output is correct
10 Correct 15 ms 16048 KB Output is correct
11 Correct 15 ms 16000 KB Output is correct
12 Correct 14 ms 16000 KB Output is correct
13 Correct 15 ms 16000 KB Output is correct
14 Correct 15 ms 16000 KB Output is correct
15 Correct 15 ms 16120 KB Output is correct
16 Correct 15 ms 16000 KB Output is correct
17 Correct 16 ms 16000 KB Output is correct
18 Correct 15 ms 16000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 16000 KB Output is correct
2 Correct 15 ms 16000 KB Output is correct
3 Correct 15 ms 16000 KB Output is correct
4 Correct 15 ms 16000 KB Output is correct
5 Correct 15 ms 15992 KB Output is correct
6 Correct 15 ms 16000 KB Output is correct
7 Correct 15 ms 16000 KB Output is correct
8 Correct 15 ms 16080 KB Output is correct
9 Correct 15 ms 16000 KB Output is correct
10 Correct 15 ms 16048 KB Output is correct
11 Correct 15 ms 16000 KB Output is correct
12 Correct 14 ms 16000 KB Output is correct
13 Correct 15 ms 16000 KB Output is correct
14 Correct 15 ms 16000 KB Output is correct
15 Correct 15 ms 16120 KB Output is correct
16 Correct 15 ms 16000 KB Output is correct
17 Correct 16 ms 16000 KB Output is correct
18 Correct 15 ms 16000 KB Output is correct
19 Correct 14 ms 16000 KB Output is correct
20 Correct 15 ms 16000 KB Output is correct
21 Correct 17 ms 16228 KB Output is correct
22 Correct 16 ms 16128 KB Output is correct
23 Correct 16 ms 16128 KB Output is correct
24 Correct 18 ms 16256 KB Output is correct
25 Correct 16 ms 16256 KB Output is correct
26 Correct 16 ms 16128 KB Output is correct
27 Correct 16 ms 16128 KB Output is correct
28 Correct 16 ms 16128 KB Output is correct
29 Correct 16 ms 16128 KB Output is correct
30 Correct 16 ms 16128 KB Output is correct
31 Correct 16 ms 16128 KB Output is correct
32 Correct 16 ms 16128 KB Output is correct
33 Correct 17 ms 16128 KB Output is correct
34 Correct 16 ms 16128 KB Output is correct
35 Correct 16 ms 16256 KB Output is correct
36 Correct 15 ms 16128 KB Output is correct
37 Correct 15 ms 16128 KB Output is correct
38 Correct 16 ms 16128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 16000 KB Output is correct
2 Correct 15 ms 16000 KB Output is correct
3 Correct 15 ms 16000 KB Output is correct
4 Correct 15 ms 16000 KB Output is correct
5 Correct 15 ms 15992 KB Output is correct
6 Correct 15 ms 16000 KB Output is correct
7 Correct 15 ms 16000 KB Output is correct
8 Correct 15 ms 16080 KB Output is correct
9 Correct 15 ms 16000 KB Output is correct
10 Correct 15 ms 16048 KB Output is correct
11 Correct 15 ms 16000 KB Output is correct
12 Correct 14 ms 16000 KB Output is correct
13 Correct 15 ms 16000 KB Output is correct
14 Correct 15 ms 16000 KB Output is correct
15 Correct 15 ms 16120 KB Output is correct
16 Correct 15 ms 16000 KB Output is correct
17 Correct 16 ms 16000 KB Output is correct
18 Correct 15 ms 16000 KB Output is correct
19 Correct 199 ms 29896 KB Output is correct
20 Correct 195 ms 29892 KB Output is correct
21 Correct 190 ms 29796 KB Output is correct
22 Correct 193 ms 29916 KB Output is correct
23 Correct 191 ms 29860 KB Output is correct
24 Correct 174 ms 30584 KB Output is correct
25 Correct 193 ms 37752 KB Output is correct
26 Correct 97 ms 46792 KB Output is correct
27 Correct 280 ms 41864 KB Output is correct
28 Correct 530 ms 79552 KB Output is correct
29 Correct 515 ms 77132 KB Output is correct
30 Correct 295 ms 41820 KB Output is correct
31 Correct 269 ms 41848 KB Output is correct
32 Correct 396 ms 41848 KB Output is correct
33 Correct 404 ms 42084 KB Output is correct
34 Correct 273 ms 39672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 16000 KB Output is correct
2 Correct 15 ms 16000 KB Output is correct
3 Correct 15 ms 16000 KB Output is correct
4 Correct 15 ms 16000 KB Output is correct
5 Correct 15 ms 15992 KB Output is correct
6 Correct 15 ms 16000 KB Output is correct
7 Correct 15 ms 16000 KB Output is correct
8 Correct 15 ms 16080 KB Output is correct
9 Correct 15 ms 16000 KB Output is correct
10 Correct 15 ms 16048 KB Output is correct
11 Correct 15 ms 16000 KB Output is correct
12 Correct 14 ms 16000 KB Output is correct
13 Correct 15 ms 16000 KB Output is correct
14 Correct 15 ms 16000 KB Output is correct
15 Correct 15 ms 16120 KB Output is correct
16 Correct 15 ms 16000 KB Output is correct
17 Correct 16 ms 16000 KB Output is correct
18 Correct 15 ms 16000 KB Output is correct
19 Correct 14 ms 16000 KB Output is correct
20 Correct 15 ms 16000 KB Output is correct
21 Correct 17 ms 16228 KB Output is correct
22 Correct 16 ms 16128 KB Output is correct
23 Correct 16 ms 16128 KB Output is correct
24 Correct 18 ms 16256 KB Output is correct
25 Correct 16 ms 16256 KB Output is correct
26 Correct 16 ms 16128 KB Output is correct
27 Correct 16 ms 16128 KB Output is correct
28 Correct 16 ms 16128 KB Output is correct
29 Correct 16 ms 16128 KB Output is correct
30 Correct 16 ms 16128 KB Output is correct
31 Correct 16 ms 16128 KB Output is correct
32 Correct 16 ms 16128 KB Output is correct
33 Correct 17 ms 16128 KB Output is correct
34 Correct 16 ms 16128 KB Output is correct
35 Correct 16 ms 16256 KB Output is correct
36 Correct 15 ms 16128 KB Output is correct
37 Correct 15 ms 16128 KB Output is correct
38 Correct 16 ms 16128 KB Output is correct
39 Correct 199 ms 29896 KB Output is correct
40 Correct 195 ms 29892 KB Output is correct
41 Correct 190 ms 29796 KB Output is correct
42 Correct 193 ms 29916 KB Output is correct
43 Correct 191 ms 29860 KB Output is correct
44 Correct 174 ms 30584 KB Output is correct
45 Correct 193 ms 37752 KB Output is correct
46 Correct 97 ms 46792 KB Output is correct
47 Correct 280 ms 41864 KB Output is correct
48 Correct 530 ms 79552 KB Output is correct
49 Correct 515 ms 77132 KB Output is correct
50 Correct 295 ms 41820 KB Output is correct
51 Correct 269 ms 41848 KB Output is correct
52 Correct 396 ms 41848 KB Output is correct
53 Correct 404 ms 42084 KB Output is correct
54 Correct 273 ms 39672 KB Output is correct
55 Correct 31 ms 17656 KB Output is correct
56 Correct 28 ms 17400 KB Output is correct
57 Correct 115 ms 28920 KB Output is correct
58 Correct 75 ms 27980 KB Output is correct
59 Correct 109 ms 47840 KB Output is correct
60 Correct 513 ms 78728 KB Output is correct
61 Correct 279 ms 42832 KB Output is correct
62 Correct 289 ms 42744 KB Output is correct
63 Correct 434 ms 42616 KB Output is correct
64 Correct 820 ms 52572 KB Output is correct
65 Correct 344 ms 40976 KB Output is correct
66 Correct 558 ms 71716 KB Output is correct
67 Correct 329 ms 40676 KB Output is correct
68 Correct 582 ms 45576 KB Output is correct
69 Correct 600 ms 48464 KB Output is correct
70 Correct 530 ms 44700 KB Output is correct