#include <bits/stdc++.h>
using namespace std;
#define SZ(s) (int)s.size()
#define ff first
#define ss second
int k, n, m, o;
vector <vector <pair<int,int>>> v;
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> k >> n >> m >> o;
v.resize(n+1);
vector <int> p(n+1,0);
for(int i = 1; i <= m; i++){
int u1, u2, s;
cin >> u1 >> u2 >> s;
v[u1].push_back({u2,s});
}
bool tr = 0;
vector <int> dis1(n+1, 1e9);
if(o > 3000 and k != 1){
dis1[0] = 0;
for(int i = 0; i < n; i++){
for(auto j : v[i]){
dis1[j.ff] = min(dis1[j.ff], dis1[i] + j.ss);
}
}
tr = 1;
}
while(o--){
int a, b;
cin >> a >> b;
if(tr){
cout << (dis1[b] == 1e9 ? -1 : dis1[b]) << '\n';
continue;
}
vector <int> dis(n+1, 1e9);
dis[a] = 0;
for(int i = a; i < n; i++){
for(auto j : v[i]){
dis[j.ff] = min(dis[j.ff], dis[i] + j.ss);
}
}
cout << (dis[b] == 1e9 ? -1 : dis[b]) << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |