답안 #1097777

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1097777 2024-10-08T09:11:46 Z TrinhKhanhDung Toll (BOI17_toll) C++14
100 / 100
164 ms 75912 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

struct Matrix{
    vector<vector<ll>> a;
    int n, m;

    Matrix(int _n = 0, int _m = 0, ll c = 0){
        n = _n;
        m = _m;
        a.resize(n, vector<ll>(m, c));
    }

    Matrix operator * (const Matrix &p) const{
        Matrix ans(n, p.m, oo);
        for(int i = 0; i < n; i++){
            for(int j = 0; j < p.m; j++){
                for(int k = 0; k < p.n; k++){
                    minimize(ans.a[i][j], a[i][k] + p.a[k][j]);
                }
            }
        }
        return ans;
    }
};

const int maxN = 5e4 + 10, LOG = 16;

int K, N, M, Q;
Matrix dist[maxN][LOG];

void solve(){
    cin >> K >> N >> M >> Q;
    for(int i = 0; i <= (N - 1) / K; i++){
        dist[i][0] = Matrix(K, K, oo);
    }
    for(int i = 1; i <= M; i++){
        int u, v, c; cin >> u >> v >> c;
        dist[u / K][0].a[u % K][v % K] = c;
    }

    clock_t start = clock();

    for(int j = 1; j < LOG; j++){
        for(int i = 0; i + MASK(j) <= (N - 1) / K; i++){
            dist[i][j] = dist[i][j - 1] * dist[i + MASK(j - 1)][j - 1];
        }
    }

    while(Q--){
        int u, v; cin >> u >> v;
        if(u == v){
            cout << 0 << '\n';
            continue;
        }
        if(u / K >= v / K){
            cout << -1 << '\n';
            continue;
        }
        int delta = v / K - u / K;
        Matrix cur(K, K, oo);
        cur.a[u % K][u % K] = 0;
        int pos = u / K;
        for(int i = 0; i < LOG; i++) if(BIT(delta, i)){
            cur = cur * dist[pos][i];
            pos += MASK(i);
        }

        ll ans = cur.a[u % K][v % K];
        if(ans >= oo) ans = -1;
        cout << ans << '\n';
    }

    cerr << "Time elapsed: " << clock() - start << " ms\n";
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("walk.inp","r",stdin);
//    freopen("walk.out","w",stdout);

    int t = 1;
//    cin >> t;
    while(t--)
        solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 72272 KB Output is correct
2 Correct 12 ms 25436 KB Output is correct
3 Correct 11 ms 25432 KB Output is correct
4 Correct 12 ms 25436 KB Output is correct
5 Correct 13 ms 25880 KB Output is correct
6 Correct 13 ms 25948 KB Output is correct
7 Correct 13 ms 25960 KB Output is correct
8 Correct 114 ms 72328 KB Output is correct
9 Correct 111 ms 72264 KB Output is correct
10 Correct 109 ms 71504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 69972 KB Output is correct
2 Correct 11 ms 25436 KB Output is correct
3 Correct 11 ms 25436 KB Output is correct
4 Correct 10 ms 25500 KB Output is correct
5 Correct 11 ms 25472 KB Output is correct
6 Correct 10 ms 25436 KB Output is correct
7 Correct 17 ms 26160 KB Output is correct
8 Correct 18 ms 25944 KB Output is correct
9 Correct 102 ms 72272 KB Output is correct
10 Correct 129 ms 65104 KB Output is correct
11 Correct 96 ms 69956 KB Output is correct
12 Correct 104 ms 64084 KB Output is correct
13 Correct 93 ms 52820 KB Output is correct
14 Correct 72 ms 48208 KB Output is correct
15 Correct 87 ms 51656 KB Output is correct
16 Correct 83 ms 51792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 25436 KB Output is correct
2 Correct 11 ms 25512 KB Output is correct
3 Correct 11 ms 25436 KB Output is correct
4 Correct 11 ms 25344 KB Output is correct
5 Correct 16 ms 25432 KB Output is correct
6 Correct 13 ms 25948 KB Output is correct
7 Correct 12 ms 26036 KB Output is correct
8 Correct 13 ms 25904 KB Output is correct
9 Correct 13 ms 25948 KB Output is correct
10 Correct 95 ms 72184 KB Output is correct
11 Correct 94 ms 69688 KB Output is correct
12 Correct 117 ms 64852 KB Output is correct
13 Correct 117 ms 65108 KB Output is correct
14 Correct 108 ms 64596 KB Output is correct
15 Correct 73 ms 51796 KB Output is correct
16 Correct 71 ms 51792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 25436 KB Output is correct
2 Correct 11 ms 25512 KB Output is correct
3 Correct 11 ms 25436 KB Output is correct
4 Correct 11 ms 25344 KB Output is correct
5 Correct 16 ms 25432 KB Output is correct
6 Correct 13 ms 25948 KB Output is correct
7 Correct 12 ms 26036 KB Output is correct
8 Correct 13 ms 25904 KB Output is correct
9 Correct 13 ms 25948 KB Output is correct
10 Correct 95 ms 72184 KB Output is correct
11 Correct 94 ms 69688 KB Output is correct
12 Correct 117 ms 64852 KB Output is correct
13 Correct 117 ms 65108 KB Output is correct
14 Correct 108 ms 64596 KB Output is correct
15 Correct 73 ms 51796 KB Output is correct
16 Correct 71 ms 51792 KB Output is correct
17 Correct 98 ms 69704 KB Output is correct
18 Correct 12 ms 25436 KB Output is correct
19 Correct 12 ms 25276 KB Output is correct
20 Correct 12 ms 25432 KB Output is correct
21 Correct 12 ms 25436 KB Output is correct
22 Correct 12 ms 25316 KB Output is correct
23 Correct 14 ms 25948 KB Output is correct
24 Correct 15 ms 25832 KB Output is correct
25 Correct 18 ms 25948 KB Output is correct
26 Correct 17 ms 25992 KB Output is correct
27 Correct 102 ms 72108 KB Output is correct
28 Correct 125 ms 64924 KB Output is correct
29 Correct 121 ms 65296 KB Output is correct
30 Correct 119 ms 64596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 72272 KB Output is correct
2 Correct 12 ms 25436 KB Output is correct
3 Correct 11 ms 25432 KB Output is correct
4 Correct 12 ms 25436 KB Output is correct
5 Correct 13 ms 25880 KB Output is correct
6 Correct 13 ms 25948 KB Output is correct
7 Correct 13 ms 25960 KB Output is correct
8 Correct 114 ms 72328 KB Output is correct
9 Correct 111 ms 72264 KB Output is correct
10 Correct 109 ms 71504 KB Output is correct
11 Correct 94 ms 69972 KB Output is correct
12 Correct 11 ms 25436 KB Output is correct
13 Correct 11 ms 25436 KB Output is correct
14 Correct 10 ms 25500 KB Output is correct
15 Correct 11 ms 25472 KB Output is correct
16 Correct 10 ms 25436 KB Output is correct
17 Correct 17 ms 26160 KB Output is correct
18 Correct 18 ms 25944 KB Output is correct
19 Correct 102 ms 72272 KB Output is correct
20 Correct 129 ms 65104 KB Output is correct
21 Correct 96 ms 69956 KB Output is correct
22 Correct 104 ms 64084 KB Output is correct
23 Correct 93 ms 52820 KB Output is correct
24 Correct 72 ms 48208 KB Output is correct
25 Correct 87 ms 51656 KB Output is correct
26 Correct 83 ms 51792 KB Output is correct
27 Correct 12 ms 25436 KB Output is correct
28 Correct 11 ms 25512 KB Output is correct
29 Correct 11 ms 25436 KB Output is correct
30 Correct 11 ms 25344 KB Output is correct
31 Correct 16 ms 25432 KB Output is correct
32 Correct 13 ms 25948 KB Output is correct
33 Correct 12 ms 26036 KB Output is correct
34 Correct 13 ms 25904 KB Output is correct
35 Correct 13 ms 25948 KB Output is correct
36 Correct 95 ms 72184 KB Output is correct
37 Correct 94 ms 69688 KB Output is correct
38 Correct 117 ms 64852 KB Output is correct
39 Correct 117 ms 65108 KB Output is correct
40 Correct 108 ms 64596 KB Output is correct
41 Correct 73 ms 51796 KB Output is correct
42 Correct 71 ms 51792 KB Output is correct
43 Correct 98 ms 69704 KB Output is correct
44 Correct 12 ms 25436 KB Output is correct
45 Correct 12 ms 25276 KB Output is correct
46 Correct 12 ms 25432 KB Output is correct
47 Correct 12 ms 25436 KB Output is correct
48 Correct 12 ms 25316 KB Output is correct
49 Correct 14 ms 25948 KB Output is correct
50 Correct 15 ms 25832 KB Output is correct
51 Correct 18 ms 25948 KB Output is correct
52 Correct 17 ms 25992 KB Output is correct
53 Correct 102 ms 72108 KB Output is correct
54 Correct 125 ms 64924 KB Output is correct
55 Correct 121 ms 65296 KB Output is correct
56 Correct 119 ms 64596 KB Output is correct
57 Correct 164 ms 75912 KB Output is correct
58 Correct 138 ms 72252 KB Output is correct
59 Correct 104 ms 69944 KB Output is correct