Submission #675642

# Submission time Handle Problem Language Result Execution time Memory
675642 2022-12-27T14:35:20 Z browntoad Toll (BOI17_toll) C++14
100 / 100
162 ms 17768 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define RREP(i, n) for (int i=(n)-1; i>=0; i--)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())
#define SQ(x) (x)*(x)
#define f first
#define s second
#define pii pair<int, int>
// #define endl '\n'
#define pb push_back

const ll mod = 1e9+7;
const ll maxn = 5e4+5;
const ll inf = (1ll<<60);

ll pw(ll x, ll p){
    ll ret=1;
    while(p>0){
        if (p&1){
            ret*=x;
            ret%=mod;
        }
        x*=x;
        x%=mod;
        p>>=1;
    }
    return ret;
}
ll inv(ll x){
    return pw(x, mod-2);
}
int k, n, m, q; 
int dp[maxn][5][5];
struct quer{
    int a, b;
    int id;
};
int res[maxn];
vector<pii> graph[maxn], rgraph[maxn];
void run(int l, int r, vector<quer>& orders){
    if (SZ(orders) == 0) return;
    if (l==r){
        REP(i, SZ(orders)){
            if (orders[i].a!=orders[i].b||orders[i].a/k!=l){
                res[orders[i].id]=-1;
            }
            else res[orders[i].id]=0;
        }
        return;
    }
    int mid = (l+r)>>1;
    vector<quer> L, R;
    REP(j, k){
        REP(z, k){
            if (j==z) dp[mid][j][z]=0;
            else dp[mid][j][z]=inf;
        }
    }
    for (int i=mid-1; i>=l; i--){
        REP(y, k){
            REP(j, k){
                dp[i][j][y]=inf;
                REP(z, SZ(graph[i*k+j])){
                    dp[i][j][y]=min(dp[i][j][y], dp[i+1][graph[i*k+j][z].f%k][y]+graph[i*k+j][z].s);
                }
            }
        }
        
    }
    for (int i=mid+1; i<=r; i++){
        REP(y, k){
            REP(j, k){
                dp[i][j][y]=inf;
                REP(z, SZ(rgraph[i*k+j])){
                    dp[i][j][y]=min(dp[i][j][y], dp[i-1][rgraph[i*k+j][z].f%k][y]+rgraph[i*k+j][z].s);
                }
            }
        }
        
    }
    REP(i, SZ(orders)){
        if (orders[i].b<orders[i].a||!(orders[i].b/k<=r&&orders[i].a/k>=l)){
            res[orders[i].id]=-1;
            continue;
        }
        if (orders[i].b/k<=mid){
            L.pb(orders[i]);
        }
        else if (orders[i].a/k>mid){
            R.pb(orders[i]);
        }
        else{
            // cout<<l<<' '<<r<<' '<<orders[i].id<<endl;
            res[orders[i].id]=inf;
            REP(j, k){
                res[orders[i].id] = min(res[orders[i].id], dp[orders[i].a/k][orders[i].a%k][j]+dp[orders[i].b/k][orders[i].b%k][j]);
            }
            if (res[orders[i].id]==inf) res[orders[i].id]=-1;
        }
    }
    run(l, mid, L);
    run(mid+1, r, R);
}
signed main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin>>k>>n>>m>>q;
    REP(i, m){
        int u, v, w; cin>>u>>v>>w;
        graph[u].pb({v, w});
        rgraph[v].pb({u, w});
    }
    vector<quer> Q;
    REP(i, q){
        int a, b; cin>>a>>b;
        Q.pb({a, b, i});
    }
    int wo = (n-1)/k;
    run(0, wo, Q);
    REP(i, q){
        cout<<res[i]<<endl;
    }
}
/*
2
3 5
3 5
*/
# Verdict Execution time Memory Grader output
1 Correct 44 ms 17100 KB Output is correct
2 Correct 1 ms 2684 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 4 ms 2900 KB Output is correct
6 Correct 3 ms 2900 KB Output is correct
7 Correct 3 ms 2952 KB Output is correct
8 Correct 51 ms 17068 KB Output is correct
9 Correct 46 ms 16904 KB Output is correct
10 Correct 23 ms 13172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 81 ms 14700 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2680 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 15 ms 3540 KB Output is correct
8 Correct 17 ms 3496 KB Output is correct
9 Correct 50 ms 17076 KB Output is correct
10 Correct 119 ms 16520 KB Output is correct
11 Correct 94 ms 15144 KB Output is correct
12 Correct 58 ms 11856 KB Output is correct
13 Correct 91 ms 15804 KB Output is correct
14 Correct 49 ms 11496 KB Output is correct
15 Correct 40 ms 9200 KB Output is correct
16 Correct 41 ms 9180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 4 ms 2952 KB Output is correct
7 Correct 3 ms 2900 KB Output is correct
8 Correct 4 ms 3028 KB Output is correct
9 Correct 4 ms 2968 KB Output is correct
10 Correct 28 ms 16332 KB Output is correct
11 Correct 69 ms 13988 KB Output is correct
12 Correct 88 ms 15792 KB Output is correct
13 Correct 89 ms 17164 KB Output is correct
14 Correct 70 ms 14008 KB Output is correct
15 Correct 44 ms 8940 KB Output is correct
16 Correct 42 ms 8960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 4 ms 2952 KB Output is correct
7 Correct 3 ms 2900 KB Output is correct
8 Correct 4 ms 3028 KB Output is correct
9 Correct 4 ms 2968 KB Output is correct
10 Correct 28 ms 16332 KB Output is correct
11 Correct 69 ms 13988 KB Output is correct
12 Correct 88 ms 15792 KB Output is correct
13 Correct 89 ms 17164 KB Output is correct
14 Correct 70 ms 14008 KB Output is correct
15 Correct 44 ms 8940 KB Output is correct
16 Correct 42 ms 8960 KB Output is correct
17 Correct 79 ms 14308 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2684 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 6 ms 3156 KB Output is correct
24 Correct 7 ms 3076 KB Output is correct
25 Correct 8 ms 3156 KB Output is correct
26 Correct 7 ms 3156 KB Output is correct
27 Correct 35 ms 16508 KB Output is correct
28 Correct 106 ms 16088 KB Output is correct
29 Correct 125 ms 17208 KB Output is correct
30 Correct 80 ms 14256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 17100 KB Output is correct
2 Correct 1 ms 2684 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 4 ms 2900 KB Output is correct
6 Correct 3 ms 2900 KB Output is correct
7 Correct 3 ms 2952 KB Output is correct
8 Correct 51 ms 17068 KB Output is correct
9 Correct 46 ms 16904 KB Output is correct
10 Correct 23 ms 13172 KB Output is correct
11 Correct 81 ms 14700 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2680 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 15 ms 3540 KB Output is correct
18 Correct 17 ms 3496 KB Output is correct
19 Correct 50 ms 17076 KB Output is correct
20 Correct 119 ms 16520 KB Output is correct
21 Correct 94 ms 15144 KB Output is correct
22 Correct 58 ms 11856 KB Output is correct
23 Correct 91 ms 15804 KB Output is correct
24 Correct 49 ms 11496 KB Output is correct
25 Correct 40 ms 9200 KB Output is correct
26 Correct 41 ms 9180 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 4 ms 2952 KB Output is correct
33 Correct 3 ms 2900 KB Output is correct
34 Correct 4 ms 3028 KB Output is correct
35 Correct 4 ms 2968 KB Output is correct
36 Correct 28 ms 16332 KB Output is correct
37 Correct 69 ms 13988 KB Output is correct
38 Correct 88 ms 15792 KB Output is correct
39 Correct 89 ms 17164 KB Output is correct
40 Correct 70 ms 14008 KB Output is correct
41 Correct 44 ms 8940 KB Output is correct
42 Correct 42 ms 8960 KB Output is correct
43 Correct 79 ms 14308 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2684 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 6 ms 3156 KB Output is correct
50 Correct 7 ms 3076 KB Output is correct
51 Correct 8 ms 3156 KB Output is correct
52 Correct 7 ms 3156 KB Output is correct
53 Correct 35 ms 16508 KB Output is correct
54 Correct 106 ms 16088 KB Output is correct
55 Correct 125 ms 17208 KB Output is correct
56 Correct 80 ms 14256 KB Output is correct
57 Correct 162 ms 17768 KB Output is correct
58 Correct 47 ms 17036 KB Output is correct
59 Correct 86 ms 15060 KB Output is correct