Submission #1320406

#TimeUsernameProblemLanguageResultExecution timeMemory
1320406NgTrung2217Toll (BOI17_toll)C++20
100 / 100
160 ms303788 KiB
#include <bits/stdc++.h>
#define taskname ""
using namespace std;
using ld = long double;
using ull = unsigned long long;
using ll = long long;
const char el = '\n';
const char sp = ' ';
const ll inf = 1e18;
const int maxN = 50005;
const int maxK = 5;

int K, N, M, O;
ll dp[maxN][31][maxK][maxK];

struct Tmat
{
    ll val[maxK][maxK];
    Tmat()
    {
        for (int i = 0; i < maxK; ++i)
            for (int j = 0; j < maxK; ++j)
                val[i][j] = inf;
    }
};

Tmat combine(int in1, int in2)
{
    Tmat cur;
    for (int i = 0; i < K; ++i) cur.val[i][i] = 0;
    int dif = in2 - in1;
    while (dif > 0)
    {
        int j = 31 - __builtin_clz(dif);
        Tmat nxt;
        for (int i = 0; i < K; ++i)
        {
            for (int k = 0; k < K; ++k)
            {
                if (cur.val[i][k] >= inf) continue;
                for (int l = 0; l < K; ++l)
                {
                    nxt.val[i][l] = min(nxt.val[i][l], cur.val[i][k] + dp[in1][j][k][l]);
                }
            }
        }
        dif -= (1 << j);
        in1 += (1 << j);
        cur = nxt;
    }
    return cur;
}

int main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen(taskname ".inp", "r"))
    {
        freopen(taskname ".inp", "r", stdin);
        freopen(taskname ".out", "w", stdout);
    }

    if (!(cin >> K >> N >> M >> O)) return 0;
    N++;
    while (N % K != K - 1) N++;

    for (int j = 0; j <= 30; ++j)
    {
        for (int i = 0; i < N / K; ++i)
        {
            for (int x = 0; x < K; ++x)
                for (int y = 0; y < K; ++y)
                    dp[i][j][x][y] = inf;
        }
    }

    while (M--)
    {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        dp[u / K][0][u % K][v % K] = min(dp[u / K][0][u % K][v % K], w);
    }

    for (int j = 1; j <= 30; ++j)
    {
        for (int i = 0; i < N / K; ++i)
        {
            if (i + (1 << (j - 1)) >= N / K) continue;
            for (int x = 0; x < K; ++x)
            {
                for (int z = 0; z < K; ++z)
                {
                    if (dp[i][j - 1][x][z] >= inf) continue;
                    for (int y = 0; y < K; ++y)
                    {
                        dp[i][j][x][y] = min(dp[i][j][x][y], dp[i][j - 1][x][z] + dp[i + (1 << (j - 1))][j - 1][z][y]);
                    }
                }
            }
        }
    }

    while (O--)
    {
        int u, v;
        cin >> u >> v;
        if (u / K >= v / K)
        {
            cout << -1 << el;
            continue;
        }
        Tmat res = combine(u / K, v / K);
        ll ans = res.val[u % K][v % K];
        if (ans >= inf) cout << -1 << el;
        else cout << ans << el;
    }

    return 0;
}







Compilation message (stderr)

toll.cpp: In function 'int main()':
toll.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |         freopen(taskname ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |         freopen(taskname ".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...