Submission #988057

#TimeUsernameProblemLanguageResultExecution timeMemory
988057NoLoveToll (BOI17_toll)C++14
100 / 100
155 ms75860 KiB

/**
 *    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 (stderr)

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);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...