#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
vector<pair<int, long long>> adg[n];
for(int i=0; i<m; i++){
int a, b;
long long c;
cin >> a >> b >> c;
adg[a-1].push_back({b-1, c});
}
int k, q;
cin >> k >> q;
vector<vector<long long>> shortest(n, vector<long long>(n, 100000000000000043LL));
for(int i=0; i<n; i++){
bool vsted[n]={false};
priority_queue<pair<pair<long long, int>, int>, vector<pair<pair<long long, int>, int>>, greater<pair<pair<long long, int>, int>>> pq; // {{dist, cnt}, pos}
pq.push({{0LL, 0}, i});
shortest[i][i]=0;
while(!pq.empty()){
pair<pair<long long, int>, int> cur = pq.top();
pq.pop();
if(cur.first.second>=k || vsted[cur.second]){
continue;
}
vsted[cur.second]=true;
for(auto u : adg[cur.second]){
if(shortest[i][u.first]>shortest[i][cur.second]+u.second){
shortest[i][u.first]=shortest[i][cur.second]+u.second;
pq.push({{shortest[i][u.first], cur.first.second+1}, u.first});
}
}
}
}
for(int i=0; i<q; i++){
int a, b;
cin >> a >> b;
cout << (shortest[a-1][b-1]==100000000000000043LL ? -1 : shortest[a-1][b-1]) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |