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>
#include<fstream>
using namespace std;
ifstream fin("VNOICUP.INP");
ofstream fout("VNOICUP.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const ld PI = 3.14159265359;
using u128 = __uint128_t;
//const int x[4] = {1, -1, 0, 0};
//const int y[4] = {0, 0, 1, -1};
const ll mod = 1e9 + 7;
const int mxn = 5e4 + 1, mxq = 2e5 + 5, sq = 400, mxv = 2e7 + 5;
//const int base = (1 << 18);
const ll inf = 1e9 + 5, neg = -69420;
int k, n, m, o;
struct MAT{
int a[5][5];
void init(){
for(int i = 0; i < k; i++){
for(int j = 0; j < k; j++){
a[i][j] = inf;
}
}
}
MAT operator *(const MAT &b){
MAT res;
for(int i = 0; i < k; i++){
for(int j = 0; j < k; j++){
res.a[i][j] = inf;
for(int l = 0; l < k; l++){
res.a[i][j] = min(res.a[i][j], a[i][l] + b.a[l][j]);
}
}
}
return(res);
}
};
MAT dp[mxn + 1][16];
void input(){
cin >> k >> n >> m >> o;
for(int i = 0; i <= (n - 1) / k; i++){
for(int j = 0; j < 16; j++){
dp[i][j].init();
}
}
for(int i = 0; i < m; i++){
int u, v, w; cin >> u >> v >> w;
dp[u / k][0].a[u % k][v % k] = w;
}
}
void process(){
int mx = (n - 1) / k;
for(int i = 1; i < 16; i++){
for(int j = 0; j + (1 << i) - 1 <= mx; j++){
dp[j][i] = dp[j][i - 1] * dp[j + (1 << (i - 1))][i - 1];
}
}
for(int i = 0; i < o; i++){
int u, v; cin >> u >> v;
MAT ans; ans.init();
forr(j, 0, k){
ans.a[j][j] = 0;
}
int uu = (u / k), vv = (v / k);
int dif = (vv - uu);
for(int j = 0; j < 16; j++){
if(dif & (1 << j)){
ans = ans * dp[uu][j]; uu += (1 << j);
}
}
cout << ((ans.a[u % k][v % k] == inf) ? -1 : ans.a[u % k][v % k]) << "\n";
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
input();
process();
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... |