#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int Dijkstra(int a, int b, vector<vector<pair<int, int>>>&listaAdy, int k) {
int N=(int)listaAdy.size();
vector<int>distancia(N, 1e9);
vector<int>usado(N, 0);
distancia[a]=0;
priority_queue<pair<int, int>>pq;
pq.push(make_pair(0, a));
while(!pq.empty()) {
int nodo=pq.top().second;
int distanciaAlt=-pq.top().first;
pq.pop();
if (distanciaAlt>distancia[nodo] or usado[nodo]>=k) continue;
for (pair<int, int> vecino: listaAdy[nodo]) {
int nodoVecino=vecino.first;
int costeVecino=vecino.second;
if (distancia[nodoVecino]>distancia[nodo]+costeVecino) {
distancia[nodoVecino]=distancia[nodo]+costeVecino;
pq.push(make_pair(-distancia[nodoVecino], nodoVecino));
usado[nodoVecino]=usado[nodo]+1;
}
}
}return distancia[b];
}
int main(){
int n, m, a, b, t, k, q;
cin>>n>>m;
vector<vector<pair<int, int>>>listaAdy(n);
for (int i=0; i<m; i++){
cin>>a>>b>>t;
listaAdy[a-1].push_back(make_pair(b-1, t));
}cin>>k>>q;
for (int i=0; i<q; i++){
cin>>a>>b;
int res=Dijkstra(a-1, b-1, listaAdy, k);
if (res==1000000000) cout<<-1<<endl;
else cout<<res<<endl;
}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 |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
344 KB |
Output is correct |
2 |
Incorrect |
20 ms |
340 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 |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 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 |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |