Submission #878118

# Submission time Handle Problem Language Result Execution time Memory
878118 2023-11-24T05:41:10 Z arashMLG Toll (BOI17_toll) C++17
100 / 100
870 ms 5852 KB
#include<bits/stdc++.h>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...) 69
#endif
using namespace std;

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

const int N = 5e4 + 23;
const int inf = 2e9;

#define F           first
#define S           second
#define pb          push_back
#define kill(x)     cout<<x<<endl, exit(0);
#define all(x)      x.begin(),x.end()
#define sz(x)       (int)x.size()
#define lc          (v << 1)
#define rc          ((v<<1) |1)

int n,m,q,k;
int beg[N],en[N];
vector<pair<pii,int>> edges;
int dist[N];
map<pii,int> mp;

int get(int a,int b) {
	if(a > b) return -1;
	if(a ==b) return 0;
	if(beg[a] == -1 || en[b] == -1) return -1;
	if(mp[{a,b}] != 0) return mp[{a,b}];
	fill(dist+a+1, dist+b+1,inf);
	dist[a] = 0;
	for(int i = beg[a]; i <= min(en[b],m-1); i ++) {
		auto [e,w] = edges[i];
		dist[e.S] = min(dist[e.S],dist[e.F] +w); 
	}
	mp[{a,b}] = (dist[b] >= inf ? -1 : dist[b]);
	return mp[{a,b}];
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(false);
	cin>>k>>n>>m>>q;
	for(int i = 0 ;i < m ;i ++) {
		int x,y,w;cin>>x>>y>>w;
		edges.pb({{x,y},w});
	}
	sort(all(edges));
	int i = 0;
	fill(beg,beg+N,-1); fill(en,en+N,-1);
	for(auto [e,_] : edges) {
		auto [x,y] = e;
		if(beg[x] == -1) beg[x] =i;
		en[y] = i;
		i++;
	}
	while(q--) {
		int a,b; cin>>a>>b;
		cout<< get(a,b) << '\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 661 ms 2308 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 637 ms 2020 KB Output is correct
9 Correct 428 ms 2156 KB Output is correct
10 Correct 2 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 819 ms 2712 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 696 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 5 ms 860 KB Output is correct
8 Correct 5 ms 860 KB Output is correct
9 Correct 870 ms 1996 KB Output is correct
10 Correct 830 ms 5064 KB Output is correct
11 Correct 844 ms 2800 KB Output is correct
12 Correct 481 ms 2300 KB Output is correct
13 Correct 270 ms 3600 KB Output is correct
14 Correct 211 ms 2000 KB Output is correct
15 Correct 145 ms 2004 KB Output is correct
16 Correct 160 ms 2004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 1112 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 856 KB Output is correct
7 Correct 1 ms 740 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 2 ms 860 KB Output is correct
10 Correct 26 ms 2512 KB Output is correct
11 Correct 32 ms 3276 KB Output is correct
12 Correct 40 ms 4868 KB Output is correct
13 Correct 46 ms 5060 KB Output is correct
14 Correct 34 ms 3524 KB Output is correct
15 Correct 24 ms 2772 KB Output is correct
16 Correct 22 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 1112 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 856 KB Output is correct
7 Correct 1 ms 740 KB Output is correct
8 Correct 2 ms 860 KB Output is correct
9 Correct 2 ms 860 KB Output is correct
10 Correct 26 ms 2512 KB Output is correct
11 Correct 32 ms 3276 KB Output is correct
12 Correct 40 ms 4868 KB Output is correct
13 Correct 46 ms 5060 KB Output is correct
14 Correct 34 ms 3524 KB Output is correct
15 Correct 24 ms 2772 KB Output is correct
16 Correct 22 ms 2772 KB Output is correct
17 Correct 195 ms 3272 KB Output is correct
18 Correct 1 ms 860 KB Output is correct
19 Correct 0 ms 604 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 0 ms 856 KB Output is correct
23 Correct 6 ms 1000 KB Output is correct
24 Correct 6 ms 984 KB Output is correct
25 Correct 11 ms 1112 KB Output is correct
26 Correct 6 ms 1116 KB Output is correct
27 Correct 201 ms 2512 KB Output is correct
28 Correct 206 ms 4764 KB Output is correct
29 Correct 224 ms 5840 KB Output is correct
30 Correct 182 ms 3768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 661 ms 2308 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 2 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 637 ms 2020 KB Output is correct
9 Correct 428 ms 2156 KB Output is correct
10 Correct 2 ms 856 KB Output is correct
11 Correct 819 ms 2712 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 0 ms 696 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 5 ms 860 KB Output is correct
18 Correct 5 ms 860 KB Output is correct
19 Correct 870 ms 1996 KB Output is correct
20 Correct 830 ms 5064 KB Output is correct
21 Correct 844 ms 2800 KB Output is correct
22 Correct 481 ms 2300 KB Output is correct
23 Correct 270 ms 3600 KB Output is correct
24 Correct 211 ms 2000 KB Output is correct
25 Correct 145 ms 2004 KB Output is correct
26 Correct 160 ms 2004 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 1112 KB Output is correct
30 Correct 0 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 1 ms 856 KB Output is correct
33 Correct 1 ms 740 KB Output is correct
34 Correct 2 ms 860 KB Output is correct
35 Correct 2 ms 860 KB Output is correct
36 Correct 26 ms 2512 KB Output is correct
37 Correct 32 ms 3276 KB Output is correct
38 Correct 40 ms 4868 KB Output is correct
39 Correct 46 ms 5060 KB Output is correct
40 Correct 34 ms 3524 KB Output is correct
41 Correct 24 ms 2772 KB Output is correct
42 Correct 22 ms 2772 KB Output is correct
43 Correct 195 ms 3272 KB Output is correct
44 Correct 1 ms 860 KB Output is correct
45 Correct 0 ms 604 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 0 ms 604 KB Output is correct
48 Correct 0 ms 856 KB Output is correct
49 Correct 6 ms 1000 KB Output is correct
50 Correct 6 ms 984 KB Output is correct
51 Correct 11 ms 1112 KB Output is correct
52 Correct 6 ms 1116 KB Output is correct
53 Correct 201 ms 2512 KB Output is correct
54 Correct 206 ms 4764 KB Output is correct
55 Correct 224 ms 5840 KB Output is correct
56 Correct 182 ms 3768 KB Output is correct
57 Correct 836 ms 5852 KB Output is correct
58 Correct 629 ms 3008 KB Output is correct
59 Correct 634 ms 3812 KB Output is correct