Submission #340414

#TimeUsernameProblemLanguageResultExecution timeMemory
3404148e7Toll (BOI17_toll)C++14
100 / 100
255 ms160348 KiB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#define maxn 50005
#define int long long
using namespace std;
const int inf = (1<<30) - 1;
struct mat{
	int m[5][5];
	mat() {
		for (int i = 0;i < 5;i++) {
			for (int j = 0;j < 5;j++) m[i][j] = inf;
		}
	}
};
void print(mat a) {
	for (int i = 0;i < 5;i++) {
		for (int j = 0;j < 5;j++) cerr << a.m[i][j] << " ";
		cerr << endl;
	}
}
mat comb(mat a, mat b) {
	mat ret;
	for (int i = 0;i < 5;i++) {
		for (int j = 0;j < 5;j++) {
			ret.m[i][j] = inf;
			for (int k = 0;k < 5;k++) ret.m[i][j] = min(ret.m[i][j], a.m[i][k] + b.m[k][j]);
		}
	}
	return ret;
}
mat anc[16][maxn];


signed main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	int k, n, m, o;
	cin >> k >> n >> m >> o;
	for (int i = 0;i < m;i++) {
		int a, b, t;
		cin >> a >> b >> t;
		anc[0][a / k].m[a % k][b % k] = t;
	}

	int len = (n + k - 1) / k;
	for (int i = 1;i < 16;i++) {
		for (int j = 0;j < len - (1<<i) + 1;j++) {
			anc[i][j] = comb(anc[i - 1][j], anc[i - 1][j + (1<<(i - 1))]);
		}
	}
	while (o--) {
		int a, b;
		cin >> a >> b;
		int ai = a / k, bi = b / k;
		mat cur;
		for (int i = 0;i < 5;i++) {
			cur.m[i][i] = 0;
		}
		for (int i = 15;i >= 0;i--) {
			if (ai + (1<<i) <= bi) {
				cur = comb(cur, anc[i][ai]);
				ai += 1<<i;
			}
		}
		int ans = cur.m[a % k][b % k];
		cout << (ans >= inf ? -1 : ans) << "\n";
	}
}
#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...