#include<bits/stdc++.h>
using namespace std;
#define maxn 50050
#define f first
#define s second
vector<vector<pair<int,int> > > adj(maxn);
vector<int> top;
vector<int> visited(maxn,0);
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);
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int k,no_of_vertex,no_of_edge,no_of_query;
int input1,input2,input3;
cin >> k >> no_of_vertex >> no_of_edge >> no_of_query;
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++){
if(visited[i]==0){
topsort(i);
}
}
reverse(top.begin(),top.end());
vector<int> dist(maxn,INT_MAX);
for (int i = 0; i < top.size(); i++)
ipos[top[i]] = i;
for(int i=0;i<no_of_query;i++){
cin >> input1 >> input2;
for(int j=0;j<dist.size();j++){
dist[j] = INT_MAX;
}
int pos = ipos[input1];
// for(int j=0;j<top.size();j++){
// if(top[j]==input1){
// pos = j;
// break;
// }
// }
dist[input1] = 0;
for(int j=pos;j<ipos[input2];j++){
if(dist[top[j]]==INT_MAX){
continue;
}
for(auto kk:adj[top[j]]){
dist[kk.first] = min(dist[kk.first],dist[top[j]]+kk.second);
}
}
if(dist[input2]==INT_MAX){
cout << -1 << "\n";
}
else{
cout << dist[input2] << "\n";
}
}
}
Compilation message
toll.cpp: In function 'int32_t main()':
toll.cpp:37:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < top.size(); i++)
~~^~~~~~~~~~~~
toll.cpp:41:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<dist.size();j++){
~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3076 ms |
7416 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1811 ms |
6588 KB |
Output is correct |
2 |
Correct |
6 ms |
2048 KB |
Output is correct |
3 |
Correct |
6 ms |
2048 KB |
Output is correct |
4 |
Correct |
6 ms |
2048 KB |
Output is correct |
5 |
Correct |
6 ms |
2048 KB |
Output is correct |
6 |
Correct |
6 ms |
2048 KB |
Output is correct |
7 |
Correct |
223 ms |
2424 KB |
Output is correct |
8 |
Correct |
288 ms |
2304 KB |
Output is correct |
9 |
Correct |
234 ms |
5112 KB |
Output is correct |
10 |
Execution timed out |
3069 ms |
8188 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2176 KB |
Output is correct |
2 |
Correct |
6 ms |
2176 KB |
Output is correct |
3 |
Correct |
6 ms |
2176 KB |
Output is correct |
4 |
Correct |
6 ms |
2048 KB |
Output is correct |
5 |
Correct |
6 ms |
2176 KB |
Output is correct |
6 |
Correct |
8 ms |
2176 KB |
Output is correct |
7 |
Correct |
9 ms |
2176 KB |
Output is correct |
8 |
Correct |
13 ms |
2304 KB |
Output is correct |
9 |
Correct |
9 ms |
2176 KB |
Output is correct |
10 |
Correct |
30 ms |
4992 KB |
Output is correct |
11 |
Correct |
102 ms |
6648 KB |
Output is correct |
12 |
Correct |
133 ms |
7796 KB |
Output is correct |
13 |
Correct |
127 ms |
8308 KB |
Output is correct |
14 |
Correct |
104 ms |
7064 KB |
Output is correct |
15 |
Correct |
64 ms |
5112 KB |
Output is correct |
16 |
Correct |
58 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2176 KB |
Output is correct |
2 |
Correct |
6 ms |
2176 KB |
Output is correct |
3 |
Correct |
6 ms |
2176 KB |
Output is correct |
4 |
Correct |
6 ms |
2048 KB |
Output is correct |
5 |
Correct |
6 ms |
2176 KB |
Output is correct |
6 |
Correct |
8 ms |
2176 KB |
Output is correct |
7 |
Correct |
9 ms |
2176 KB |
Output is correct |
8 |
Correct |
13 ms |
2304 KB |
Output is correct |
9 |
Correct |
9 ms |
2176 KB |
Output is correct |
10 |
Correct |
30 ms |
4992 KB |
Output is correct |
11 |
Correct |
102 ms |
6648 KB |
Output is correct |
12 |
Correct |
133 ms |
7796 KB |
Output is correct |
13 |
Correct |
127 ms |
8308 KB |
Output is correct |
14 |
Correct |
104 ms |
7064 KB |
Output is correct |
15 |
Correct |
64 ms |
5112 KB |
Output is correct |
16 |
Correct |
58 ms |
5112 KB |
Output is correct |
17 |
Correct |
1440 ms |
6964 KB |
Output is correct |
18 |
Correct |
6 ms |
2176 KB |
Output is correct |
19 |
Correct |
6 ms |
2048 KB |
Output is correct |
20 |
Correct |
6 ms |
2048 KB |
Output is correct |
21 |
Correct |
6 ms |
2048 KB |
Output is correct |
22 |
Correct |
6 ms |
2048 KB |
Output is correct |
23 |
Correct |
69 ms |
2176 KB |
Output is correct |
24 |
Correct |
84 ms |
2304 KB |
Output is correct |
25 |
Correct |
98 ms |
2304 KB |
Output is correct |
26 |
Correct |
87 ms |
2304 KB |
Output is correct |
27 |
Correct |
89 ms |
4992 KB |
Output is correct |
28 |
Correct |
1786 ms |
8052 KB |
Output is correct |
29 |
Correct |
2161 ms |
8588 KB |
Output is correct |
30 |
Correct |
503 ms |
7028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3076 ms |
7416 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |