답안 #882046

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
882046 2023-12-02T13:46:09 Z Karoot Toll (BOI17_toll) C++17
100 / 100
171 ms 56884 KB
#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>

#define all(x)  (x).begin(), (x).end()
#define rall(x)  (x).rbegin(), (x).rend()

using namespace std;

typedef long long ll;

ll linf = 1e15+1;

inline void scoobydoobydoo(){
    ios::sync_with_stdio(false);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
}

const int MAXN = 5e4+7;
ll shortestPath[MAXN][20][6]; //node, binaryjump, what nodi? 0 if not possible
vector<pair<ll, ll>> adj[MAXN];
ll bestAt[MAXN];
ll k, n, m, q; 

void initShortest(){
    for (int i = 0; i < MAXN; i++){
        for (auto& e : adj[i]){
            int node = e.first%k;
            int w = e.second;
            shortestPath[i][0][node] = w;
        }
    }
    for (int j = 1; j < 20; j++){
        for (int i = 0; i < n; i++){
            for (int target = 0; target < k; target++){
                ll best = 200000000000;
                for (int l = 0; l < k; l++){
                    if (shortestPath[i][j-1][l] != 0 && shortestPath[(i/k+(1<<(j-1)))*k+l][j-1][target] != 0){
                        best = min(best, shortestPath[(i/k+(1<<(j-1)))*k+l][j-1][target]+shortestPath[i][j-1][l]);
                    }
                }
                shortestPath[i][j][target] = (best < 10000000000 ? best : 0);
            }
        }
    }

}

vector<ll> needClear;

ll findBest(int node, int jumpDist, int target){
    //cout << node << " " << jumpDist << " " << target << endl;
    if (jumpDist == 0)return 100000000000*(node != target);
    //cout << "here ";
    if (bestAt[node] != 0)return bestAt[node];
    needClear.push_back(node);
    //cout << "there" << endl;
    for (int i = 18; i >= 0; i--){
        if (jumpDist&(1<<i)){   
            //cout << i << endl;
            ll best = 1000000000000;
            for (int j = 0; j < k; j++){
                if (shortestPath[node][i][j]){
                    best = min(best, findBest((((node/k)+(1<<i))*k)+j, jumpDist^(1<<i), target)+shortestPath[node][i][j]);
                }
            }
            //cout << best << " " << node << endl;
            return bestAt[node] = best;
        }
    }
    return bestAt[node] = 1000000000000;
}


