답안 #674609

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674609 2022-12-25T10:48:06 Z esomer Toll (BOI17_toll) C++17
100 / 100
1219 ms 12480 KB
#include<bits/stdc++.h>
 
using namespace std;
 
#define int long long
#define endl "\n"
 
const int MOD = 998244353;

void solve(){
	int n, o, k, m; cin >> k >> n >> m >> o;
	vector<vector<pair<int, int>>> adj(n);
	for(int i = 0; i < m; i++){
		int a, b, t; cin >> a >> b >> t;
		adj[a].push_back({b, t});
	}
	int groups = (n - 1) / k + 1;
	int sqr = sqrt(groups);
	vector<vector<int>> calc(n, vector<int> (k, 1e17));
	for(int i = 0; i < groups; i += sqr){
		for(int j = i * k; j < (i + 1) * k && j < n; j++){
			queue<int> q;
			q.push(j);
			map<int, int> d; d[j] = 0;
			while(!q.empty()){
				int x = q.front(); q.pop();
				if(x / k == i + sqr){
					calc[j][x % k] = d[x];
					continue;
				}
				if((x / k) % sqr != 0){
					calc[x][j % k] = d[x];
				}
				for(auto p : adj[x]){
					int node = p.first;
					bool gd = d.count(node);
					if(!gd) {d[node] = d[x] + p.second; q.push(node);}
					else d[node] = min(d[node], d[x] + p.second);
				}
			}
		}
	}
	while(o--){
		int a, b; cin >> a >> b;
		if(a / k >= b / k){
			cout << -1 << endl;
			continue;
		}
		queue<int> q;
		q.push(a);
		map<int, int> d; d[a] = 0;
		vector<int> dist(k, 1e17);
		int gr = -1;
		bool done = 0;
		while(!q.empty()){
			int x = q.front(); q.pop();
			if(done) continue;
			if((x / k) % sqr == 0){
				gr = x / k;
				dist[x % k] = d[x];
				continue;
			}
			if(x == b){
				cout << d[x] << endl;
				done = 1;
				continue;
			}
			for(auto p : adj[x]){
				int node = p.first;
				bool gd = d.count(node);
				if(!gd) {d[node] = d[x] + p.second; q.push(node);}
				else d[node] = min(d[node], d[x] + p.second);
			}
		}
		if(done) continue;
		if(b / k < gr){
			cout << -1 << endl;
			continue;
		}
		if(gr == -1){
			cout << -1 << endl;
			continue;
		}
		while((gr + sqr) * k <= b){
			vector<int> nw(k, 1e17);
			for(int i = 0; i < k && gr * k + i < n; i++){
				for(int j = 0; j < k; j++){
					nw[j] = min(nw[j], dist[i] + calc[gr * k + i][j]);
				}
			}
			dist = nw;
			gr += sqr;
		}
		if((b / k) % sqr == 0) cout << (dist[b % k] >= 1e10 ? -1 : dist[b % k]) << endl;
		else{
			int ans = 1e17;
			for(int i = 0; i < k; i++){
				ans = min(ans, dist[i] + calc[b][i]);
			}
			if(ans >= 1e10) cout << -1 << endl;
			else cout << ans << endl;
		}
	}
}
 
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
    //freopen("inpt.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
 
    //int tt; cin >> tt;
    int tt = 1;
    for(int t = 1; t <= tt; t++){
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 5932 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 137 ms 5816 KB Output is correct
9 Correct 37 ms 5708 KB Output is correct
10 Correct 8 ms 4260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 6600 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 9 ms 448 KB Output is correct
8 Correct 7 ms 432 KB Output is correct
9 Correct 51 ms 5828 KB Output is correct
10 Correct 133 ms 7924 KB Output is correct
11 Correct 87 ms 6736 KB Output is correct
12 Correct 77 ms 6340 KB Output is correct
13 Correct 156 ms 7564 KB Output is correct
14 Correct 70 ms 5164 KB Output is correct
15 Correct 91 ms 5072 KB Output is correct
16 Correct 89 ms 5072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 6 ms 468 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 24 ms 5804 KB Output is correct
11 Correct 57 ms 6612 KB Output is correct
12 Correct 121 ms 7856 KB Output is correct
13 Correct 128 ms 8356 KB Output is correct
14 Correct 88 ms 7116 KB Output is correct
15 Correct 81 ms 5028 KB Output is correct
16 Correct 82 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 6 ms 468 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 24 ms 5804 KB Output is correct
11 Correct 57 ms 6612 KB Output is correct
12 Correct 121 ms 7856 KB Output is correct
13 Correct 128 ms 8356 KB Output is correct
14 Correct 88 ms 7116 KB Output is correct
15 Correct 81 ms 5028 KB Output is correct
16 Correct 82 ms 5076 KB Output is correct
17 Correct 156 ms 6632 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 7 ms 448 KB Output is correct
24 Correct 11 ms 468 KB Output is correct
25 Correct 39 ms 612 KB Output is correct
26 Correct 25 ms 564 KB Output is correct
27 Correct 58 ms 6652 KB Output is correct
28 Correct 326 ms 10336 KB Output is correct
29 Correct 309 ms 10740 KB Output is correct
30 Correct 235 ms 9012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 165 ms 5932 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 137 ms 5816 KB Output is correct
9 Correct 37 ms 5708 KB Output is correct
10 Correct 8 ms 4260 KB Output is correct
11 Correct 91 ms 6600 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 9 ms 448 KB Output is correct
18 Correct 7 ms 432 KB Output is correct
19 Correct 51 ms 5828 KB Output is correct
20 Correct 133 ms 7924 KB Output is correct
21 Correct 87 ms 6736 KB Output is correct
22 Correct 77 ms 6340 KB Output is correct
23 Correct 156 ms 7564 KB Output is correct
24 Correct 70 ms 5164 KB Output is correct
25 Correct 91 ms 5072 KB Output is correct
26 Correct 89 ms 5072 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 6 ms 468 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 24 ms 5804 KB Output is correct
37 Correct 57 ms 6612 KB Output is correct
38 Correct 121 ms 7856 KB Output is correct
39 Correct 128 ms 8356 KB Output is correct
40 Correct 88 ms 7116 KB Output is correct
41 Correct 81 ms 5028 KB Output is correct
42 Correct 82 ms 5076 KB Output is correct
43 Correct 156 ms 6632 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 7 ms 448 KB Output is correct
50 Correct 11 ms 468 KB Output is correct
51 Correct 39 ms 612 KB Output is correct
52 Correct 25 ms 564 KB Output is correct
53 Correct 58 ms 6652 KB Output is correct
54 Correct 326 ms 10336 KB Output is correct
55 Correct 309 ms 10740 KB Output is correct
56 Correct 235 ms 9012 KB Output is correct
57 Correct 1219 ms 12480 KB Output is correct
58 Correct 164 ms 6760 KB Output is correct
59 Correct 393 ms 8396 KB Output is correct