#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define ms(a,n) memset(a,n,sizeof(a))
#define FOR(i,a,b) for(int i = a; i <= b; i++)
#define FOD(i,b,a) for(int i = b; i >= a; i--)
//Bit
#define MASK(x) (1ll<<(x))
#define BIT(mask, i) (((mask) >> (i))&1)
#define SET_ON(x,i) ((x) | MASK(i))
#define SET_OFF(x,i) ((x) & ~MASK(i))
#define c_bit(i) __builtin_popcountll(i)
#define TASK "test"
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
typedef pair<double,double> dd;
typedef vector<ll> vll;
typedef vector<int> vi;
inline ll gcd(ll a, ll b){return b==0 ? a : gcd(b,a%b);}
inline ll lcm(ll a, ll b){return a/gcd(a,b)*b;}
const ld PI = 3.141592653589793238462643383279502884L;
const int MOD = 1e9+7;
const ll LINF = 1e18;
const int INF = 1e9;
const int N = 5e4+5;
const int LOG = __lg(N)+5;
int k,n,m,q;
ll dp[N][LOG][6][6], ans[6][6], tmp[6][6];
void combine(ll target[6][6], ll a[6][6], ll b[6][6]) {
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
for (int z = 0; z < k; z++) {
target[x][y] = min(target[x][y], a[x][z] + b[z][y]);
}
}
}
}
void solve() {
cin >> k >> n >> m >> q;
ms(dp,0x3f);
FOR(i,1,m){
int u,v,w; cin >> u >> v >> w;
dp[u/k][0][u%k][v%k] = w;
}
FOR(j,1,LOG-1){
for (int i = 0; i + (1 << j) < (n + k - 1) / k; i++) {
combine(dp[i][j], dp[i][j - 1], dp[i + MASK(j) -1][j - 1]);
}
}
while (q--) {
int a, b;
cin >> a >> b;
memset(ans, 0x3f, sizeof ans);
for (int i = 0; i < 5; i++) ans[i][i] = 0;
for (int curr = a / k, dest = b / k, i = 16; ~i; i--) {
if (curr + (1 << i) <= dest) {
memset(tmp, 0x3f, sizeof tmp);
combine(tmp, ans, dp[curr][i]);
memcpy(ans, tmp, sizeof ans);
curr += (1 << i);
}
}
cout << (ans[a % k][b % k] >= 0x3f3f3f3f ? -1 : ans[a % k][b % k]) << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen(TASK ".inp", "r", stdin);
//freopen(TASK ".out", "w", stdout);
int tc = 1; //cin >> tc;
while (tc--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
282196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
72 ms |
282788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
282192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
282192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
282196 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |