Submission #365634

# Submission time Handle Problem Language Result Execution time Memory
365634 2021-02-12T03:26:46 Z luciocf Toll (BOI17_toll) C++14
100 / 100
291 ms 10476 KB
#include <bits/stdc++.h>

#define ff first
#define ss second

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 5e4+10;
const ll inf = 1e18+10;

struct Query
{
	int block, a, b, ind;
};

int n, k;

ll ans[maxn];

ll dist[2][maxn];

vector<pii> grafo[2][maxn];
vector<Query> qry[maxn];

void calc_dist(int s, int q, int l, int r)
{
	for (int i = l; i <= r; i++)
		dist[q][i] = inf;

	dist[q][s] = 0;

	queue<int> fila;
	fila.push(s);

	while (!fila.empty())
	{
		int u = fila.front(); fila.pop();

		for (auto pp: grafo[q][u])
		{
			int v = pp.ff, w = pp.ss;

			if (v >= l && v <= r && dist[q][v] > dist[q][u] + 1ll*w)
			{
				dist[q][v] = dist[q][u] + 1ll*w;
				fila.push(v);
			}
		}
	}
}

void solve(int l, int r)
{
	if (l > r) return;

	int mid = (l+r)>>1;

	for (int u = k*mid; u < min(n, k*(mid+1)); u++)
	{
		calc_dist(u, 0, k*l, min(n-1, k*(r+1)-1)); calc_dist(u, 1,  k*l, min(n-1, k*(r+1)-1));

		for (int i = l; i <= mid; i++)
		{
			for (auto Q: qry[i])
			{
				if (Q.block < mid || Q.block == i) continue;
				if (Q.block > r) break;

				int a = Q.a, b = Q.b, ind = Q.ind;

				ans[ind] = min(ans[ind], dist[1][a] + dist[0][b]);
			}
		}
	}

	solve(l, mid-1); solve(mid+1, r);
}

int main(void)
{
	int m, q;
	scanf("%d %d %d %d", &k, &n, &m, &q);

	for (int i = 1; i <= m; i++)
	{
		int u, v, w;
		scanf("%d %d %d", &u, &v, &w);

		grafo[0][u].push_back({v, w});
		grafo[1][v].push_back({u, w});
	}

	for (int i = 1; i <= q; i++)
	{
		int a, b;
		scanf("%d %d", &a, &b);

		qry[a/k].push_back({b/k, a, b, i});
		ans[i] = inf;
	}

	for (int i = 0; i <= (n-1)/k; i++)
		sort(qry[i].begin(), qry[i].end(), [&] (Query a, Query b) {return a.block < b.block;});

	solve(0, (n-1)/k);

	for (int i = 1; i <= q; i++)
	{
		if (ans[i] == inf) printf("-1\n");
		else printf("%lld\n", ans[i]);
	}
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   85 |  scanf("%d %d %d %d", &k, &n, &m, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   90 |   scanf("%d %d %d", &u, &v, &w);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:99:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   99 |   scanf("%d %d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 68 ms 8172 KB Output is correct
2 Correct 3 ms 3820 KB Output is correct
3 Correct 3 ms 3820 KB Output is correct
4 Correct 3 ms 3820 KB Output is correct
5 Correct 5 ms 3948 KB Output is correct
6 Correct 4 ms 3948 KB Output is correct
7 Correct 4 ms 3948 KB Output is correct
8 Correct 57 ms 8428 KB Output is correct
9 Correct 50 ms 8172 KB Output is correct
10 Correct 19 ms 5100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 8176 KB Output is correct
2 Correct 3 ms 3820 KB Output is correct
3 Correct 3 ms 3820 KB Output is correct
4 Correct 4 ms 3820 KB Output is correct
5 Correct 3 ms 3820 KB Output is correct
6 Correct 3 ms 3820 KB Output is correct
7 Correct 7 ms 4460 KB Output is correct
8 Correct 8 ms 4460 KB Output is correct
9 Correct 56 ms 8304 KB Output is correct
10 Correct 190 ms 10216 KB Output is correct
11 Correct 120 ms 8304 KB Output is correct
12 Correct 76 ms 8168 KB Output is correct
13 Correct 202 ms 10064 KB Output is correct
14 Correct 109 ms 7916 KB Output is correct
15 Correct 115 ms 7276 KB Output is correct
16 Correct 107 ms 7148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3820 KB Output is correct
2 Correct 3 ms 3820 KB Output is correct
3 Correct 3 ms 3820 KB Output is correct
4 Correct 4 ms 3820 KB Output is correct
5 Correct 3 ms 3820 KB Output is correct
6 Correct 4 ms 3948 KB Output is correct
7 Correct 4 ms 3948 KB Output is correct
8 Correct 7 ms 4076 KB Output is correct
9 Correct 6 ms 4076 KB Output is correct
10 Correct 57 ms 7916 KB Output is correct
11 Correct 113 ms 7916 KB Output is correct
12 Correct 187 ms 9708 KB Output is correct
13 Correct 206 ms 10344 KB Output is correct
14 Correct 162 ms 8940 KB Output is correct
15 Correct 131 ms 7020 KB Output is correct
16 Correct 109 ms 7020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3820 KB Output is correct
2 Correct 3 ms 3820 KB Output is correct
3 Correct 3 ms 3820 KB Output is correct
4 Correct 4 ms 3820 KB Output is correct
5 Correct 3 ms 3820 KB Output is correct
6 Correct 4 ms 3948 KB Output is correct
7 Correct 4 ms 3948 KB Output is correct
8 Correct 7 ms 4076 KB Output is correct
9 Correct 6 ms 4076 KB Output is correct
10 Correct 57 ms 7916 KB Output is correct
11 Correct 113 ms 7916 KB Output is correct
12 Correct 187 ms 9708 KB Output is correct
13 Correct 206 ms 10344 KB Output is correct
14 Correct 162 ms 8940 KB Output is correct
15 Correct 131 ms 7020 KB Output is correct
16 Correct 109 ms 7020 KB Output is correct
17 Correct 115 ms 8044 KB Output is correct
18 Correct 3 ms 3820 KB Output is correct
19 Correct 3 ms 3820 KB Output is correct
20 Correct 3 ms 3820 KB Output is correct
21 Correct 3 ms 3852 KB Output is correct
22 Correct 3 ms 3820 KB Output is correct
23 Correct 5 ms 4076 KB Output is correct
24 Correct 6 ms 4076 KB Output is correct
25 Correct 8 ms 4204 KB Output is correct
26 Correct 7 ms 4204 KB Output is correct
27 Correct 56 ms 8044 KB Output is correct
28 Correct 191 ms 9708 KB Output is correct
29 Correct 202 ms 10220 KB Output is correct
30 Correct 155 ms 8940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 8172 KB Output is correct
2 Correct 3 ms 3820 KB Output is correct
3 Correct 3 ms 3820 KB Output is correct
4 Correct 3 ms 3820 KB Output is correct
5 Correct 5 ms 3948 KB Output is correct
6 Correct 4 ms 3948 KB Output is correct
7 Correct 4 ms 3948 KB Output is correct
8 Correct 57 ms 8428 KB Output is correct
9 Correct 50 ms 8172 KB Output is correct
10 Correct 19 ms 5100 KB Output is correct
11 Correct 119 ms 8176 KB Output is correct
12 Correct 3 ms 3820 KB Output is correct
13 Correct 3 ms 3820 KB Output is correct
14 Correct 4 ms 3820 KB Output is correct
15 Correct 3 ms 3820 KB Output is correct
16 Correct 3 ms 3820 KB Output is correct
17 Correct 7 ms 4460 KB Output is correct
18 Correct 8 ms 4460 KB Output is correct
19 Correct 56 ms 8304 KB Output is correct
20 Correct 190 ms 10216 KB Output is correct
21 Correct 120 ms 8304 KB Output is correct
22 Correct 76 ms 8168 KB Output is correct
23 Correct 202 ms 10064 KB Output is correct
24 Correct 109 ms 7916 KB Output is correct
25 Correct 115 ms 7276 KB Output is correct
26 Correct 107 ms 7148 KB Output is correct
27 Correct 3 ms 3820 KB Output is correct
28 Correct 3 ms 3820 KB Output is correct
29 Correct 3 ms 3820 KB Output is correct
30 Correct 4 ms 3820 KB Output is correct
31 Correct 3 ms 3820 KB Output is correct
32 Correct 4 ms 3948 KB Output is correct
33 Correct 4 ms 3948 KB Output is correct
34 Correct 7 ms 4076 KB Output is correct
35 Correct 6 ms 4076 KB Output is correct
36 Correct 57 ms 7916 KB Output is correct
37 Correct 113 ms 7916 KB Output is correct
38 Correct 187 ms 9708 KB Output is correct
39 Correct 206 ms 10344 KB Output is correct
40 Correct 162 ms 8940 KB Output is correct
41 Correct 131 ms 7020 KB Output is correct
42 Correct 109 ms 7020 KB Output is correct
43 Correct 115 ms 8044 KB Output is correct
44 Correct 3 ms 3820 KB Output is correct
45 Correct 3 ms 3820 KB Output is correct
46 Correct 3 ms 3820 KB Output is correct
47 Correct 3 ms 3852 KB Output is correct
48 Correct 3 ms 3820 KB Output is correct
49 Correct 5 ms 4076 KB Output is correct
50 Correct 6 ms 4076 KB Output is correct
51 Correct 8 ms 4204 KB Output is correct
52 Correct 7 ms 4204 KB Output is correct
53 Correct 56 ms 8044 KB Output is correct
54 Correct 191 ms 9708 KB Output is correct
55 Correct 202 ms 10220 KB Output is correct
56 Correct 155 ms 8940 KB Output is correct
57 Correct 291 ms 10476 KB Output is correct
58 Correct 62 ms 8300 KB Output is correct
59 Correct 132 ms 8300 KB Output is correct