Submission #1115841

# Submission time Handle Problem Language Result Execution time Memory
1115841 2024-11-21T02:36:27 Z kustizus Toll (BOI17_toll) C++17
100 / 100
166 ms 72520 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define read_input(file) if (fopen(file".inp", "r")) freopen(file".inp", "r", stdin);
#define file(file) freopen (file".inp", "r", stdin); freopen (file".out", "w", stdout);

const int N = 5e4, inf = 1e12;
int n, m, o, k;

struct matrix{
	vector <vector<int>> d;
	void init(int n, int m, int v){
		d.resize(n, vector<int>(m, v));
	}
};

matrix operator* (const matrix &a, const matrix &b){
	matrix c;
	c.init(a.d.size(), b.d[0].size(), inf);
	for (int i = 0; i < (int)a.d.size(); ++i)
		for (int j = 0; j < (int)b.d[0].size(); ++j)
			for (int k = 0; k < (int)a.d[0].size(); ++k)
				c.d[i][j] = min(c.d[i][j], a.d[i][k] + b.d[k][j]);
	return c;
}

matrix M[N + 5];
matrix binlift[N + 5][17];

void solve(){
	cin >> k >> n >> m >> o;
	int sum = n / k;
	for (int i = 0; i <= sum; ++i)
		M[i].init(k, k, inf);
	for (int i = 1; i <= m; ++i){
		int u, v, w;
		cin >> u >> v >> w;
		M[u / k].d[u % k][v % k] = w;
	}
	for (int i = 0; i <= sum; ++i) binlift[i][0] = M[i];
	for (int i = 1; i <= 16; ++i)
		for (int j = 0; j <= sum - (1 << i); ++j)
			binlift[j][i] = binlift[j][i - 1] * binlift[j + (1 << (i - 1))][i - 1];
	while (o--){
		int u, v;
		cin >> u >> v;
		if (u > v){
			cout << "-1\n";
			continue;
		}
		matrix nw;
		nw.init(k, k, inf);
		nw.d[0][u % k] = 0;
		int s = u / k, t = v / k;
		for (int i = 16; i >= 0; --i)
			if (s + (1 << i) <= t){
				nw = nw * binlift[s][i];
				s += (1 << i);
			}
		cout << (nw.d[0][v % k] >= inf ? -1 : nw.d[0][v % k]) << "\n";
	}
}

