답안 #979309

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
979309 2024-05-10T14:25:00 Z chautanphat Toll (BOI17_toll) C++14
100 / 100
72 ms 86956 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 5e4;
int k, n, m, o, cost[N][5][5], ans[__lg(N)+1][N][5][5], mask[N];
 
void dnc(int l, int r, int level = 0)
{
    if (l == r) return;
    int mid = (l+r)/2;
    
    for (int a = 0; a < k; a++)
        ans[level][mid][a][a] = 0;
    for (int i = mid-1; i >= l; i--)
        for (int a = 0; a < k; a++)
            for (int b = 0; b < k; b++)
                for (int c = 0; c < k; c++)
                    ans[level][i][a][c] = min(ans[level][i][a][c], ans[level][i+1][b][c]+cost[i][a][b]);
    for (int a = 0; a < k; a++)
        for (int b = 0; b < k; b++)
            ans[level][mid+1][a][b] = min(ans[level][mid+1][a][b], cost[mid][a][b]);
    for (int i = mid+2; i <= r; i++)
        for (int a = 0; a < k; a++)
            for (int b = 0; b < k; b++)
                for (int c = 0; c < k; c++)
                    ans[level][i][c][a] = min(ans[level][i][c][a], ans[level][i-1][c][b]+cost[i-1][b][a]);
 
    for (int i = mid+1; i <= r; i++) mask[i] |= 1<<level;
 
    dnc(l, mid, level+1);
    dnc(mid+1, r, level+1);
}
 
