답안 #1115840

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1115840 2024-11-21T02:36:27 Z kustizus Toll (BOI17_toll) C++17
100 / 100
161 ms 72408 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();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 70728 KB Output is correct
2 Correct 7 ms 21596 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 138 ms 70728 KB Output is correct
9 Correct 130 ms 70692 KB Output is correct
10 Correct 123 ms 70728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 67476 KB Output is correct
2 Correct 6 ms 21584 KB Output is correct
3 Correct 9 ms 21604 KB Output is correct
4 Correct 9 ms 21584 KB Output is correct
5 Correct 6 ms 21760 KB Output is correct
6 Correct 6 ms 21584 KB Output is correct
7 Correct 12 ms 22352 KB Output is correct
8 Correct 19 ms 22184 KB Output is correct
9 Correct 123 ms 70724 KB Output is correct
10 Correct 142 ms 61896 KB Output is correct
11 Correct 119 ms 67664 KB Output is correct
12 Correct 126 ms 61768 KB Output is correct
13 Correct 97 ms 48832 KB Output is correct
14 Correct 81 ms 44624 KB Output is correct
15 Correct 82 ms 48712 KB Output is correct
16 Correct 80 ms 48840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21584 KB Output is correct
2 Correct 6 ms 21596 KB Output is correct
3 Correct 6 ms 21584 KB Output is correct
4 Correct 6 ms 21752 KB Output is correct
5 Correct 6 ms 21584 KB Output is correct
6 Correct 8 ms 22096 KB Output is correct
7 Correct 7 ms 22064 KB Output is correct
8 Correct 12 ms 22264 KB Output is correct
9 Correct 8 ms 22008 KB Output is correct
10 Correct 114 ms 70728 KB Output is correct
11 Correct 103 ms 67476 KB Output is correct
12 Correct 116 ms 61836 KB Output is correct
13 Correct 127 ms 61768 KB Output is correct
14 Correct 115 ms 61768 KB Output is correct
15 Correct 65 ms 48720 KB Output is correct
16 Correct 67 ms 48832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 21584 KB Output is correct
2 Correct 6 ms 21596 KB Output is correct
3 Correct 6 ms 21584 KB Output is correct
4 Correct 6 ms 21752 KB Output is correct
5 Correct 6 ms 21584 KB Output is correct
6 Correct 8 ms 22096 KB Output is correct
7 Correct 7 ms 22064 KB Output is correct
8 Correct 12 ms 22264 KB Output is correct
9 Correct 8 ms 22008 KB Output is correct
10 Correct 114 ms 70728 KB Output is correct
11 Correct 103 ms 67476 KB Output is correct
12 Correct 116 ms 61836 KB Output is correct
13 Correct 127 ms 61768 KB Output is correct
14 Correct 115 ms 61768 KB Output is correct
15 Correct 65 ms 48720 KB Output is correct
16 Correct 67 ms 48832 KB Output is correct
17 Correct 99 ms 67400 KB Output is correct
18 Correct 8 ms 21756 KB Output is correct
19 Correct 6 ms 21584 KB Output is correct
20 Correct 6 ms 21584 KB Output is correct
21 Correct 6 ms 21584 KB Output is correct
22 Correct 5 ms 21584 KB Output is correct
23 Correct 13 ms 22048 KB Output is correct
24 Correct 9 ms 22064 KB Output is correct
25 Correct 10 ms 22096 KB Output is correct
26 Correct 9 ms 22096 KB Output is correct
27 Correct 141 ms 70484 KB Output is correct
28 Correct 116 ms 61704 KB Output is correct
29 Correct 116 ms 61776 KB Output is correct
30 Correct 152 ms 61768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 70728 KB Output is correct
2 Correct 7 ms 21596 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 138 ms 70728 KB Output is correct
9 Correct 130 ms 70692 KB Output is correct
10 Correct 123 ms 70728 KB Output is correct
11 Correct 114 ms 67476 KB Output is correct
12 Correct 6 ms 21584 KB Output is correct
13 Correct 9 ms 21604 KB Output is correct
14 Correct 9 ms 21584 KB Output is correct
15 Correct 6 ms 21760 KB Output is correct
16 Correct 6 ms 21584 KB Output is correct
17 Correct 12 ms 22352 KB Output is correct
18 Correct 19 ms 22184 KB Output is correct
19 Correct 123 ms 70724 KB Output is correct
20 Correct 142 ms 61896 KB Output is correct
21 Correct 119 ms 67664 KB Output is correct
22 Correct 126 ms 61768 KB Output is correct
23 Correct 97 ms 48832 KB Output is correct
24 Correct 81 ms 44624 KB Output is correct
25 Correct 82 ms 48712 KB Output is correct
26 Correct 80 ms 48840 KB Output is correct
27 Correct 6 ms 21584 KB Output is correct
28 Correct 6 ms 21596 KB Output is correct
29 Correct 6 ms 21584 KB Output is correct
30 Correct 6 ms 21752 KB Output is correct
31 Correct 6 ms 21584 KB Output is correct
32 Correct 8 ms 22096 KB Output is correct
33 Correct 7 ms 22064 KB Output is correct
34 Correct 12 ms 22264 KB Output is correct
35 Correct 8 ms 22008 KB Output is correct
36 Correct 114 ms 70728 KB Output is correct
37 Correct 103 ms 67476 KB Output is correct
38 Correct 116 ms 61836 KB Output is correct
39 Correct 127 ms 61768 KB Output is correct
40 Correct 115 ms 61768 KB Output is correct
41 Correct 65 ms 48720 KB Output is correct
42 Correct 67 ms 48832 KB Output is correct
43 Correct 99 ms 67400 KB Output is correct
44 Correct 8 ms 21756 KB Output is correct
45 Correct 6 ms 21584 KB Output is correct
46 Correct 6 ms 21584 KB Output is correct
47 Correct 6 ms 21584 KB Output is correct
48 Correct 5 ms 21584 KB Output is correct
49 Correct 13 ms 22048 KB Output is correct
50 Correct 9 ms 22064 KB Output is correct
51 Correct 10 ms 22096 KB Output is correct
52 Correct 9 ms 22096 KB Output is correct
53 Correct 141 ms 70484 KB Output is correct
54 Correct 116 ms 61704 KB Output is correct
55 Correct 116 ms 61776 KB Output is correct
56 Correct 152 ms 61768 KB Output is correct
57 Correct 161 ms 72408 KB Output is correct
58 Correct 122 ms 70544 KB Output is correct
59 Correct 109 ms 67656 KB Output is correct