Submission #714566

# Submission time Handle Problem Language Result Execution time Memory
714566 2023-03-25T05:37:02 Z parsadox2 Toll (BOI17_toll) C++14
100 / 100
1118 ms 6220 KB
#include <bits/stdc++.h>
#define pb 		push_back
#define F		first
#define S 		second
#define debug(x)    cout << #x << "= " << x << ", "
#define ll 		long long
#define fast 		ios::sync_with_stdio(false), cin.tie(0),  cout.tie(0)
#define SZ(x)         (int) x.size()
#define wall 		cout << endl;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 5e4 + 10 , maxsq = 2 , maxk = 7 , inf = 1e9;
int n , m , q , k , dis[maxn] , dis_jmp[maxn][maxk];
vector <pair<int , int>> adj[maxn];
vector <int> vec;

void Relax()
{
	for(auto u : vec)   dis[u] = inf;
	vec.clear();
}

void find_nex(int now , int to)
{
	int l = now * k , r = min(n , l + k);
	for(int i = l ; i < r ; i++)  vec.pb(i);
	if(now == to)  return;
	for(int i = l ; i < r ; i++)
		for(auto u : adj[i])
			dis[u.F] = min(dis[u.F] , dis[i] + u.S);
	find_nex(now + 1 , to);
}

void Build(int num)
{
	int l = num * k , r = min(n , l + k);
	int l2 = (num + maxsq) * k;
	for(int i = l ; i < r ; i++)
	{
		dis[i] = 0;
		find_nex(num , num + maxsq);
		for(int j = 0 ; j < k ; j++)
			dis_jmp[i][j] = dis[l2 + j];
		Relax();
	}
}

void jmp_nex(int a , int b)
{
	int l = a * k , r = min(n , l + k) , l2 = (a + maxsq) * k;
	for(int i = l ; i < r ; i++)  vec.pb(i);
	if(a == b)  return;
	for(int i = l ; i < r ; i++)
		for(int j = 0 ; j < k ; j++)
			dis[l2 + j] = min(dis[l2 + j] , dis[i] + dis_jmp[i][j]);
	jmp_nex(a + maxsq , b);
}

void Find_dis(int a , int b)
{
	int tmp = (a + maxsq) / maxsq;
	tmp *= maxsq;
	if(b <= tmp)
	{
		find_nex(a , b);
		return;
	}
	find_nex(a , tmp);
	int tmp2 = b / maxsq;
	tmp2 *= maxsq;
	jmp_nex(tmp , tmp2);
	find_nex(tmp2 , b);
}

int32_t main()
{
	fast;
	cin >> k >> n >> m >> q;
	for(int i = 0 ; i < n ; i++)  dis[i] = inf;
	for(int i = 0 ; i < m ; i++)
	{
		int v , u , c;  cin >> v >> u >> c;
		adj[v].pb({u , c});
	}
	for(int i = 0 ; (i + maxsq) * k < n ; i += maxsq)  Build(i);
	while(q--)
	{
		int v , u;  cin >> v >> u;
		if(v > u)
		{
			cout << -1 << '\n';
			continue;
		}
		dis[v] = 0;
		vec.pb(v);
		Find_dis(v / k , u / k);
		cout << (dis[u] == inf ? -1 : dis[u]) << '\n';
		Relax();
	}
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 710 ms 4932 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 3 ms 1492 KB Output is correct
6 Correct 3 ms 1580 KB Output is correct
7 Correct 3 ms 1492 KB Output is correct
8 Correct 720 ms 4856 KB Output is correct
9 Correct 678 ms 4900 KB Output is correct
10 Correct 664 ms 3404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1039 ms 5104 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 2 ms 1488 KB Output is correct
7 Correct 24 ms 1584 KB Output is correct
8 Correct 24 ms 1620 KB Output is correct
9 Correct 992 ms 4868 KB Output is correct
10 Correct 1118 ms 6016 KB Output is correct
11 Correct 1016 ms 4924 KB Output is correct
12 Correct 1094 ms 4768 KB Output is correct
13 Correct 298 ms 5320 KB Output is correct
14 Correct 223 ms 4268 KB Output is correct
15 Correct 277 ms 3880 KB Output is correct
16 Correct 261 ms 3884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 2 ms 1492 KB Output is correct
7 Correct 2 ms 1492 KB Output is correct
8 Correct 3 ms 1620 KB Output is correct
9 Correct 2 ms 1492 KB Output is correct
10 Correct 28 ms 4820 KB Output is correct
11 Correct 37 ms 4728 KB Output is correct
12 Correct 57 ms 5668 KB Output is correct
13 Correct 63 ms 5936 KB Output is correct
14 Correct 47 ms 5260 KB Output is correct
15 Correct 36 ms 3788 KB Output is correct
16 Correct 33 ms 3852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 2 ms 1492 KB Output is correct
7 Correct 2 ms 1492 KB Output is correct
8 Correct 3 ms 1620 KB Output is correct
9 Correct 2 ms 1492 KB Output is correct
10 Correct 28 ms 4820 KB Output is correct
11 Correct 37 ms 4728 KB Output is correct
12 Correct 57 ms 5668 KB Output is correct
13 Correct 63 ms 5936 KB Output is correct
14 Correct 47 ms 5260 KB Output is correct
15 Correct 36 ms 3788 KB Output is correct
16 Correct 33 ms 3852 KB Output is correct
17 Correct 222 ms 4860 KB Output is correct
18 Correct 1 ms 1492 KB Output is correct
19 Correct 1 ms 1388 KB Output is correct
20 Correct 1 ms 1492 KB Output is correct
21 Correct 1 ms 1492 KB Output is correct
22 Correct 1 ms 1492 KB Output is correct
23 Correct 7 ms 1492 KB Output is correct
24 Correct 8 ms 1600 KB Output is correct
25 Correct 11 ms 1748 KB Output is correct
26 Correct 8 ms 1608 KB Output is correct
27 Correct 227 ms 4968 KB Output is correct
28 Correct 257 ms 5784 KB Output is correct
29 Correct 282 ms 6144 KB Output is correct
30 Correct 253 ms 5420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 710 ms 4932 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 3 ms 1492 KB Output is correct
6 Correct 3 ms 1580 KB Output is correct
7 Correct 3 ms 1492 KB Output is correct
8 Correct 720 ms 4856 KB Output is correct
9 Correct 678 ms 4900 KB Output is correct
10 Correct 664 ms 3404 KB Output is correct
11 Correct 1039 ms 5104 KB Output is correct
12 Correct 1 ms 1492 KB Output is correct
13 Correct 1 ms 1492 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 1 ms 1492 KB Output is correct
16 Correct 2 ms 1488 KB Output is correct
17 Correct 24 ms 1584 KB Output is correct
18 Correct 24 ms 1620 KB Output is correct
19 Correct 992 ms 4868 KB Output is correct
20 Correct 1118 ms 6016 KB Output is correct
21 Correct 1016 ms 4924 KB Output is correct
22 Correct 1094 ms 4768 KB Output is correct
23 Correct 298 ms 5320 KB Output is correct
24 Correct 223 ms 4268 KB Output is correct
25 Correct 277 ms 3880 KB Output is correct
26 Correct 261 ms 3884 KB Output is correct
27 Correct 1 ms 1492 KB Output is correct
28 Correct 1 ms 1492 KB Output is correct
29 Correct 1 ms 1492 KB Output is correct
30 Correct 1 ms 1492 KB Output is correct
31 Correct 1 ms 1492 KB Output is correct
32 Correct 2 ms 1492 KB Output is correct
33 Correct 2 ms 1492 KB Output is correct
34 Correct 3 ms 1620 KB Output is correct
35 Correct 2 ms 1492 KB Output is correct
36 Correct 28 ms 4820 KB Output is correct
37 Correct 37 ms 4728 KB Output is correct
38 Correct 57 ms 5668 KB Output is correct
39 Correct 63 ms 5936 KB Output is correct
40 Correct 47 ms 5260 KB Output is correct
41 Correct 36 ms 3788 KB Output is correct
42 Correct 33 ms 3852 KB Output is correct
43 Correct 222 ms 4860 KB Output is correct
44 Correct 1 ms 1492 KB Output is correct
45 Correct 1 ms 1388 KB Output is correct
46 Correct 1 ms 1492 KB Output is correct
47 Correct 1 ms 1492 KB Output is correct
48 Correct 1 ms 1492 KB Output is correct
49 Correct 7 ms 1492 KB Output is correct
50 Correct 8 ms 1600 KB Output is correct
51 Correct 11 ms 1748 KB Output is correct
52 Correct 8 ms 1608 KB Output is correct
53 Correct 227 ms 4968 KB Output is correct
54 Correct 257 ms 5784 KB Output is correct
55 Correct 282 ms 6144 KB Output is correct
56 Correct 253 ms 5420 KB Output is correct
57 Correct 896 ms 6220 KB Output is correct
58 Correct 664 ms 4876 KB Output is correct
59 Correct 698 ms 4976 KB Output is correct