Submission #979201

# Submission time Handle Problem Language Result Execution time Memory
979201 2024-05-10T11:30:33 Z chautanphat Toll (BOI17_toll) C++14
46 / 100
394 ms 85076 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 5e4;
 
struct query
{
    int a, b, id, ans;
} queries[N];
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] = 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 = 0; i < o; i++)
    {
        int a = queries[i].a, b = queries[i].b;
        if (queries[i].ans == 1e9 && l <= a/k && a/k <= mid && mid+1 <= b/k && b/k <= r)
            for (int j = 0; j < k; j++)
                queries[i].ans = min(queries[i].ans, ans[level][a/k][a%k][j]+ans[level][b/k][j][b%k]);
    }

    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;
}
# Verdict Execution time Memory Grader output
1 Correct 394 ms 84576 KB Output is correct
2 Correct 1 ms 10584 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10704 KB Output is correct
5 Correct 4 ms 22872 KB Output is correct
6 Correct 4 ms 23060 KB Output is correct
7 Correct 5 ms 22876 KB Output is correct
8 Correct 348 ms 84560 KB Output is correct
9 Correct 353 ms 84568 KB Output is correct
10 Correct 342 ms 83908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 202 ms 84760 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10588 KB Output is correct
6 Correct 2 ms 10700 KB Output is correct
7 Correct 11 ms 23332 KB Output is correct
8 Correct 9 ms 23132 KB Output is correct
9 Incorrect 346 ms 84572 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10840 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10672 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 22876 KB Output is correct
9 Correct 3 ms 22876 KB Output is correct
10 Correct 42 ms 84528 KB Output is correct
11 Correct 44 ms 84560 KB Output is correct
12 Correct 59 ms 85016 KB Output is correct
13 Correct 54 ms 85032 KB Output is correct
14 Correct 50 ms 84820 KB Output is correct
15 Correct 41 ms 74836 KB Output is correct
16 Correct 41 ms 74832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 10840 KB Output is correct
2 Correct 1 ms 10588 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10588 KB Output is correct
5 Correct 2 ms 10672 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 22876 KB Output is correct
9 Correct 3 ms 22876 KB Output is correct
10 Correct 42 ms 84528 KB Output is correct
11 Correct 44 ms 84560 KB Output is correct
12 Correct 59 ms 85016 KB Output is correct
13 Correct 54 ms 85032 KB Output is correct
14 Correct 50 ms 84820 KB Output is correct
15 Correct 41 ms 74836 KB Output is correct
16 Correct 41 ms 74832 KB Output is correct
17 Correct 94 ms 84816 KB Output is correct
18 Correct 1 ms 10588 KB Output is correct
19 Correct 1 ms 10588 KB Output is correct
20 Correct 1 ms 10588 KB Output is correct
21 Correct 1 ms 10588 KB Output is correct
22 Correct 1 ms 10588 KB Output is correct
23 Correct 8 ms 23076 KB Output is correct
24 Correct 5 ms 22876 KB Output is correct
25 Correct 5 ms 23128 KB Output is correct
26 Correct 4 ms 22876 KB Output is correct
27 Correct 131 ms 84404 KB Output is correct
28 Correct 86 ms 84888 KB Output is correct
29 Correct 90 ms 85076 KB Output is correct
30 Correct 81 ms 84816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 394 ms 84576 KB Output is correct
2 Correct 1 ms 10584 KB Output is correct
3 Correct 2 ms 10588 KB Output is correct
4 Correct 2 ms 10704 KB Output is correct
5 Correct 4 ms 22872 KB Output is correct
6 Correct 4 ms 23060 KB Output is correct
7 Correct 5 ms 22876 KB Output is correct
8 Correct 348 ms 84560 KB Output is correct
9 Correct 353 ms 84568 KB Output is correct
10 Correct 342 ms 83908 KB Output is correct
11 Correct 202 ms 84760 KB Output is correct
12 Correct 2 ms 10584 KB Output is correct
13 Correct 2 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 2 ms 10588 KB Output is correct
16 Correct 2 ms 10700 KB Output is correct
17 Correct 11 ms 23332 KB Output is correct
18 Correct 9 ms 23132 KB Output is correct
19 Incorrect 346 ms 84572 KB Output isn't correct
20 Halted 0 ms 0 KB -