signed main(){
    faster;
    // file("file");
    // read_input("file");
    int tt = 1;
    // cin >> tt;
    while (tt--){
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 131 ms 70720 KB Output is correct
2 Correct 7 ms 21584 KB Output is correct
3 Correct 7 ms 21584 KB Output is correct
4 Correct 6 ms 21584 KB Output is correct
5 Correct 8 ms 22096 KB Output is correct
6 Correct 8 ms 22096 KB Output is correct
7 Correct 8 ms 22096 KB Output is correct
8 Correct 132 ms 70684 KB Output is correct
9 Correct 131 ms 70604 KB Output is correct
10 Correct 124 ms 70728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 67556 KB Output is correct
2 Correct 6 ms 21584 KB Output is correct
3 Correct 7 ms 21584 KB Output is correct
4 Correct 9 ms 21584 KB Output is correct
5 Correct 6 ms 21584 KB Output is correct
6 Correct 6 ms 21584 KB Output is correct
7 Correct 13 ms 22096 KB Output is correct
8 Correct 15 ms 22264 KB Output is correct
9 Correct 127 ms 70624 KB Output is correct
10 Correct 138 ms 61768 KB Output is correct
11 Correct 142 ms 67480 KB Output is correct
12 Correct 127 ms 61824 KB Output is correct
13 Correct 100 ms 48712 KB Output is correct
14 Correct 81 ms 44468 KB Output is correct
15 Correct 82 ms 48712 KB Output is correct
16 Correct 86 ms 48836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 21584 KB Output is correct
2 Correct 6 ms 21556 KB Output is correct
3 Correct 6 ms 21584 KB Output is correct
4 Correct 6 ms 21584 KB Output is correct
5 Correct 6 ms 21604 KB Output is correct
6 Correct 8 ms 22096 KB Output is correct
7 Correct 8 ms 22108 KB Output is correct
8 Correct 12 ms 22096 KB Output is correct
9 Correct 8 ms 22096 KB Output is correct
10 Correct 118 ms 70472 KB Output is correct
11 Correct 103 ms 67344 KB Output is correct
12 Correct 118 ms 61768 KB Output is correct
13 Correct 126 ms 61768 KB Output is correct
14 Correct 112 ms 61832 KB Output is correct
15 Correct 66 ms 48720 KB Output is correct
16 Correct 68 ms 48712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 21584 KB Output is correct
2 Correct 6 ms 21556 KB Output is correct
3 Correct 6 ms 21584 KB Output is correct
4 Correct 6 ms 21584 KB Output is correct
5 Correct 6 ms 21604 KB Output is correct
6 Correct 8 ms 22096 KB Output is correct
7 Correct 8 ms 22108 KB Output is correct
8 Correct 12 ms 22096 KB Output is correct
9 Correct 8 ms 22096 KB Output is correct
10 Correct 118 ms 70472 KB Output is correct
11 Correct 103 ms 67344 KB Output is correct
12 Correct 118 ms 61768 KB Output is correct
13 Correct 126 ms 61768 KB Output is correct
14 Correct 112 ms 61832 KB Output is correct
15 Correct 66 ms 48720 KB Output is correct
16 Correct 68 ms 48712 KB Output is correct
17 Correct 97 ms 67480 KB Output is correct
18 Correct 6 ms 21584 KB Output is correct
19 Correct 5 ms 21584 KB Output is correct
20 Correct 6 ms 21584 KB Output is correct
21 Correct 7 ms 21776 KB Output is correct
22 Correct 5 ms 21552 KB Output is correct
23 Correct 13 ms 22236 KB Output is correct
24 Correct 9 ms 22096 KB Output is correct
25 Correct 10 ms 22096 KB Output is correct
26 Correct 9 ms 22112 KB Output is correct
27 Correct 140 ms 70692 KB Output is correct
28 Correct 118 ms 61844 KB Output is correct
29 Correct 113 ms 61768 KB Output is correct
30 Correct 147 ms 61768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 70720 KB Output is correct
2 Correct 7 ms 21584 KB Output is correct
3 Correct 7 ms 21584 KB Output is correct
4 Correct 6 ms 21584 KB Output is correct
5 Correct 8 ms 22096 KB Output is correct
6 Correct 8 ms 22096 KB Output is correct
7 Correct 8 ms 22096 KB Output is correct
8 Correct 132 ms 70684 KB Output is correct
9 Correct 131 ms 70604 KB Output is correct
10 Correct 124 ms 70728 KB Output is correct
11 Correct 114 ms 67556 KB Output is correct
12 Correct 6 ms 21584 KB Output is correct
13 Correct 7 ms 21584 KB Output is correct
14 Correct 9 ms 21584 KB Output is correct
15 Correct 6 ms 21584 KB Output is correct
16 Correct 6 ms 21584 KB Output is correct
17 Correct 13 ms 22096 KB Output is correct
18 Correct 15 ms 22264 KB Output is correct
19 Correct 127 ms 70624 KB Output is correct
20 Correct 138 ms 61768 KB Output is correct
21 Correct 142 ms 67480 KB Output is correct
22 Correct 127 ms 61824 KB Output is correct
23 Correct 100 ms 48712 KB Output is correct
24 Correct 81 ms 44468 KB Output is correct
25 Correct 82 ms 48712 KB Output is correct
26 Correct 86 ms 48836 KB Output is correct
27 Correct 6 ms 21584 KB Output is correct
28 Correct 6 ms 21556 KB Output is correct
29 Correct 6 ms 21584 KB Output is correct
30 Correct 6 ms 21584 KB Output is correct
31 Correct 6 ms 21604 KB Output is correct
32 Correct 8 ms 22096 KB Output is correct
33 Correct 8 ms 22108 KB Output is correct
34 Correct 12 ms 22096 KB Output is correct
35 Correct 8 ms 22096 KB Output is correct
36 Correct 118 ms 70472 KB Output is correct
37 Correct 103 ms 67344 KB Output is correct
38 Correct 118 ms 61768 KB Output is correct
39 Correct 126 ms 61768 KB Output is correct
40 Correct 112 ms 61832 KB Output is correct
41 Correct 66 ms 48720 KB Output is correct
42 Correct 68 ms 48712 KB Output is correct
43 Correct 97 ms 67480 KB Output is correct
44 Correct 6 ms 21584 KB Output is correct
45 Correct 5 ms 21584 KB Output is correct
46 Correct 6 ms 21584 KB Output is correct
47 Correct 7 ms 21776 KB Output is correct
48 Correct 5 ms 21552 KB Output is correct
49 Correct 13 ms 22236 KB Output is correct
50 Correct 9 ms 22096 KB Output is correct
51 Correct 10 ms 22096 KB Output is correct
52 Correct 9 ms 22112 KB Output is correct
53 Correct 140 ms 70692 KB Output is correct
54 Correct 118 ms 61844 KB Output is correct
55 Correct 113 ms 61768 KB Output is correct
56 Correct 147 ms 61768 KB Output is correct
57 Correct 166 ms 72520 KB Output is correct
58 Correct 118 ms 70728 KB Output is correct
59 Correct 110 ms 67628 KB Output is correct