Submission #382073

# Submission time Handle Problem Language Result Execution time Memory
382073 2021-03-26T11:06:53 Z rocks03 Toll (BOI17_toll) C++14
25 / 100
3000 ms 9192 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 1e18;
const int MAXN = 5e4+100;
int K, N, M, Q;
vector<pii> g[MAXN];

vector<int> toposort;
bool vis[MAXN];
void dfs(int v){
    vis[v] = true;
    for(auto [u, w] : g[v]){
        if(!vis[u])
            dfs(u);
    }
    toposort.pb(v);
}

void solve(){
    cin >> K >> N >> M >> Q;
    rep(m, 0, M){
        int a, b, t;
        cin >> a >> b >> t;
        g[a].pb({b, t}); 
    }
    rep(i, 0, N){
        if(!vis[i])
            dfs(i);
    }
    reverse(all(toposort));
    vector<ll> dp0(N, INF);
    dp0[0] = 0;
    for(int v : toposort){
        for(auto [u, w] : g[v]){
            dp0[u] = min(dp0[u], dp0[v] + w);
        }
    }
    vector<ll> k1(N, INF);
    vector<int> comp(N, -1);
    int szcomp = 0;
    for(int v : toposort){
        if(comp[v] == -1){
            comp[v] = szcomp++;
            k1[v] = 0;
        }
        for(auto [u, w] : g[v]){
            comp[u] = comp[v];
            k1[u] = k1[v] + w;
        }
    }
    rep(q, 0, Q){
        int a, b;
        cin >> a >> b;
        if(K == 1){
            if(comp[a] == comp[b] && k1[a] <= k1[b])
                cout << k1[b] - k1[a] << "\n";
            else
                cout << "-1\n";
            continue;
        }
        if(a == 0){
            if(dp0[b] == INF)
                cout << "-1\n";
            else
                cout << dp0[b] << "\n";
        } else{
            vector<ll> dp(N, INF);
            dp[a] = 0;
            for(int v : toposort){
                if(v == b) break;
                for(auto [u, w] : g[v]){
                    dp[u] = min(dp[u], dp[v] + w);
                }
            }
            if(dp[b] == INF)
                cout << "-1\n";
            else
                cout << dp[b] << "\n";
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    solve();
    return 0;
}

Compilation message

toll.cpp:17:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   17 | const int INF = 1e18;
      |                 ^~~~
toll.cpp: In function 'void dfs(int)':
toll.cpp:26:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   26 |     for(auto [u, w] : g[v]){
      |              ^
toll.cpp: In function 'void solve()':
toll.cpp:48:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   48 |         for(auto [u, w] : g[v]){
      |                  ^
toll.cpp:60:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   60 |         for(auto [u, w] : g[v]){
      |                  ^
toll.cpp:85:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   85 |                 for(auto [u, w] : g[v]){
      |                          ^
# Verdict Execution time Memory Grader output
1 Correct 31 ms 7788 KB Output is correct
2 Correct 2 ms 1516 KB Output is correct
3 Correct 2 ms 1516 KB Output is correct
4 Correct 2 ms 1516 KB Output is correct
5 Correct 4 ms 1644 KB Output is correct
6 Correct 2 ms 1644 KB Output is correct
7 Correct 3 ms 1644 KB Output is correct
8 Correct 40 ms 5488 KB Output is correct
9 Correct 28 ms 5232 KB Output is correct
10 Correct 6 ms 3056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 6772 KB Output is correct
2 Correct 2 ms 1516 KB Output is correct
3 Correct 2 ms 1516 KB Output is correct
4 Correct 2 ms 1516 KB Output is correct
5 Correct 2 ms 1516 KB Output is correct
6 Correct 2 ms 1516 KB Output is correct
7 Correct 4 ms 1644 KB Output is correct
8 Correct 5 ms 1772 KB Output is correct
9 Correct 30 ms 5532 KB Output is correct
10 Correct 83 ms 8424 KB Output is correct
11 Correct 54 ms 7404 KB Output is correct
12 Correct 46 ms 5616 KB Output is correct
13 Correct 61 ms 7660 KB Output is correct
14 Correct 40 ms 5868 KB Output is correct
15 Correct 34 ms 5100 KB Output is correct
16 Correct 35 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1516 KB Output is correct
2 Correct 2 ms 1516 KB Output is correct
3 Correct 2 ms 1516 KB Output is correct
4 Correct 2 ms 1516 KB Output is correct
5 Correct 2 ms 1516 KB Output is correct
6 Correct 2 ms 1644 KB Output is correct
7 Correct 4 ms 1644 KB Output is correct
8 Correct 5 ms 1644 KB Output is correct
9 Correct 4 ms 1644 KB Output is correct
10 Correct 26 ms 5488 KB Output is correct
11 Correct 138 ms 7532 KB Output is correct
12 Correct 208 ms 8680 KB Output is correct
13 Correct 229 ms 9192 KB Output is correct
14 Correct 173 ms 7912 KB Output is correct
15 Correct 95 ms 5376 KB Output is correct
16 Correct 92 ms 5324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1516 KB Output is correct
2 Correct 2 ms 1516 KB Output is correct
3 Correct 2 ms 1516 KB Output is correct
4 Correct 2 ms 1516 KB Output is correct
5 Correct 2 ms 1516 KB Output is correct
6 Correct 2 ms 1644 KB Output is correct
7 Correct 4 ms 1644 KB Output is correct
8 Correct 5 ms 1644 KB Output is correct
9 Correct 4 ms 1644 KB Output is correct
10 Correct 26 ms 5488 KB Output is correct
11 Correct 138 ms 7532 KB Output is correct
12 Correct 208 ms 8680 KB Output is correct
13 Correct 229 ms 9192 KB Output is correct
14 Correct 173 ms 7912 KB Output is correct
15 Correct 95 ms 5376 KB Output is correct
16 Correct 92 ms 5324 KB Output is correct
17 Correct 2814 ms 7740 KB Output is correct
18 Correct 2 ms 1516 KB Output is correct
19 Correct 2 ms 1516 KB Output is correct
20 Correct 2 ms 1516 KB Output is correct
21 Correct 2 ms 1516 KB Output is correct
22 Correct 2 ms 1516 KB Output is correct
23 Correct 4 ms 1644 KB Output is correct
24 Correct 35 ms 1644 KB Output is correct
25 Correct 51 ms 1772 KB Output is correct
26 Correct 45 ms 1772 KB Output is correct
27 Correct 29 ms 5488 KB Output is correct
28 Execution timed out 3078 ms 8800 KB Time limit exceeded
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 31 ms 7788 KB Output is correct
2 Correct 2 ms 1516 KB Output is correct
3 Correct 2 ms 1516 KB Output is correct
4 Correct 2 ms 1516 KB Output is correct
5 Correct 4 ms 1644 KB Output is correct
6 Correct 2 ms 1644 KB Output is correct
7 Correct 3 ms 1644 KB Output is correct
8 Correct 40 ms 5488 KB Output is correct
9 Correct 28 ms 5232 KB Output is correct
10 Correct 6 ms 3056 KB Output is correct
11 Correct 49 ms 6772 KB Output is correct
12 Correct 2 ms 1516 KB Output is correct
13 Correct 2 ms 1516 KB Output is correct
14 Correct 2 ms 1516 KB Output is correct
15 Correct 2 ms 1516 KB Output is correct
16 Correct 2 ms 1516 KB Output is correct
17 Correct 4 ms 1644 KB Output is correct
18 Correct 5 ms 1772 KB Output is correct
19 Correct 30 ms 5532 KB Output is correct
20 Correct 83 ms 8424 KB Output is correct
21 Correct 54 ms 7404 KB Output is correct
22 Correct 46 ms 5616 KB Output is correct
23 Correct 61 ms 7660 KB Output is correct
24 Correct 40 ms 5868 KB Output is correct
25 Correct 34 ms 5100 KB Output is correct
26 Correct 35 ms 5100 KB Output is correct
27 Correct 2 ms 1516 KB Output is correct
28 Correct 2 ms 1516 KB Output is correct
29 Correct 2 ms 1516 KB Output is correct
30 Correct 2 ms 1516 KB Output is correct
31 Correct 2 ms 1516 KB Output is correct
32 Correct 2 ms 1644 KB Output is correct
33 Correct 4 ms 1644 KB Output is correct
34 Correct 5 ms 1644 KB Output is correct
35 Correct 4 ms 1644 KB Output is correct
36 Correct 26 ms 5488 KB Output is correct
37 Correct 138 ms 7532 KB Output is correct
38 Correct 208 ms 8680 KB Output is correct
39 Correct 229 ms 9192 KB Output is correct
40 Correct 173 ms 7912 KB Output is correct
41 Correct 95 ms 5376 KB Output is correct
42 Correct 92 ms 5324 KB Output is correct
43 Correct 2814 ms 7740 KB Output is correct
44 Correct 2 ms 1516 KB Output is correct
45 Correct 2 ms 1516 KB Output is correct
46 Correct 2 ms 1516 KB Output is correct
47 Correct 2 ms 1516 KB Output is correct
48 Correct 2 ms 1516 KB Output is correct
49 Correct 4 ms 1644 KB Output is correct
50 Correct 35 ms 1644 KB Output is correct
51 Correct 51 ms 1772 KB Output is correct
52 Correct 45 ms 1772 KB Output is correct
53 Correct 29 ms 5488 KB Output is correct
54 Execution timed out 3078 ms 8800 KB Time limit exceeded
55 Halted 0 ms 0 KB -