#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, m;
cin >> n >> m;
vector<vector<long long>> tempadg(n, vector<long long>(n, -1));
vector<pair<long long, long long>> adg[n];
for(long long i=0; i<m; i++){
long long a, b;
long long c;
cin >> a >> b >> c;
if(tempadg[a-1][b-1]==-1){
tempadg[a-1][b-1]=c;
}
else{
tempadg[a-1][b-1]=min(tempadg[a-1][b-1], c);
}
}
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(tempadg[i][j]!=-1){
adg[i].push_back({j, tempadg[i][j]});
}
}
}
long long k, q;
cin >> k >> q;
vector<vector<long long>> shortest(n, vector<long long>(n, 100000000000000043LL));
for(long long i=0; i<n; i++){
queue<pair<long long, long long>> que; //pos, cnt
que.push({i, 0});
shortest[i][i]=0;
while(!que.empty()){
pair<long long, long long> cur = que.front();
que.pop();
for(auto u : adg[cur.first]){
if(shortest[i][u.first]>shortest[i][cur.first]+u.second && cur.second+1<=k){
shortest[i][u.first]=min(shortest[i][u.first], shortest[i][cur.first]+u.second);
que.push({u.first, cur.second+1});
}
}
}
}
for(long long i=0; i<q; i++){
long long a, b;
cin >> a >> b;
cout << (shortest[a-1][b-1]==100000000000000043LL ? -1 : shortest[a-1][b-1]) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Incorrect |
2 ms |
472 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |