Submission #245591

# Submission time Handle Problem Language Result Execution time Memory
245591 2020-07-06T22:22:47 Z Tc14 Toll (BOI17_toll) C++17
100 / 100
1434 ms 7928 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ve vector
typedef long long ll;

const int INF = 1e9;

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int k, n, m, o, a, b, t, rows, compRows, index;
    ve<ve<pair<int, int>>> Graph;
    ve<tuple<int, int, int, int>> DiffComp;
    ve<tuple<int, int, int>> SameComp;
    ve<int> Ans, T, TNew;
    ve<ve<int>> TF, TFNew, TB, TBNew;

    cin >> k >> n >> m >> o;
    Graph = ve<ve<pair<int, int>>>(n);
    Ans = ve<int>(o);

    if (n % k == 0) rows = n / k;
    else rows = n / k + 1;

    compRows = sqrt(rows);

    for (int i = 0; i < m; i++) {
        cin >> a >> b >> t;
        Graph[a].push_back({b, t});
    }

    for (int i = 0; i < o; i++) {
        cin >> a >> b;
        if (b >= a) {
            int ca = a / k / compRows;
            int cb = b / k / compRows;
            if (ca == cb) SameComp.push_back({a, b, i});
            else DiffComp.push_back({ca, b, a, i});
        }
    }

    for (tuple<int, int, int> p : SameComp) {
        tie(a, b, index) = p;
        int r = a / k;
        T = ve<int>(k, INF);
        T[a % k] = 0;

        while (r != b / k) {
            TNew = ve<int>(k, INF);
            for (int i = 0; i < k; i++) {
                if (T[i] != INF) {
                    for (pair<int, int> e : Graph[r * k + i]) {
                        int tNew = T[i] + e.second;
                        TNew[e.first % k] = min(TNew[e.first % k], tNew);
                    }
                }
            }
            T = TNew;
            r++;
        }

        if (T[b % k] == INF) Ans[index] = -1;
        else Ans[index] = T[b % k];
    }

    sort(DiffComp.begin(), DiffComp.end());

    auto it = DiffComp.begin();

    while (it != DiffComp.end()) {

        int c = get<0>(*it);
        int rf = (c + 1) * compRows - 1;

        TF = ve<ve<int>>(k, ve<int>(k, INF));
        for (int i = 0; i < k; i++) TF[i][i] = 0;

        while (it != DiffComp.end() && get<0>(*it) == c) {
            b = get<1>(*it);
            a = get<2>(*it);
            index = get<3>(*it);

            while (rf != b / k) {
                TFNew = ve<ve<int>>(k, ve<int>(k, INF));
                for (int i = 0; i < k; i++) {
                    for (int j = 0; j < k; j++) {
                        if (TF[i][j] != INF) {
                            for (pair<int, int> e : Graph[rf * k + j]) {
                                int tNew = TF[i][j] + e.second;
                                TFNew[i][e.first % k] = min(TFNew[i][e.first % k], tNew);
                            }
                        }
                    }
                }
                TF = TFNew;
                rf++;
            }

            int rb = (c + 1) * compRows - 1;
            TB = TF;

            while (rb != a / k) {
                rb--;
                TBNew = ve<ve<int>>(k, ve<int>(k, INF));
                for (int i = 0; i < k; i++) {
                    for (pair<int, int> e : Graph[rb * k + i]) {
                        for (int j = 0; j < k; j++) {
                            if (TB[e.first % k][j] != INF) {
                                int tNew = TB[e.first % k][j] + e.second;
                                TBNew[i][j] = min(TBNew[i][j], tNew);
                            }
                        }
                    }
                }
                TB = TBNew;
            }

            if (TB[a % k][b % k] == INF) Ans[index] = -1;
            else Ans[index] = TB[a % k][b % k];
            it++;
        }

    }

    for (int i = 0; i < o; i++) {
        cout << Ans[i] << endl;
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 956 ms 3580 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 10 ms 384 KB Output is correct
6 Correct 11 ms 384 KB Output is correct
7 Correct 15 ms 512 KB Output is correct
8 Correct 829 ms 4472 KB Output is correct
9 Correct 807 ms 4352 KB Output is correct
10 Correct 804 ms 2164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 399 ms 3576 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 61 ms 896 KB Output is correct
8 Correct 67 ms 896 KB Output is correct
9 Correct 309 ms 4352 KB Output is correct
10 Correct 557 ms 6648 KB Output is correct
11 Correct 401 ms 5240 KB Output is correct
12 Correct 416 ms 4728 KB Output is correct
13 Correct 321 ms 6268 KB Output is correct
14 Correct 169 ms 4344 KB Output is correct
15 Correct 214 ms 3864 KB Output is correct
16 Correct 204 ms 3768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 7 ms 384 KB Output is correct
7 Correct 7 ms 384 KB Output is correct
8 Correct 9 ms 512 KB Output is correct
9 Correct 8 ms 512 KB Output is correct
10 Correct 204 ms 3840 KB Output is correct
11 Correct 229 ms 4600 KB Output is correct
12 Correct 289 ms 6136 KB Output is correct
13 Correct 303 ms 6776 KB Output is correct
14 Correct 286 ms 5368 KB Output is correct
15 Correct 209 ms 3704 KB Output is correct
16 Correct 159 ms 3704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 7 ms 384 KB Output is correct
7 Correct 7 ms 384 KB Output is correct
8 Correct 9 ms 512 KB Output is correct
9 Correct 8 ms 512 KB Output is correct
10 Correct 204 ms 3840 KB Output is correct
11 Correct 229 ms 4600 KB Output is correct
12 Correct 289 ms 6136 KB Output is correct
13 Correct 303 ms 6776 KB Output is correct
14 Correct 286 ms 5368 KB Output is correct
15 Correct 209 ms 3704 KB Output is correct
16 Correct 159 ms 3704 KB Output is correct
17 Correct 707 ms 4824 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 19 ms 640 KB Output is correct
24 Correct 20 ms 640 KB Output is correct
25 Correct 32 ms 768 KB Output is correct
26 Correct 26 ms 640 KB Output is correct
27 Correct 688 ms 4096 KB Output is correct
28 Correct 736 ms 6396 KB Output is correct
29 Correct 774 ms 6904 KB Output is correct
30 Correct 501 ms 5648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 956 ms 3580 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 10 ms 384 KB Output is correct
6 Correct 11 ms 384 KB Output is correct
7 Correct 15 ms 512 KB Output is correct
8 Correct 829 ms 4472 KB Output is correct
9 Correct 807 ms 4352 KB Output is correct
10 Correct 804 ms 2164 KB Output is correct
11 Correct 399 ms 3576 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 61 ms 896 KB Output is correct
18 Correct 67 ms 896 KB Output is correct
19 Correct 309 ms 4352 KB Output is correct
20 Correct 557 ms 6648 KB Output is correct
21 Correct 401 ms 5240 KB Output is correct
22 Correct 416 ms 4728 KB Output is correct
23 Correct 321 ms 6268 KB Output is correct
24 Correct 169 ms 4344 KB Output is correct
25 Correct 214 ms 3864 KB Output is correct
26 Correct 204 ms 3768 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 7 ms 384 KB Output is correct
33 Correct 7 ms 384 KB Output is correct
34 Correct 9 ms 512 KB Output is correct
35 Correct 8 ms 512 KB Output is correct
36 Correct 204 ms 3840 KB Output is correct
37 Correct 229 ms 4600 KB Output is correct
38 Correct 289 ms 6136 KB Output is correct
39 Correct 303 ms 6776 KB Output is correct
40 Correct 286 ms 5368 KB Output is correct
41 Correct 209 ms 3704 KB Output is correct
42 Correct 159 ms 3704 KB Output is correct
43 Correct 707 ms 4824 KB Output is correct
44 Correct 5 ms 384 KB Output is correct
45 Correct 4 ms 384 KB Output is correct
46 Correct 5 ms 384 KB Output is correct
47 Correct 5 ms 384 KB Output is correct
48 Correct 5 ms 384 KB Output is correct
49 Correct 19 ms 640 KB Output is correct
50 Correct 20 ms 640 KB Output is correct
51 Correct 32 ms 768 KB Output is correct
52 Correct 26 ms 640 KB Output is correct
53 Correct 688 ms 4096 KB Output is correct
54 Correct 736 ms 6396 KB Output is correct
55 Correct 774 ms 6904 KB Output is correct
56 Correct 501 ms 5648 KB Output is correct
57 Correct 1434 ms 7928 KB Output is correct
58 Correct 855 ms 4480 KB Output is correct
59 Correct 840 ms 5372 KB Output is correct