답안 #244983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
244983 2020-07-05T10:40:22 Z santaclaus03 Toll (BOI17_toll) C++14
56 / 100
3000 ms 53632 KB
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using ii = pair<int, int>;
using vvii = vector<vector<ii>>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;

#define INF 1000000000

int K, N, M, O; 
vvii rev;
vector<ii> queries;

vi sqrt_dcmp() {
    int S = (int) sqrt(N);
    int G = (int) ceil((double) N / S);
    vvvi dist(G, vvi(S + K, vi(S + K, INF)));
    for (int g = 0; g < G; ++g) {
        for (int u = 0; u < S + K; ++u) {
            dist[g][u][u] = 0;
            for (int v = u + 1; v < S + K; ++v) {
                if (u + g * S >= N || v + g * S >= N) continue;
                for (ii e : rev[v + g * S]) {
                    int w = e.second - g * S;
                    if (w < u) continue;
                    dist[g][u][v] = min(dist[g][u][v], dist[g][u][w] + e.first);
                }
            }
        }
    }
    vi answers;
    for (ii q : queries) {
        int a = q.first, b = q.second;
        if (b < a) {
            answers.push_back(-1);
            continue;
        }
        int ans = INF;
        int g1 = a / S, g2 = b / S;
        if (g1 == g2) {
            ans = dist[g1][a % S][b % S];
        } else {
            vvi d(G, vi(K, INF));
            for (int k = 0; k < K; ++k) {
                d[g1+1][k] = dist[g1][a % S][k + S];
            }
            for (int g = g1 + 2; g <= g2; ++g) {
                for (int k1 = 0; k1 < K; ++k1) {
                    for (int k2 = 0; k2 < K; ++k2) {
                        d[g][k1] = min(d[g][k1], d[g-1][k2] + dist[g-1][k2][k1 + S]);
                    }
                }
            }
            for (int k = 0; k < K; ++k) {
                ans = min(ans, d[g2][k] + dist[g2][k][b % S]);
            }
        }
        answers.push_back(ans == INF ? -1 : ans);
    }
    return answers;
}

vi bruteforce() {
    vi d0(N, INF);
    d0[0] = 0;
    for (int u = 1; u < N; ++u)
    {
        for (ii e : rev[u])
        {
            d0[u] = min(d0[u], d0[e.second] + e.first);
        }
    }
    vi answers;
    for (ii q : queries)
    {
        int a = q.first, b = q.second;
        int ans;
        if (a == 0) {
            ans = d0[b];
        }
        else
        {
            vi d(N, INF);
            d[a] = 0;
            for (int u = a + 1; u < N; ++u)
            {
                for (ii e : rev[u])
                {
                    d[u] = min(d[u], d[e.second] + e.first);
                }
            }
            ans = d[b];
        }
        answers.push_back(ans == INF ? -1 : ans);
    }
    return answers;
}

