This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int NMAX(50005);
vector<pair<int, int> > G[NMAX];
ll dp[6], newDp[5];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int k, n, m, o;
cin >> k >> n >> m >> o;
for(int i = 1; i <= m; ++i){
int a, b, t;
cin >> a >> b >> t;
G[b].push_back({a, t});
}
for(int i = 1; i <= o; ++i){
int a, b;
cin >> a >> b;
int wh = b / k;
for(int j = 0; j < k; ++j)
dp[j] = LLONG_MAX / 2;
dp[b % k] = 0;
int fin = a / k;
for(int segm = wh; segm > fin; --segm){
for(int j = 0; j < k; ++j)
newDp[j] = LLONG_MAX / 2;
for(int j = 0; j < k; ++j)
{
int nod = segm * k + j;
for(auto it: G[nod])
newDp[it.first % k] = min(newDp[it.first % k], dp[j] + it.second);
}
for(int j = 0; j < k; ++j)
dp[j] = newDp[j];
}
cout << (dp[a % k] == LLONG_MAX / 2 ? - 1 : dp[a % k]) << '\n';
}
return 0;
}
# | 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... |