#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define SZ(s) (int)s.size()
#define ff first
#define ss second
ll k, n, m, o;
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> k >> n >> m >> o;
vector <vector <pair<int,ll>>> v(n);
for(int i = 1; i <= m; i++){
int u1, u2;
ll s;
cin >> u1 >> u2 >> s;
v[u1].push_back({u2,s});
}
bool tr = 0;
vector <ll> dis1(n, 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 <ll> dis(n, 1e9);
dis[a] = 0;
for(int i = a; i <= b; 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... |