int main() {
    cin >> K >> N >> M >> O;
    rev.resize(N);
    queries.resize(O);
    for (int i = 0; i < M; ++i) {
        int a, b, t; cin >> a >> b >> t;
        rev[b].emplace_back(t, a);
    }
    for (int i = 0; i < O; ++i) cin >> queries[i].first >> queries[i].second;
    vi expected = bruteforce();
    vi actual = sqrt_dcmp();
    for (int i = 0; i < O; ++i) {
        //cout << "Expected: " << expected[i] << ", Actual: " << actual[i] << endl;
        cout << expected[i] << endl;
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1906 ms 50656 KB Output is correct
2 Correct 5 ms 256 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 13 ms 512 KB Output is correct
6 Correct 12 ms 640 KB Output is correct
7 Correct 13 ms 640 KB Output is correct
8 Correct 1993 ms 50568 KB Output is correct
9 Correct 1850 ms 50468 KB Output is correct
10 Correct 645 ms 48240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 353 ms 51328 KB Output is correct
2 Correct 5 ms 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 256 KB Output is correct
6 Correct 5 ms 256 KB Output is correct
7 Correct 47 ms 896 KB Output is correct
8 Correct 44 ms 1016 KB Output is correct
9 Correct 280 ms 50484 KB Output is correct
10 Correct 458 ms 53172 KB Output is correct
11 Correct 354 ms 51648 KB Output is correct
12 Correct 325 ms 51380 KB Output is correct
13 Correct 361 ms 28920 KB Output is correct
14 Correct 221 ms 26616 KB Output is correct
15 Correct 200 ms 26360 KB Output is correct
16 Correct 204 ms 26212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 6 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 8 ms 512 KB Output is correct
7 Correct 8 ms 588 KB Output is correct
8 Correct 14 ms 640 KB Output is correct
9 Correct 10 ms 640 KB Output is correct
10 Correct 191 ms 50192 KB Output is correct
11 Correct 238 ms 51236 KB Output is correct
12 Correct 353 ms 52844 KB Output is correct
13 Correct 358 ms 53464 KB Output is correct
14 Correct 286 ms 52380 KB Output is correct
15 Correct 168 ms 26260 KB Output is correct
16 Correct 180 ms 26328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 256 KB Output is correct
2 Correct 6 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 8 ms 512 KB Output is correct
7 Correct 8 ms 588 KB Output is correct
8 Correct 14 ms 640 KB Output is correct
9 Correct 10 ms 640 KB Output is correct
10 Correct 191 ms 50192 KB Output is correct
11 Correct 238 ms 51236 KB Output is correct
12 Correct 353 ms 52844 KB Output is correct
13 Correct 358 ms 53464 KB Output is correct
14 Correct 286 ms 52380 KB Output is correct
15 Correct 168 ms 26260 KB Output is correct
16 Correct 180 ms 26328 KB Output is correct
17 Correct 964 ms 51308 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 256 KB Output is correct
20 Correct 5 ms 256 KB Output is correct
21 Correct 5 ms 256 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 28 ms 640 KB Output is correct
24 Correct 33 ms 640 KB Output is correct
25 Correct 52 ms 760 KB Output is correct
26 Correct 53 ms 804 KB Output is correct
27 Correct 711 ms 50288 KB Output is correct
28 Correct 1585 ms 53032 KB Output is correct
29 Correct 1397 ms 53632 KB Output is correct
30 Correct 1302 ms 52336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1906 ms 50656 KB Output is correct
2 Correct 5 ms 256 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 13 ms 512 KB Output is correct
6 Correct 12 ms 640 KB Output is correct
7 Correct 13 ms 640 KB Output is correct
8 Correct 1993 ms 50568 KB Output is correct
9 Correct 1850 ms 50468 KB Output is correct
10 Correct 645 ms 48240 KB Output is correct
11 Correct 353 ms 51328 KB Output is correct
12 Correct 5 ms 256 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 4 ms 256 KB Output is correct
16 Correct 5 ms 256 KB Output is correct
17 Correct 47 ms 896 KB Output is correct
18 Correct 44 ms 1016 KB Output is correct
19 Correct 280 ms 50484 KB Output is correct
20 Correct 458 ms 53172 KB Output is correct
21 Correct 354 ms 51648 KB Output is correct
22 Correct 325 ms 51380 KB Output is correct
23 Correct 361 ms 28920 KB Output is correct
24 Correct 221 ms 26616 KB Output is correct
25 Correct 200 ms 26360 KB Output is correct
26 Correct 204 ms 26212 KB Output is correct
27 Correct 5 ms 256 KB Output is correct
28 Correct 6 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 8 ms 512 KB Output is correct
33 Correct 8 ms 588 KB Output is correct
34 Correct 14 ms 640 KB Output is correct
35 Correct 10 ms 640 KB Output is correct
36 Correct 191 ms 50192 KB Output is correct
37 Correct 238 ms 51236 KB Output is correct
38 Correct 353 ms 52844 KB Output is correct
39 Correct 358 ms 53464 KB Output is correct
40 Correct 286 ms 52380 KB Output is correct
41 Correct 168 ms 26260 KB Output is correct
42 Correct 180 ms 26328 KB Output is correct
43 Correct 964 ms 51308 KB Output is correct
44 Correct 5 ms 384 KB Output is correct
45 Correct 5 ms 256 KB Output is correct
46 Correct 5 ms 256 KB Output is correct
47 Correct 5 ms 256 KB Output is correct
48 Correct 5 ms 384 KB Output is correct
49 Correct 28 ms 640 KB Output is correct
50 Correct 33 ms 640 KB Output is correct
51 Correct 52 ms 760 KB Output is correct
52 Correct 53 ms 804 KB Output is correct
53 Correct 711 ms 50288 KB Output is correct
54 Correct 1585 ms 53032 KB Output is correct
55 Correct 1397 ms 53632 KB Output is correct
56 Correct 1302 ms 52336 KB Output is correct
57 Execution timed out 3084 ms 8244 KB Time limit exceeded
58 Halted 0 ms 0 KB -