Submission #365593

# Submission time Handle Problem Language Result Execution time Memory
365593 2021-02-11T22:30:24 Z LucaDantas Toll (BOI17_toll) C++17
100 / 100
378 ms 13932 KB
#include<cstdio>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
using namespace std;

constexpr int maxn = 1e5+10, inf = 0x3f3f3f3f;

vector<pair<int,int>> g[maxn], rev_g[maxn], query[maxn];

int K, dist[maxn][5], ans[maxn];

void min_self(int& a, int b) {a = min(a,b);}

void solve(int l, int r) {
	if(l == r) return;
	int m = (l+r) >> 1;
	solve(l, m); solve(m+1, r);

	for(int i = K*l; i < K*(r+1); i++)
		for(int j = 0; j < K; j++)
			dist[i][j] = inf;

	for(int x = K*m; x < K*(m+1); x++) {
		dist[x][x%K] = 0;
		queue<int> q;

		// andando pra esquerda
		q.push(x);
		while(q.size()) {
			int u = q.front(); q.pop();
			for(auto pp : rev_g[u]) {
				auto [v, w] = pp;
				if(v/K < l) continue;
				if(dist[v][x%K] == inf) q.push(v);
				min_self(dist[v][x%K], dist[u][x%K] + w);
			}
		}

		// andando pra direita
		q.push(x);
		while(q.size()) {
			int u = q.front(); q.pop();
			for(auto pp : g[u]) {
				auto [v, w] = pp;
				if(v/K > r) continue;
				if(dist[v][x%K] == inf) q.push(v);
				min_self(dist[v][x%K], dist[u][x%K] + w);
			}
		}
	}

	for(int i = K*l; i < K*(m+1); i++) {
		while(query[i].size() && query[i].back().first/K <= r) {
			auto [b, id] = query[i].back();
			query[i].pop_back();
			for(int x = 0; x < K; x++)
				min_self(ans[id], dist[i][x]+dist[b][x]);
		}
	}
}