int main(){
    scoobydoobydoo();
    //freopen("toll.in", "r", stdin);
    //freopen("toll.out", "w", stdout);
    cin >> k >> n >> m >> q;
    for (int i = 0; i < m; i++){
        int a, b, t; cin >> a >> b >> t;
        adj[a].push_back({b, t});
    }
    initShortest();
    vector<ll> ans;

    while (q--){
        int a, b; cin >> a >> b;
        int depthA = a/k;
        int depthB = b/k;
        if (a == b){
            ans.push_back(0);
            continue;
        }
        if (depthA >= depthB){
            ans.push_back(-1);
            continue;
        }
        int depthDiff = depthB-depthA;

        /*for (int i = 0; i < n; i++){
            cout << bestAt[i] << " ";
        }
        cout << endl;*/

        ll ret = findBest(a, depthDiff, b);
        ans.push_back((ret < 10000000000 ? ret : -1));

        for (ll x : needClear){
            bestAt[x] = 0;
        }
        needClear.clear();
    }


    /*for (int i = 0; i < n; i++){
        cout << i << "___: " << endl;
        for (int j = 0; j < 4; j++){
            cout << j << ": ";
            for (int l = 0; l < k; l++){
                cout << shortestPath[i][j][l] << " ";
            }
            cout << endl;
        }
        -1
        3324
        7419
        7419
        6681
    }*/

    for (auto& e : ans){
        cout << e << endl;
    }



    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 51600 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 3 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 3 ms 4572 KB Output is correct
8 Correct 42 ms 51540 KB Output is correct
9 Correct 37 ms 51544 KB Output is correct
10 Correct 37 ms 49232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 53228 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 13 ms 4700 KB Output is correct
8 Correct 15 ms 4988 KB Output is correct
9 Correct 37 ms 51284 KB Output is correct
10 Correct 101 ms 55380 KB Output is correct
11 Correct 66 ms 53544 KB Output is correct
12 Correct 62 ms 52364 KB Output is correct
13 Correct 113 ms 38352 KB Output is correct
14 Correct 55 ms 35408 KB Output is correct
15 Correct 88 ms 34676 KB Output is correct
16 Correct 90 ms 34640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 4576 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 3 ms 4556 KB Output is correct
9 Correct 2 ms 4700 KB Output is correct
10 Correct 28 ms 51280 KB Output is correct
11 Correct 50 ms 52816 KB Output is correct
12 Correct 86 ms 54868 KB Output is correct
13 Correct 88 ms 55632 KB Output is correct
14 Correct 77 ms 53776 KB Output is correct
15 Correct 83 ms 34432 KB Output is correct
16 Correct 87 ms 34404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 4576 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 3 ms 4556 KB Output is correct
9 Correct 2 ms 4700 KB Output is correct
10 Correct 28 ms 51280 KB Output is correct
11 Correct 50 ms 52816 KB Output is correct
12 Correct 86 ms 54868 KB Output is correct
13 Correct 88 ms 55632 KB Output is correct
14 Correct 77 ms 53776 KB Output is correct
15 Correct 83 ms 34432 KB Output is correct
16 Correct 87 ms 34404 KB Output is correct
17 Correct 55 ms 52992 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2512 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 5 ms 4700 KB Output is correct
24 Correct 6 ms 4720 KB Output is correct
25 Correct 8 ms 4696 KB Output is correct
26 Correct 7 ms 4696 KB Output is correct
27 Correct 30 ms 51292 KB Output is correct
28 Correct 92 ms 55036 KB Output is correct
29 Correct 96 ms 55856 KB Output is correct
30 Correct 82 ms 53844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 49 ms 51600 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 3 ms 4440 KB Output is correct
6 Correct 2 ms 4444 KB Output is correct
7 Correct 3 ms 4572 KB Output is correct
8 Correct 42 ms 51540 KB Output is correct
9 Correct 37 ms 51544 KB Output is correct
10 Correct 37 ms 49232 KB Output is correct
11 Correct 62 ms 53228 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 13 ms 4700 KB Output is correct
18 Correct 15 ms 4988 KB Output is correct
19 Correct 37 ms 51284 KB Output is correct
20 Correct 101 ms 55380 KB Output is correct
21 Correct 66 ms 53544 KB Output is correct
22 Correct 62 ms 52364 KB Output is correct
23 Correct 113 ms 38352 KB Output is correct
24 Correct 55 ms 35408 KB Output is correct
25 Correct 88 ms 34676 KB Output is correct
26 Correct 90 ms 34640 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 1 ms 2396 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 1 ms 4576 KB Output is correct
33 Correct 2 ms 4444 KB Output is correct
34 Correct 3 ms 4556 KB Output is correct
35 Correct 2 ms 4700 KB Output is correct
36 Correct 28 ms 51280 KB Output is correct
37 Correct 50 ms 52816 KB Output is correct
38 Correct 86 ms 54868 KB Output is correct
39 Correct 88 ms 55632 KB Output is correct
40 Correct 77 ms 53776 KB Output is correct
41 Correct 83 ms 34432 KB Output is correct
42 Correct 87 ms 34404 KB Output is correct
43 Correct 55 ms 52992 KB Output is correct
44 Correct 1 ms 2396 KB Output is correct
45 Correct 1 ms 2396 KB Output is correct
46 Correct 1 ms 2512 KB Output is correct
47 Correct 1 ms 2396 KB Output is correct
48 Correct 1 ms 2396 KB Output is correct
49 Correct 5 ms 4700 KB Output is correct
50 Correct 6 ms 4720 KB Output is correct
51 Correct 8 ms 4696 KB Output is correct
52 Correct 7 ms 4696 KB Output is correct
53 Correct 30 ms 51292 KB Output is correct
54 Correct 92 ms 55036 KB Output is correct
55 Correct 96 ms 55856 KB Output is correct
56 Correct 82 ms 53844 KB Output is correct
57 Correct 171 ms 56884 KB Output is correct
58 Correct 39 ms 51664 KB Output is correct
59 Correct 73 ms 53584 KB Output is correct