#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int k, n, m, q;
cin >> k >> n >> m >> q;
vector<pair<int, int>> E[n];
while (m--) {
int x, y, w;
cin >> x >> y >> w;
E[y].push_back({x, w});
}
while (q--) {
int x, y;
cin >> x >> y;
if (x > y) {
cout << "-1\n";
continue;
}
vector<int> dp(n, (int)1e9);
dp[x] = 0;
for (int i = x + 1; i <= y; i++) {
for (auto j : E[i])
dp[i] = min(dp[i], dp[j.first] + j.second);
}
cout << (dp[y] == (int)1e9 ? -1 : dp[y]) << '\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... |