int main() {
	int n, m, q; scanf("%d %d %d %d", &K, &n, &m, &q);
	for(int i = 0, a, b, c; i < m; i++) {
		scanf("%d %d %d", &a, &b, &c);
		g[a].push_back({b, c});
		rev_g[b].push_back({a, c});
	}
	for(int i = 0; i < q; i++) {
		int a, b; scanf("%d %d", &a, &b);
		query[a].push_back({b, i});
		ans[i] = inf;
	}
	for(int i = 0; i < n; i++)
		sort(query[i].begin(), query[i].end()), reverse(query[i].begin(), query[i].end());
	solve(0, (n-1)/K);
	for(int i = 0; i < q; i++)
		printf("%d\n", ans[i]<inf?ans[i]:-1);
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:65:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   65 |  int n, m, q; scanf("%d %d %d %d", &K, &n, &m, &q);
      |               ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:67:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   67 |   scanf("%d %d %d", &a, &b, &c);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:72:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   72 |   int a, b; scanf("%d %d", &a, &b);
      |             ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 87 ms 11884 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 6 ms 7404 KB Output is correct
6 Correct 6 ms 7404 KB Output is correct
7 Correct 7 ms 7404 KB Output is correct
8 Correct 65 ms 11756 KB Output is correct
9 Correct 58 ms 11628 KB Output is correct
10 Correct 16 ms 8684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 154 ms 11824 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 6 ms 7404 KB Output is correct
7 Correct 10 ms 7660 KB Output is correct
8 Correct 11 ms 7788 KB Output is correct
9 Correct 62 ms 11892 KB Output is correct
10 Correct 233 ms 13676 KB Output is correct
11 Correct 141 ms 11884 KB Output is correct
12 Correct 91 ms 11500 KB Output is correct
13 Correct 300 ms 13420 KB Output is correct
14 Correct 138 ms 11244 KB Output is correct
15 Correct 148 ms 10732 KB Output is correct
16 Correct 154 ms 10604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 6 ms 7404 KB Output is correct
7 Correct 6 ms 7404 KB Output is correct
8 Correct 10 ms 7532 KB Output is correct
9 Correct 8 ms 7532 KB Output is correct
10 Correct 58 ms 11500 KB Output is correct
11 Correct 171 ms 11500 KB Output is correct
12 Correct 233 ms 13292 KB Output is correct
13 Correct 258 ms 13932 KB Output is correct
14 Correct 246 ms 12524 KB Output is correct
15 Correct 149 ms 10604 KB Output is correct
16 Correct 146 ms 10604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 7404 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 5 ms 7404 KB Output is correct
6 Correct 6 ms 7404 KB Output is correct
7 Correct 6 ms 7404 KB Output is correct
8 Correct 10 ms 7532 KB Output is correct
9 Correct 8 ms 7532 KB Output is correct
10 Correct 58 ms 11500 KB Output is correct
11 Correct 171 ms 11500 KB Output is correct
12 Correct 233 ms 13292 KB Output is correct
13 Correct 258 ms 13932 KB Output is correct
14 Correct 246 ms 12524 KB Output is correct
15 Correct 149 ms 10604 KB Output is correct
16 Correct 146 ms 10604 KB Output is correct
17 Correct 179 ms 11628 KB Output is correct
18 Correct 6 ms 7404 KB Output is correct
19 Correct 6 ms 7404 KB Output is correct
20 Correct 6 ms 7404 KB Output is correct
21 Correct 6 ms 7404 KB Output is correct
22 Correct 5 ms 7404 KB Output is correct
23 Correct 7 ms 7532 KB Output is correct
24 Correct 8 ms 7532 KB Output is correct
25 Correct 10 ms 7532 KB Output is correct
26 Correct 9 ms 7532 KB Output is correct
27 Correct 61 ms 11628 KB Output is correct
28 Correct 261 ms 13420 KB Output is correct
29 Correct 261 ms 13804 KB Output is correct
30 Correct 196 ms 12524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 11884 KB Output is correct
2 Correct 5 ms 7404 KB Output is correct
3 Correct 5 ms 7404 KB Output is correct
4 Correct 5 ms 7404 KB Output is correct
5 Correct 6 ms 7404 KB Output is correct
6 Correct 6 ms 7404 KB Output is correct
7 Correct 7 ms 7404 KB Output is correct
8 Correct 65 ms 11756 KB Output is correct
9 Correct 58 ms 11628 KB Output is correct
10 Correct 16 ms 8684 KB Output is correct
11 Correct 154 ms 11824 KB Output is correct
12 Correct 5 ms 7404 KB Output is correct
13 Correct 5 ms 7404 KB Output is correct
14 Correct 5 ms 7404 KB Output is correct
15 Correct 5 ms 7404 KB Output is correct
16 Correct 6 ms 7404 KB Output is correct
17 Correct 10 ms 7660 KB Output is correct
18 Correct 11 ms 7788 KB Output is correct
19 Correct 62 ms 11892 KB Output is correct
20 Correct 233 ms 13676 KB Output is correct
21 Correct 141 ms 11884 KB Output is correct
22 Correct 91 ms 11500 KB Output is correct
23 Correct 300 ms 13420 KB Output is correct
24 Correct 138 ms 11244 KB Output is correct
25 Correct 148 ms 10732 KB Output is correct
26 Correct 154 ms 10604 KB Output is correct
27 Correct 5 ms 7404 KB Output is correct
28 Correct 5 ms 7404 KB Output is correct
29 Correct 5 ms 7404 KB Output is correct
30 Correct 5 ms 7404 KB Output is correct
31 Correct 5 ms 7404 KB Output is correct
32 Correct 6 ms 7404 KB Output is correct
33 Correct 6 ms 7404 KB Output is correct
34 Correct 10 ms 7532 KB Output is correct
35 Correct 8 ms 7532 KB Output is correct
36 Correct 58 ms 11500 KB Output is correct
37 Correct 171 ms 11500 KB Output is correct
38 Correct 233 ms 13292 KB Output is correct
39 Correct 258 ms 13932 KB Output is correct
40 Correct 246 ms 12524 KB Output is correct
41 Correct 149 ms 10604 KB Output is correct
42 Correct 146 ms 10604 KB Output is correct
43 Correct 179 ms 11628 KB Output is correct
44 Correct 6 ms 7404 KB Output is correct
45 Correct 6 ms 7404 KB Output is correct
46 Correct 6 ms 7404 KB Output is correct
47 Correct 6 ms 7404 KB Output is correct
48 Correct 5 ms 7404 KB Output is correct
49 Correct 7 ms 7532 KB Output is correct
50 Correct 8 ms 7532 KB Output is correct
51 Correct 10 ms 7532 KB Output is correct
52 Correct 9 ms 7532 KB Output is correct
53 Correct 61 ms 11628 KB Output is correct
54 Correct 261 ms 13420 KB Output is correct
55 Correct 261 ms 13804 KB Output is correct
56 Correct 196 ms 12524 KB Output is correct
57 Correct 378 ms 13804 KB Output is correct
58 Correct 63 ms 11756 KB Output is correct
59 Correct 153 ms 11884 KB Output is correct