답안 #988057

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
988057 2024-05-24T01:57:24 Z NoLove Toll (BOI17_toll) C++14
100 / 100
155 ms 75860 KB
/**
 *    author : Lăng Trọng Đạt
 *    created: 24-05-2024 
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int long long
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
using pii = pair<int, int>;
using vi = vector<int>;
#define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
void mx(int& a, int b) { if (b > a) a = b; }
void mi(int& a, int b) { if (b < a) a = b; }
#define si(x) (int)(x.size())
const int INF = 1e18;
const int MOD = 1e9 + 7;
 
const int MAXN = 5e4 + 5;
int g[MAXN];
vector<pii> nxt[MAXN];
int n, q, k, w, a, b, m;
pair<int, int> down[17][MAXN][5]; // down[i][v][x] sau khi nhảy 2^i nhóm thì nó sẽ dừng tại node thứ x trong nhóm đó (tính từ 0) với chi phí tối thiểu
 
int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    if (fopen("hi.inp", "r")) {
        freopen("hi.inp", "r", stdin);
        freopen("hi.txt", "r", stdin);
       freopen("hi.out", "w", stdout);
    } 
    
    memset(down, 0x3f, sizeof(down));
    cin >> k >> n >> m >> q;
    FOR(i, 1, m) {
        cin >> a >> b >> w;
        nxt[a].pb({b, w});
        down[0][a][b % k] = {b, w};
    }
    FOR(i, 0, 15) {
        FOR(v, 0, n - 1) {
            FOR(x, 0, k - 1) { // sau khi nhảy sẽ ở vị trí x trong nhóm đó
                FOR(j, 0, k - 1) {
                    auto[b, t] = down[i][v][j];
                    if (b <= n && down[i + 1][v][x].se > down[i][b][x].se + t) {
                        down[i + 1][v][x].f = down[i][b][x].f;
                        down[i + 1][v][x].se = down[i][b][x].se + t;
                    }
                }
            }
        }
    }
 
    auto solve = [&](int a, int b) -> int {
        int diff = b / k - a / k;
        if (diff <= 0) return -1;
        else {
            vector<int> mn(k, INF);
            mn[a % k] = 0;
            FOR(i, 0, 16) {
                if ((1 << i) & diff) {
                    vector<int> tmp(k, INF);
                    int can_down = -1;
                    for (int v = a / k * k; v < a / k * k + k; v++) {
                        FOR(x, 0, k - 1) {
                            if (mn[v % k] < INF && down[i][v][x].f <= n) {
                                can_down = down[i][v][x].f;
                            }   
                            mi(tmp[x], mn[v % k] + down[i][v][x].se);
                        }
                    }
                    if (can_down == -1) return -1;
                    a = can_down;
                    swap(tmp, mn);
                }
            }
            return (mn[b % k] < INF ? mn[b % k] : -1);
        }
        return 0;
    };
    FOR(i, 1, q) {
        cin >> a >> b;
        cout << solve(a, b) << "\n";
    }
}

Compilation message

toll.cpp: In function 'int32_t main()':
toll.cpp:19:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   19 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
toll.cpp:42:5: note: in expansion of macro 'FOR'
   42 |     FOR(i, 1, m) {
      |     ^~~
toll.cpp:19:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   19 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
toll.cpp:47:5: note: in expansion of macro 'FOR'
   47 |     FOR(i, 0, 15) {
      |     ^~~
toll.cpp:19:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   19 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
toll.cpp:48:9: note: in expansion of macro 'FOR'
   48 |         FOR(v, 0, n - 1) {
      |         ^~~
toll.cpp:19:31: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   19 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
toll.cpp:49:13: note: in expansion of macro 'FOR'
   49 |             FOR(x, 0, k - 1) { // sau khi nhảy sẽ ở vị trí x trong nhóm đó
      |             ^~~
toll.cpp:19:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   19 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
toll.cpp:50:17: note: in expansion of macro 'FOR'
   50 |                 FOR(j, 0, k - 1) {
      |                 ^~~
toll.cpp:51:25: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   51 |                     auto[b, t] = down[i][v][j];
      |                         ^
toll.cpp: In lambda function:
toll.cpp:19:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   19 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
toll.cpp:67:13: note: in expansion of macro 'FOR'
   67 |             FOR(i, 0, 16) {
      |             ^~~
toll.cpp:19:31: warning: unnecessary parentheses in declaration of 'x' [-Wparentheses]
   19 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
toll.cpp:72:25: note: in expansion of macro 'FOR'
   72 |                         FOR(x, 0, k - 1) {
      |                         ^~~
toll.cpp: In function 'int32_t main()':
toll.cpp:19:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   19 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
toll.cpp:88:5: note: in expansion of macro 'FOR'
   88 |     FOR(i, 1, q) {
      |     ^~~
toll.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         freopen("hi.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen("hi.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:37:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |        freopen("hi.out", "w", stdout);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 70988 KB Output is correct
2 Correct 32 ms 67920 KB Output is correct
3 Correct 29 ms 67956 KB Output is correct
4 Correct 29 ms 68188 KB Output is correct
5 Correct 33 ms 68180 KB Output is correct
6 Correct 31 ms 68188 KB Output is correct
7 Correct 30 ms 68176 KB Output is correct
8 Correct 76 ms 70492 KB Output is correct
9 Correct 69 ms 70384 KB Output is correct
10 Correct 43 ms 68184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 80 ms 72248 KB Output is correct
2 Correct 33 ms 67920 KB Output is correct
3 Correct 32 ms 67924 KB Output is correct
4 Correct 28 ms 68184 KB Output is correct
5 Correct 29 ms 67924 KB Output is correct
6 Correct 32 ms 67932 KB Output is correct
7 Correct 35 ms 68176 KB Output is correct
8 Correct 32 ms 68188 KB Output is correct
9 Correct 64 ms 70480 KB Output is correct
10 Correct 97 ms 74324 KB Output is correct
11 Correct 86 ms 72532 KB Output is correct
12 Correct 82 ms 71508 KB Output is correct
13 Correct 98 ms 75356 KB Output is correct
14 Correct 70 ms 72020 KB Output is correct
15 Correct 82 ms 71316 KB Output is correct
16 Correct 101 ms 71364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 67932 KB Output is correct
2 Correct 31 ms 67932 KB Output is correct
3 Correct 31 ms 68036 KB Output is correct
4 Correct 32 ms 68060 KB Output is correct
5 Correct 32 ms 67932 KB Output is correct
6 Correct 32 ms 68184 KB Output is correct
7 Correct 33 ms 68188 KB Output is correct
8 Correct 38 ms 68312 KB Output is correct
9 Correct 33 ms 68048 KB Output is correct
10 Correct 60 ms 70528 KB Output is correct
11 Correct 75 ms 72020 KB Output is correct
12 Correct 91 ms 74064 KB Output is correct
13 Correct 93 ms 74880 KB Output is correct
14 Correct 79 ms 73040 KB Output is correct
15 Correct 75 ms 71252 KB Output is correct
16 Correct 79 ms 71252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 67932 KB Output is correct
2 Correct 31 ms 67932 KB Output is correct
3 Correct 31 ms 68036 KB Output is correct
4 Correct 32 ms 68060 KB Output is correct
5 Correct 32 ms 67932 KB Output is correct
6 Correct 32 ms 68184 KB Output is correct
7 Correct 33 ms 68188 KB Output is correct
8 Correct 38 ms 68312 KB Output is correct
9 Correct 33 ms 68048 KB Output is correct
10 Correct 60 ms 70528 KB Output is correct
11 Correct 75 ms 72020 KB Output is correct
12 Correct 91 ms 74064 KB Output is correct
13 Correct 93 ms 74880 KB Output is correct
14 Correct 79 ms 73040 KB Output is correct
15 Correct 75 ms 71252 KB Output is correct
16 Correct 79 ms 71252 KB Output is correct
17 Correct 81 ms 72016 KB Output is correct
18 Correct 33 ms 68020 KB Output is correct
19 Correct 31 ms 67928 KB Output is correct
20 Correct 33 ms 67924 KB Output is correct
21 Correct 34 ms 67932 KB Output is correct
22 Correct 31 ms 67928 KB Output is correct
23 Correct 31 ms 68180 KB Output is correct
24 Correct 34 ms 68188 KB Output is correct
25 Correct 36 ms 68444 KB Output is correct
26 Correct 33 ms 68188 KB Output is correct
27 Correct 66 ms 70480 KB Output is correct
28 Correct 98 ms 74168 KB Output is correct
29 Correct 98 ms 74820 KB Output is correct
30 Correct 88 ms 73040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 70988 KB Output is correct
2 Correct 32 ms 67920 KB Output is correct
3 Correct 29 ms 67956 KB Output is correct
4 Correct 29 ms 68188 KB Output is correct
5 Correct 33 ms 68180 KB Output is correct
6 Correct 31 ms 68188 KB Output is correct
7 Correct 30 ms 68176 KB Output is correct
8 Correct 76 ms 70492 KB Output is correct
9 Correct 69 ms 70384 KB Output is correct
10 Correct 43 ms 68184 KB Output is correct
11 Correct 80 ms 72248 KB Output is correct
12 Correct 33 ms 67920 KB Output is correct
13 Correct 32 ms 67924 KB Output is correct
14 Correct 28 ms 68184 KB Output is correct
15 Correct 29 ms 67924 KB Output is correct
16 Correct 32 ms 67932 KB Output is correct
17 Correct 35 ms 68176 KB Output is correct
18 Correct 32 ms 68188 KB Output is correct
19 Correct 64 ms 70480 KB Output is correct
20 Correct 97 ms 74324 KB Output is correct
21 Correct 86 ms 72532 KB Output is correct
22 Correct 82 ms 71508 KB Output is correct
23 Correct 98 ms 75356 KB Output is correct
24 Correct 70 ms 72020 KB Output is correct
25 Correct 82 ms 71316 KB Output is correct
26 Correct 101 ms 71364 KB Output is correct
27 Correct 28 ms 67932 KB Output is correct
28 Correct 31 ms 67932 KB Output is correct
29 Correct 31 ms 68036 KB Output is correct
30 Correct 32 ms 68060 KB Output is correct
31 Correct 32 ms 67932 KB Output is correct
32 Correct 32 ms 68184 KB Output is correct
33 Correct 33 ms 68188 KB Output is correct
34 Correct 38 ms 68312 KB Output is correct
35 Correct 33 ms 68048 KB Output is correct
36 Correct 60 ms 70528 KB Output is correct
37 Correct 75 ms 72020 KB Output is correct
38 Correct 91 ms 74064 KB Output is correct
39 Correct 93 ms 74880 KB Output is correct
40 Correct 79 ms 73040 KB Output is correct
41 Correct 75 ms 71252 KB Output is correct
42 Correct 79 ms 71252 KB Output is correct
43 Correct 81 ms 72016 KB Output is correct
44 Correct 33 ms 68020 KB Output is correct
45 Correct 31 ms 67928 KB Output is correct
46 Correct 33 ms 67924 KB Output is correct
47 Correct 34 ms 67932 KB Output is correct
48 Correct 31 ms 67928 KB Output is correct
49 Correct 31 ms 68180 KB Output is correct
50 Correct 34 ms 68188 KB Output is correct
51 Correct 36 ms 68444 KB Output is correct
52 Correct 33 ms 68188 KB Output is correct
53 Correct 66 ms 70480 KB Output is correct
54 Correct 98 ms 74168 KB Output is correct
55 Correct 98 ms 74820 KB Output is correct
56 Correct 88 ms 73040 KB Output is correct
57 Correct 155 ms 75860 KB Output is correct
58 Correct 89 ms 70484 KB Output is correct
59 Correct 125 ms 72620 KB Output is correct