답안 #1115796

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1115796 2024-11-21T01:15:41 Z vjudge1 Toll (BOI17_toll) C++17
100 / 100
79 ms 78844 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
// #define int long long
#define endl '\n'
#define setmin(x, y) x = ((x) < (y) ? (x) : (y))
#define setmax(x, y) x = ((x) > (y) ? (x) : (y))

mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}

const int NM = 5e4 + 5;
const int LG = 16;
const int inf = 1e9;

int n, m, q, k;

struct node
{
    int dt[5][5];
    node() {for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) dt[i][j] = inf;}
    node operator*(const node& o)
    {
        node res;
        for (int i = 0; i < k; i++)
            for (int j = 0; j < k; j++)
                for (int t = 0; t < k; t++)
                    setmin(res.dt[i][j], dt[i][t] + o.dt[t][j]);
        return res;
    }
}st[NM][LG];

void solve()    
{
    cin >> k >> n >> m >> q;
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        setmin(st[u / k][0].dt[u % k][v % k], w);
    }
    for (int lg = 1; lg <= __lg(n / k + 1); lg++)
        for (int i = 0; i + (1 << lg) - 1 < n / k; i++)
            st[i][lg] = st[i][lg - 1] * st[i + (1 << lg - 1)][lg - 1];
    while (q--)
    {
        int u, v;
        cin >> u >> v;
        if (u == v)
            cout << 0 << endl;
        else if (u / k < v / k)
        {
            int x = v / k - u / k;
            node res = st[u / k][__lg(x)];
            int t = x - (1 << __lg(x)), cur = u / k + (1 << __lg(x));
            for (int i = t ? __lg(t) : -1; i >= 0; i--)
                if (t & (1 << i))
                {
                    res = res * st[cur][i];
                    cur += 1 << i;
                }
            cout << (res.dt[u % k][v % k] == inf ? -1 : res.dt[u % k][v % k]) << endl;
        }
        else
            cout << -1 << endl;
    }
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    if (fopen("in.txt", "r")) 
    {
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    }
    // freopen("storagebox.inp", "r", stdin);
    // freopen("storagebox.out", "w", stdout);
    int tc = 1; 
    // cin >> tc;
    while (tc--)
        solve();
}

Compilation message

toll.cpp: In function 'void solve()':
toll.cpp:46:57: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   46 |             st[i][lg] = st[i][lg - 1] * st[i + (1 << lg - 1)][lg - 1];
      |                                                      ~~~^~~
toll.cpp: In function 'int main()':
toll.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   76 |         freopen("in.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |         freopen("out.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 78672 KB Output is correct
2 Correct 36 ms 78668 KB Output is correct
3 Correct 40 ms 78672 KB Output is correct
4 Correct 42 ms 78672 KB Output is correct
5 Correct 45 ms 78664 KB Output is correct
6 Correct 43 ms 78684 KB Output is correct
7 Correct 33 ms 78672 KB Output is correct
8 Correct 67 ms 78672 KB Output is correct
9 Correct 70 ms 78664 KB Output is correct
10 Correct 51 ms 78664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 78668 KB Output is correct
2 Correct 34 ms 78560 KB Output is correct
3 Correct 32 ms 78664 KB Output is correct
4 Correct 27 ms 78672 KB Output is correct
5 Correct 29 ms 78736 KB Output is correct
6 Correct 29 ms 78672 KB Output is correct
7 Correct 33 ms 78592 KB Output is correct
8 Correct 33 ms 78664 KB Output is correct
9 Correct 62 ms 78672 KB Output is correct
10 Correct 66 ms 78672 KB Output is correct
11 Correct 58 ms 78748 KB Output is correct
12 Correct 62 ms 78664 KB Output is correct
13 Correct 61 ms 78664 KB Output is correct
14 Correct 50 ms 78664 KB Output is correct
15 Correct 54 ms 78744 KB Output is correct
16 Correct 53 ms 78688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 78664 KB Output is correct
2 Correct 33 ms 78596 KB Output is correct
3 Correct 34 ms 78664 KB Output is correct
4 Correct 36 ms 78664 KB Output is correct
5 Correct 40 ms 78720 KB Output is correct
6 Correct 41 ms 78668 KB Output is correct
7 Correct 38 ms 78664 KB Output is correct
8 Correct 37 ms 78832 KB Output is correct
9 Correct 38 ms 78844 KB Output is correct
10 Correct 68 ms 78672 KB Output is correct
11 Correct 64 ms 78664 KB Output is correct
12 Correct 66 ms 78664 KB Output is correct
13 Correct 70 ms 78672 KB Output is correct
14 Correct 68 ms 78496 KB Output is correct
15 Correct 58 ms 78664 KB Output is correct
16 Correct 55 ms 78664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 78664 KB Output is correct
2 Correct 33 ms 78596 KB Output is correct
3 Correct 34 ms 78664 KB Output is correct
4 Correct 36 ms 78664 KB Output is correct
5 Correct 40 ms 78720 KB Output is correct
6 Correct 41 ms 78668 KB Output is correct
7 Correct 38 ms 78664 KB Output is correct
8 Correct 37 ms 78832 KB Output is correct
9 Correct 38 ms 78844 KB Output is correct
10 Correct 68 ms 78672 KB Output is correct
11 Correct 64 ms 78664 KB Output is correct
12 Correct 66 ms 78664 KB Output is correct
13 Correct 70 ms 78672 KB Output is correct
14 Correct 68 ms 78496 KB Output is correct
15 Correct 58 ms 78664 KB Output is correct
16 Correct 55 ms 78664 KB Output is correct
17 Correct 74 ms 78664 KB Output is correct
18 Correct 45 ms 78524 KB Output is correct
19 Correct 43 ms 78664 KB Output is correct
20 Correct 42 ms 78664 KB Output is correct
21 Correct 42 ms 78704 KB Output is correct
22 Correct 45 ms 78664 KB Output is correct
23 Correct 42 ms 78672 KB Output is correct
24 Correct 44 ms 78664 KB Output is correct
25 Correct 47 ms 78664 KB Output is correct
26 Correct 36 ms 78744 KB Output is correct
27 Correct 59 ms 78704 KB Output is correct
28 Correct 67 ms 78524 KB Output is correct
29 Correct 72 ms 78664 KB Output is correct
30 Correct 60 ms 78748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 78672 KB Output is correct
2 Correct 36 ms 78668 KB Output is correct
3 Correct 40 ms 78672 KB Output is correct
4 Correct 42 ms 78672 KB Output is correct
5 Correct 45 ms 78664 KB Output is correct
6 Correct 43 ms 78684 KB Output is correct
7 Correct 33 ms 78672 KB Output is correct
8 Correct 67 ms 78672 KB Output is correct
9 Correct 70 ms 78664 KB Output is correct
10 Correct 51 ms 78664 KB Output is correct
11 Correct 63 ms 78668 KB Output is correct
12 Correct 34 ms 78560 KB Output is correct
13 Correct 32 ms 78664 KB Output is correct
14 Correct 27 ms 78672 KB Output is correct
15 Correct 29 ms 78736 KB Output is correct
16 Correct 29 ms 78672 KB Output is correct
17 Correct 33 ms 78592 KB Output is correct
18 Correct 33 ms 78664 KB Output is correct
19 Correct 62 ms 78672 KB Output is correct
20 Correct 66 ms 78672 KB Output is correct
21 Correct 58 ms 78748 KB Output is correct
22 Correct 62 ms 78664 KB Output is correct
23 Correct 61 ms 78664 KB Output is correct
24 Correct 50 ms 78664 KB Output is correct
25 Correct 54 ms 78744 KB Output is correct
26 Correct 53 ms 78688 KB Output is correct
27 Correct 31 ms 78664 KB Output is correct
28 Correct 33 ms 78596 KB Output is correct
29 Correct 34 ms 78664 KB Output is correct
30 Correct 36 ms 78664 KB Output is correct
31 Correct 40 ms 78720 KB Output is correct
32 Correct 41 ms 78668 KB Output is correct
33 Correct 38 ms 78664 KB Output is correct
34 Correct 37 ms 78832 KB Output is correct
35 Correct 38 ms 78844 KB Output is correct
36 Correct 68 ms 78672 KB Output is correct
37 Correct 64 ms 78664 KB Output is correct
38 Correct 66 ms 78664 KB Output is correct
39 Correct 70 ms 78672 KB Output is correct
40 Correct 68 ms 78496 KB Output is correct
41 Correct 58 ms 78664 KB Output is correct
42 Correct 55 ms 78664 KB Output is correct
43 Correct 74 ms 78664 KB Output is correct
44 Correct 45 ms 78524 KB Output is correct
45 Correct 43 ms 78664 KB Output is correct
46 Correct 42 ms 78664 KB Output is correct
47 Correct 42 ms 78704 KB Output is correct
48 Correct 45 ms 78664 KB Output is correct
49 Correct 42 ms 78672 KB Output is correct
50 Correct 44 ms 78664 KB Output is correct
51 Correct 47 ms 78664 KB Output is correct
52 Correct 36 ms 78744 KB Output is correct
53 Correct 59 ms 78704 KB Output is correct
54 Correct 67 ms 78524 KB Output is correct
55 Correct 72 ms 78664 KB Output is correct
56 Correct 60 ms 78748 KB Output is correct
57 Correct 79 ms 78664 KB Output is correct
58 Correct 70 ms 78672 KB Output is correct
59 Correct 66 ms 78664 KB Output is correct