int main()
{    
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    cin >> k >> n >> m >> o;
 
    for (int i = 0; i < n; i++)
        for (int a = 0; a < k; a++)
            for (int b = 0; b < k; b++)
                cost[i][a][b] = 1e9;
    for (int level = 0; (1<<level) < n; level++)
        for (int i = 0; i < n; i++)
            for (int a = 0; a < k; a++)
                for (int b = 0; b < k; b++)
                    ans[level][i][a][b] = 1e9;
 
    for (int i = 0; i < m; i++)
    {
        int a, b, t;
        cin >> a >> b >> t;
        cost[a/k][a%k][b%k] = t;
    }
 
    dnc(0, n/k);

 
    for (int i = 0; i < o; i++)
    {
        int a, b;
        cin >> a >> b;
        if (a/k == b/k)
        {
            cout << -1 << '\n';
            continue;
        }
        int level = __builtin_ctz(mask[a/k]^mask[b/k]), res = 1e9;
        for (int j = 0; j < k; j++)
            res = min(res, ans[level][a/k][a%k][j]+ans[level][b/k][j][b%k]);
        cout << (res == 1e9 ? -1 : res) << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 84716 KB Output is correct
2 Correct 3 ms 10700 KB Output is correct
3 Correct 1 ms 10584 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 3 ms 22872 KB Output is correct
6 Correct 4 ms 23052 KB Output is correct
7 Correct 4 ms 22876 KB Output is correct
8 Correct 37 ms 84652 KB Output is correct
9 Correct 37 ms 84572 KB Output is correct
10 Correct 24 ms 83892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 85328 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 1 ms 10588 KB Output is correct
5 Correct 1 ms 10588 KB Output is correct
6 Correct 1 ms 10588 KB Output is correct
7 Correct 5 ms 23128 KB Output is correct
8 Correct 5 ms 23132 KB Output is correct
9 Correct 41 ms 84636 KB Output is correct
10 Correct 55 ms 85844 KB Output is correct
11 Correct 40 ms 85332 KB Output is correct
12 Correct 39 ms 84944 KB Output is correct
13 Correct 55 ms 78776 KB Output is correct
14 Correct 32 ms 77648 KB Output is correct
15 Correct 44 ms 77396 KB Output is correct
16 Correct 43 ms 77624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10584 KB Output is correct
4 Correct 1 ms 10724 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 3 ms 22876 KB Output is correct
7 Correct 3 ms 22876 KB Output is correct
8 Correct 4 ms 23092 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 37 ms 84656 KB Output is correct
11 Correct 36 ms 85076 KB Output is correct
12 Correct 50 ms 85844 KB Output is correct
13 Correct 49 ms 85844 KB Output is correct
14 Correct 43 ms 85368 KB Output is correct
15 Correct 40 ms 77396 KB Output is correct
16 Correct 40 ms 77400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10588 KB Output is correct
3 Correct 1 ms 10584 KB Output is correct
4 Correct 1 ms 10724 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 3 ms 22876 KB Output is correct
7 Correct 3 ms 22876 KB Output is correct
8 Correct 4 ms 23092 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 37 ms 84656 KB Output is correct
11 Correct 36 ms 85076 KB Output is correct
12 Correct 50 ms 85844 KB Output is correct
13 Correct 49 ms 85844 KB Output is correct
14 Correct 43 ms 85368 KB Output is correct
15 Correct 40 ms 77396 KB Output is correct
16 Correct 40 ms 77400 KB Output is correct
17 Correct 37 ms 85072 KB Output is correct
18 Correct 1 ms 10588 KB Output is correct
19 Correct 2 ms 10588 KB Output is correct
20 Correct 1 ms 10588 KB Output is correct
21 Correct 1 ms 10584 KB Output is correct
22 Correct 1 ms 10588 KB Output is correct
23 Correct 4 ms 23016 KB Output is correct
24 Correct 4 ms 22876 KB Output is correct
25 Correct 4 ms 23132 KB Output is correct
26 Correct 4 ms 22876 KB Output is correct
27 Correct 34 ms 84564 KB Output is correct
28 Correct 49 ms 85776 KB Output is correct
29 Correct 50 ms 86096 KB Output is correct
30 Correct 43 ms 85328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 84716 KB Output is correct
2 Correct 3 ms 10700 KB Output is correct
3 Correct 1 ms 10584 KB Output is correct
4 Correct 2 ms 10584 KB Output is correct
5 Correct 3 ms 22872 KB Output is correct
6 Correct 4 ms 23052 KB Output is correct
7 Correct 4 ms 22876 KB Output is correct
8 Correct 37 ms 84652 KB Output is correct
9 Correct 37 ms 84572 KB Output is correct
10 Correct 24 ms 83892 KB Output is correct
11 Correct 39 ms 85328 KB Output is correct
12 Correct 1 ms 10588 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 1 ms 10588 KB Output is correct
15 Correct 1 ms 10588 KB Output is correct
16 Correct 1 ms 10588 KB Output is correct
17 Correct 5 ms 23128 KB Output is correct
18 Correct 5 ms 23132 KB Output is correct
19 Correct 41 ms 84636 KB Output is correct
20 Correct 55 ms 85844 KB Output is correct
21 Correct 40 ms 85332 KB Output is correct
22 Correct 39 ms 84944 KB Output is correct
23 Correct 55 ms 78776 KB Output is correct
24 Correct 32 ms 77648 KB Output is correct
25 Correct 44 ms 77396 KB Output is correct
26 Correct 43 ms 77624 KB Output is correct
27 Correct 2 ms 10588 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 1 ms 10584 KB Output is correct
30 Correct 1 ms 10724 KB Output is correct
31 Correct 2 ms 10588 KB Output is correct
32 Correct 3 ms 22876 KB Output is correct
33 Correct 3 ms 22876 KB Output is correct
34 Correct 4 ms 23092 KB Output is correct
35 Correct 4 ms 22876 KB Output is correct
36 Correct 37 ms 84656 KB Output is correct
37 Correct 36 ms 85076 KB Output is correct
38 Correct 50 ms 85844 KB Output is correct
39 Correct 49 ms 85844 KB Output is correct
40 Correct 43 ms 85368 KB Output is correct
41 Correct 40 ms 77396 KB Output is correct
42 Correct 40 ms 77400 KB Output is correct
43 Correct 37 ms 85072 KB Output is correct
44 Correct 1 ms 10588 KB Output is correct
45 Correct 2 ms 10588 KB Output is correct
46 Correct 1 ms 10588 KB Output is correct
47 Correct 1 ms 10584 KB Output is correct
48 Correct 1 ms 10588 KB Output is correct
49 Correct 4 ms 23016 KB Output is correct
50 Correct 4 ms 22876 KB Output is correct
51 Correct 4 ms 23132 KB Output is correct
52 Correct 4 ms 22876 KB Output is correct
53 Correct 34 ms 84564 KB Output is correct
54 Correct 49 ms 85776 KB Output is correct
55 Correct 50 ms 86096 KB Output is correct
56 Correct 43 ms 85328 KB Output is correct
57 Correct 72 ms 86956 KB Output is correct
58 Correct 37 ms 84576 KB Output is correct
59 Correct 41 ms 85428 KB Output is correct