#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define F first
#define S second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forbn(i, b, n) for(int i = b; i < n; ++i)
#define sz(v) (int)v.size()
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define mp make_pair
#define at(m, k) (m.find(k) != m.end() ? m[k] : 0)
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
//typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
template <class T> inline void mineq(T &a, T b) { a = min(a, b); }
template <class T> inline void maxeq(T &a, T b) { a = max(a, b); }
const int INF = 1000 * 1000 * 1000;
const int MX = 50 * 1000 + 10;
const int mxpw = 16;
vii gr[MX][mxpw];
void solve() {
int k, n, m, o;
cin >> k >> n >> m >> o;
forn(_, m) {
int a, b, t;
cin >> a >> b >> t;
gr[a][0].eb(b, t);
}
forn(a, n)
forbn(d, 1, mxpw) {
vi mns(k, INF);
int fac = -1;
for(ii nb_t: gr[a][d - 1]) {
int nb = nb_t.F, t = nb_t.S;
for(ii nb2_t: gr[nb][d - 1]) {
int nb2 = nb2_t.F, t2 = nb2_t.S;
mineq(mns[nb2 % k], t + t2);
fac = nb2 / k;
}
}
forn(t, k) {
if(mns[t] != INF) {
gr[a][d].eb(k * fac + t, mns[t]);
}
}
}
forn(_, o) {
int a, b;
cin >> a >> b;
int diff = (b - a) / k;
if(diff >= 0) {
vii positions;
positions.eb(a, 0);
for(int pw = 0; diff; pw++, diff >>= 1) {
if(diff & 1)
{
vi mns(k, INF);
int fac = -1;
for(ii pos_t: positions) {
int pos = pos_t.F, t = pos_t.S;
for(ii nb2_t: gr[pos][pw]) {
int nb2 = nb2_t.F, t2 = nb2_t.S;
mineq(mns[nb2 % k], t + t2);
fac = nb2 / k;
}
}
positions.clear();
forn(t, k) {
if(mns[t] != INF) {
positions.eb(k * fac + t, mns[t]);
}
}
}
}
bool printed = false;
for(ii pos: positions) {
if(pos.F == b) {
cout << pos.S << '\n';
printed = true;
}
}
if(!printed)
cout << -1 << '\n';
} else {
cout << -1 << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// freopen("262144.in", "r", stdin);
// freopen("262144.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
54 ms |
23080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
73 ms |
23740 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19020 KB |
Output is correct |
2 |
Incorrect |
9 ms |
19008 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19020 KB |
Output is correct |
2 |
Incorrect |
9 ms |
19008 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
54 ms |
23080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |