#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
#define sqrtn 230
#define maxn 50050
#define mxdist 500000000
vector<int> jump = {0,228,112,74,45,44};
int dist[maxn][231];
int k,no_of_vertex,no_of_edge,no_of_query;
vector<int> top;
vector<int> visited(maxn,0);
vector<vector<pair<int,int> > > adj(maxn);
vector<int> ipos(maxn,0);
void topsort(int node){
visited[node] = 1;
for(auto k:adj[node]){
if(visited[k.f]==1){
continue;
}
topsort(k.f);
}
top.push_back(node);
}
void find(int node){
vector<pair<int,int> > curr;
vector<int> distt(231,INT_MAX);
for(int i=node;i<min(no_of_vertex,node+231);i++){
curr.push_back({ipos[i],i});
}
distt[0] = 0;
sort(curr.begin(),curr.end());
for(int i=0;i<curr.size();i++){
for(auto kk:adj[curr[i].s]){
if(kk.f>node+230){
continue;
}
distt[kk.f-node] = min(distt[kk.f-node],distt[curr[i].s-node]+kk.s);
}
}
for(int i=0;i<231;i++){
dist[node][i] = INT_MAX;
}
for(int i=0;i<curr.size();i++){
dist[node][i] = distt[i];
}
return;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> k >> no_of_vertex >> no_of_edge >> no_of_query;
int input1,input2,input3;
for(int i=0;i<no_of_edge;i++){
cin >> input1 >> input2 >> input3;
adj[input1].push_back({input2,input3});
}
// for (int i = 0; i < no_of_vertex; i++)
// sort(adj[i].begin(), adj[i].end());
for(int i=0;i<no_of_vertex;i++){
if(visited[i]==0){
topsort(i);
}
}
reverse(top.begin(),top.end());
for(int i=0;i<top.size();i++){
ipos[top[i]] = i;
}
for(int i=0;i<no_of_vertex;i++){
find(i);
}
for(int i=0;i<no_of_query;i++){
cin >> input1 >> input2;
vector<int> currdist(k,INT_MAX);
if(input1+230>=input2){
if(dist[input1][input2-input1]>=INT_MAX){
cout << -1 << "\n";
}
else{
cout << dist[input1][input2-input1] << "\n";
}
continue;
}
int q = input1/k;
q++;
for(int j=0;j<k;j++){
currdist[j] = dist[input1][q*k+j-input1];
}
while(1){
if(q*k+230>=input2){
int mini = INT_MAX;
for(int j=0;j<k;j++){
mini = min(mini,dist[q*k+j][input2-q*k-j]+currdist[j]);
}
if(mini==INT_MAX){
cout << -1 << "\n";
}
else{
cout << mini << "\n";
}
break;
}
vector<int> next(k,INT_MAX);
for(int j=0;j<k;j++){
for(int a=0;a<k;a++){
next[a] = min(next[a],dist[q*k+j][k*jump[k]+a-j]+currdist[j]);
}
}
// swap(currdist, next);
currdist = next;
q += jump[k];
}
}
}
Compilation message
toll.cpp: In function 'void find(long long int)':
toll.cpp:34:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<curr.size();i++){
~^~~~~~~~~~~~
toll.cpp:45:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<curr.size();i++){
~^~~~~~~~~~~~
toll.cpp: In function 'int32_t main()':
toll.cpp:67:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<top.size();i++){
~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
666 ms |
97904 KB |
Output is correct |
2 |
Correct |
6 ms |
2304 KB |
Output is correct |
3 |
Correct |
6 ms |
2304 KB |
Output is correct |
4 |
Correct |
6 ms |
2304 KB |
Output is correct |
5 |
Correct |
17 ms |
4224 KB |
Output is correct |
6 |
Correct |
18 ms |
4208 KB |
Output is correct |
7 |
Correct |
21 ms |
4172 KB |
Output is correct |
8 |
Correct |
705 ms |
95820 KB |
Output is correct |
9 |
Correct |
895 ms |
95548 KB |
Output is correct |
10 |
Correct |
432 ms |
93404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
731 ms |
97140 KB |
Output is correct |
2 |
Correct |
6 ms |
2304 KB |
Output is correct |
3 |
Correct |
6 ms |
2304 KB |
Output is correct |
4 |
Correct |
6 ms |
2304 KB |
Output is correct |
5 |
Correct |
6 ms |
2304 KB |
Output is correct |
6 |
Correct |
7 ms |
2304 KB |
Output is correct |
7 |
Correct |
20 ms |
4224 KB |
Output is correct |
8 |
Correct |
22 ms |
4352 KB |
Output is correct |
9 |
Correct |
629 ms |
95860 KB |
Output is correct |
10 |
Correct |
844 ms |
99556 KB |
Output is correct |
11 |
Correct |
727 ms |
98176 KB |
Output is correct |
12 |
Correct |
953 ms |
96112 KB |
Output is correct |
13 |
Correct |
593 ms |
63436 KB |
Output is correct |
14 |
Correct |
501 ms |
60772 KB |
Output is correct |
15 |
Correct |
561 ms |
60076 KB |
Output is correct |
16 |
Correct |
561 ms |
60000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2304 KB |
Output is correct |
2 |
Correct |
7 ms |
2304 KB |
Output is correct |
3 |
Correct |
7 ms |
2304 KB |
Output is correct |
4 |
Correct |
6 ms |
2304 KB |
Output is correct |
5 |
Correct |
6 ms |
2304 KB |
Output is correct |
6 |
Correct |
17 ms |
4224 KB |
Output is correct |
7 |
Correct |
17 ms |
4224 KB |
Output is correct |
8 |
Correct |
21 ms |
4352 KB |
Output is correct |
9 |
Correct |
20 ms |
4344 KB |
Output is correct |
10 |
Correct |
551 ms |
95644 KB |
Output is correct |
11 |
Correct |
688 ms |
97456 KB |
Output is correct |
12 |
Correct |
791 ms |
98928 KB |
Output is correct |
13 |
Correct |
759 ms |
99316 KB |
Output is correct |
14 |
Correct |
813 ms |
97652 KB |
Output is correct |
15 |
Correct |
528 ms |
59768 KB |
Output is correct |
16 |
Correct |
543 ms |
59640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2304 KB |
Output is correct |
2 |
Correct |
7 ms |
2304 KB |
Output is correct |
3 |
Correct |
7 ms |
2304 KB |
Output is correct |
4 |
Correct |
6 ms |
2304 KB |
Output is correct |
5 |
Correct |
6 ms |
2304 KB |
Output is correct |
6 |
Correct |
17 ms |
4224 KB |
Output is correct |
7 |
Correct |
17 ms |
4224 KB |
Output is correct |
8 |
Correct |
21 ms |
4352 KB |
Output is correct |
9 |
Correct |
20 ms |
4344 KB |
Output is correct |
10 |
Correct |
551 ms |
95644 KB |
Output is correct |
11 |
Correct |
688 ms |
97456 KB |
Output is correct |
12 |
Correct |
791 ms |
98928 KB |
Output is correct |
13 |
Correct |
759 ms |
99316 KB |
Output is correct |
14 |
Correct |
813 ms |
97652 KB |
Output is correct |
15 |
Correct |
528 ms |
59768 KB |
Output is correct |
16 |
Correct |
543 ms |
59640 KB |
Output is correct |
17 |
Correct |
736 ms |
97692 KB |
Output is correct |
18 |
Correct |
6 ms |
2304 KB |
Output is correct |
19 |
Correct |
6 ms |
2304 KB |
Output is correct |
20 |
Correct |
5 ms |
2304 KB |
Output is correct |
21 |
Correct |
6 ms |
2304 KB |
Output is correct |
22 |
Correct |
6 ms |
2432 KB |
Output is correct |
23 |
Correct |
19 ms |
4352 KB |
Output is correct |
24 |
Correct |
18 ms |
4344 KB |
Output is correct |
25 |
Correct |
21 ms |
4352 KB |
Output is correct |
26 |
Correct |
20 ms |
4352 KB |
Output is correct |
27 |
Correct |
622 ms |
95524 KB |
Output is correct |
28 |
Correct |
823 ms |
99112 KB |
Output is correct |
29 |
Correct |
936 ms |
99448 KB |
Output is correct |
30 |
Correct |
852 ms |
97396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
666 ms |
97904 KB |
Output is correct |
2 |
Correct |
6 ms |
2304 KB |
Output is correct |
3 |
Correct |
6 ms |
2304 KB |
Output is correct |
4 |
Correct |
6 ms |
2304 KB |
Output is correct |
5 |
Correct |
17 ms |
4224 KB |
Output is correct |
6 |
Correct |
18 ms |
4208 KB |
Output is correct |
7 |
Correct |
21 ms |
4172 KB |
Output is correct |
8 |
Correct |
705 ms |
95820 KB |
Output is correct |
9 |
Correct |
895 ms |
95548 KB |
Output is correct |
10 |
Correct |
432 ms |
93404 KB |
Output is correct |
11 |
Correct |
731 ms |
97140 KB |
Output is correct |
12 |
Correct |
6 ms |
2304 KB |
Output is correct |
13 |
Correct |
6 ms |
2304 KB |
Output is correct |
14 |
Correct |
6 ms |
2304 KB |
Output is correct |
15 |
Correct |
6 ms |
2304 KB |
Output is correct |
16 |
Correct |
7 ms |
2304 KB |
Output is correct |
17 |
Correct |
20 ms |
4224 KB |
Output is correct |
18 |
Correct |
22 ms |
4352 KB |
Output is correct |
19 |
Correct |
629 ms |
95860 KB |
Output is correct |
20 |
Correct |
844 ms |
99556 KB |
Output is correct |
21 |
Correct |
727 ms |
98176 KB |
Output is correct |
22 |
Correct |
953 ms |
96112 KB |
Output is correct |
23 |
Correct |
593 ms |
63436 KB |
Output is correct |
24 |
Correct |
501 ms |
60772 KB |
Output is correct |
25 |
Correct |
561 ms |
60076 KB |
Output is correct |
26 |
Correct |
561 ms |
60000 KB |
Output is correct |
27 |
Correct |
6 ms |
2304 KB |
Output is correct |
28 |
Correct |
7 ms |
2304 KB |
Output is correct |
29 |
Correct |
7 ms |
2304 KB |
Output is correct |
30 |
Correct |
6 ms |
2304 KB |
Output is correct |
31 |
Correct |
6 ms |
2304 KB |
Output is correct |
32 |
Correct |
17 ms |
4224 KB |
Output is correct |
33 |
Correct |
17 ms |
4224 KB |
Output is correct |
34 |
Correct |
21 ms |
4352 KB |
Output is correct |
35 |
Correct |
20 ms |
4344 KB |
Output is correct |
36 |
Correct |
551 ms |
95644 KB |
Output is correct |
37 |
Correct |
688 ms |
97456 KB |
Output is correct |
38 |
Correct |
791 ms |
98928 KB |
Output is correct |
39 |
Correct |
759 ms |
99316 KB |
Output is correct |
40 |
Correct |
813 ms |
97652 KB |
Output is correct |
41 |
Correct |
528 ms |
59768 KB |
Output is correct |
42 |
Correct |
543 ms |
59640 KB |
Output is correct |
43 |
Correct |
736 ms |
97692 KB |
Output is correct |
44 |
Correct |
6 ms |
2304 KB |
Output is correct |
45 |
Correct |
6 ms |
2304 KB |
Output is correct |
46 |
Correct |
5 ms |
2304 KB |
Output is correct |
47 |
Correct |
6 ms |
2304 KB |
Output is correct |
48 |
Correct |
6 ms |
2432 KB |
Output is correct |
49 |
Correct |
19 ms |
4352 KB |
Output is correct |
50 |
Correct |
18 ms |
4344 KB |
Output is correct |
51 |
Correct |
21 ms |
4352 KB |
Output is correct |
52 |
Correct |
20 ms |
4352 KB |
Output is correct |
53 |
Correct |
622 ms |
95524 KB |
Output is correct |
54 |
Correct |
823 ms |
99112 KB |
Output is correct |
55 |
Correct |
936 ms |
99448 KB |
Output is correct |
56 |
Correct |
852 ms |
97396 KB |
Output is correct |
57 |
Correct |
986 ms |
98964 KB |
Output is correct |
58 |
Correct |
670 ms |
95092 KB |
Output is correct |
59 |
Correct |
745 ms |
97264 KB |
Output is correct |