#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
long long int Dijkstra(int a, int b, vector<vector<pair<int, int>>>&listaAdy, int k) {
int N=(int)listaAdy.size();
vector<long long int>distancia(N, 1e18);
vector<int>usado(N, 0);
distancia[a]=0;
priority_queue<pair<long long int, int>>pq;
pq.push(make_pair(0, a));
while(!pq.empty()) {
int nodo=pq.top().second;
long long int distanciaAlt=-pq.top().first;
pq.pop();
if (distanciaAlt>distancia[nodo]) continue;
if (usado[nodo]==k and nodo!=b){
distancia[nodo]=Dijkstra(a, nodo, listaAdy, k-1);
usado[nodo]=k-1;
continue;
}for (pair<int, int> vecino: listaAdy[nodo]) {
int nodoVecino=vecino.first;
long long 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;
long long int res=Dijkstra(a-1, b-1, listaAdy, k);
if (res==1000000000000000000) cout<<-1<<endl;
else cout<<res<<endl;
}return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
295 ms |
480 KB |
Output is correct |
2 |
Execution timed out |
1072 ms |
340 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
61 ms |
464 KB |
Output is correct |
8 |
Correct |
66 ms |
360 KB |
Output is correct |
9 |
Execution timed out |
1073 ms |
340 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
295 ms |
480 KB |
Output is correct |
8 |
Execution timed out |
1072 ms |
340 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |