Submission #784671

#TimeUsernameProblemLanguageResultExecution timeMemory
784671chanhchuong123Toll (BOI17_toll)C++14
100 / 100
98 ms19264 KiB
#include <bits/stdc++.h>
using namespace std;
#define task ""
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)

template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}

const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};

const int INF = 1e9 + 7;
const int MAX = 50000;
int k, n, m, q;
int dp[MAX][16][5];
#define ID(i, j, t) (((i) / k + (1 << (j))) * k + (t))

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr);

	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}

    cin >> k >> n >> m >> q;
    for (int j = 0; j <= 15; ++j) {
        for (int i = 0; i < n; ++i)
            for (int t = 0; t < 5; ++t)
                dp[i][j][t] = INF;
    }
    while (m--) {
        int a, b, t; cin >> a >> b >> t;
        dp[a][0][b % k] = t;
    }
    for (int j = 1; j <= 15; ++j) {
        for (int i = 0; i < n; ++i) {
            for (int m = 0; m < k; ++m) {
                for (int t = 0; t < k; ++t) {
                    if (ID(i, j, t) >= n) continue;
                    if (dp[i][j - 1][m] >= INF) continue;
                    if (dp[ID(i, j - 1, m)][j - 1][t] >= INF) continue;
                    minimize(dp[i][j][t], dp[i][j - 1][m] + dp[ID(i, j - 1, m)][j - 1][t]);
                }
            }
        }
    }
    while (q--) {
        int a, b; cin >> a >> b;
        vector<int> curDist(5, INF), nextDist(5, INF);
        curDist[a % k] = 0;
        int u = a / k, v = b / k;
        for (int j = 15; j >= 0; --j) {
            if (u + (1 << j) <= v) {
                for (int m = 0; m < k; ++m) {
                    for (int t = 0; t < k; ++t) {
                        minimize(nextDist[t], curDist[m] + dp[u * k + m][j][t]);
                    }
                }
                curDist = nextDist; u += 1 << j;
                nextDist.assign(k, INF);
            }
        }
        int res = curDist[b % k];
        if (res == INF) res = -1;
        cout << res << '\n';
    }

	return 0;
}

Compilation message (stderr)

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