#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define REP(a) F0R(_,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define rsz resize
#define pb push_back
#define f first
#define s second
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define tcT template<class T
#define tcTU tcT, class U
#define nl "\n"
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using pi = pair<int,int>;
using vpi = vector<pi>;
using str = string;
using vs = vector<str>;
using db = double;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
tcTU> using PR = pair<T,U>;
const int MOD = 998244353;
const ll INF = 1e18;
const int dx[] = {-1,0,1,0}, dy[] = {0,-1,0,1};
constexpr int bits(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); }
constexpr bool onbit(int msk, int i) { return msk>>i&1; }
constexpr ll p2(int x) { return 1LL<<x; }
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int lg2(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); }; //int only
tcT> bool ckmin(T&a, const T&b) { return b < a ? a = b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) { return b > a ? a = b,1 : 0; }
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); }
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); }
tcT> void remDup(V<T> &v) {
sort(all(v)); v.erase(unique(all(v)), end(v)); }
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
void setPrec() { cout << fixed << setprecision(16); }
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
setPrec();
if(sz(NAME)) {
freopen((NAME + ".in").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
const int MX = 5e4+5;
int K, N, M, O;
int dp[MX][17][5][5], ans[5][5], tmp[5][5];
void cmb(int target[5][5], int a[5][5], int b[5][5]) {
F0R(z, K) F0R(x, K) F0R(y, K) target[x][y] = min(target[x][y], a[x][z] + b[z][y]);
}
void solve() {
cin>>K>>N>>M>>O;
memset(dp, 0x3f, sizeof dp);
REP(M) {
int u,v,t;
cin>>u>>v>>t;
dp[u/K][0][u%K][v%K] = t;
}
FOR(j,1,17) for(int i = 0; i + (1 << j) < (N + K - 1)/K; i++) {
cmb(dp[i][j], dp[i][j-1], dp[i + (1<<j-1)][j-1]);
}
while(O--) {
int a,b; cin>>a>>b;
memset(ans, 0x3f, sizeof ans);
F0R(i, 5) 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);
cmb(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]) << nl;
}
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message
toll.cpp: In function 'void solve()':
toll.cpp:96:41: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
96 | cmb(dp[i][j], dp[i][j-1], dp[i + (1<<j-1)][j-1]);
| ~^~
toll.cpp: In function 'void setIO(std::string)':
toll.cpp:74:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen((NAME + ".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:75:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen((NAME + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
84548 KB |
Output is correct |
2 |
Correct |
32 ms |
83384 KB |
Output is correct |
3 |
Correct |
40 ms |
83396 KB |
Output is correct |
4 |
Correct |
32 ms |
83392 KB |
Output is correct |
5 |
Correct |
36 ms |
83460 KB |
Output is correct |
6 |
Correct |
34 ms |
83404 KB |
Output is correct |
7 |
Correct |
35 ms |
83472 KB |
Output is correct |
8 |
Correct |
70 ms |
84352 KB |
Output is correct |
9 |
Correct |
72 ms |
84292 KB |
Output is correct |
10 |
Correct |
54 ms |
83524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
85124 KB |
Output is correct |
2 |
Correct |
32 ms |
83472 KB |
Output is correct |
3 |
Correct |
32 ms |
83384 KB |
Output is correct |
4 |
Correct |
33 ms |
83352 KB |
Output is correct |
5 |
Correct |
36 ms |
83396 KB |
Output is correct |
6 |
Correct |
32 ms |
83472 KB |
Output is correct |
7 |
Correct |
35 ms |
83524 KB |
Output is correct |
8 |
Correct |
38 ms |
83512 KB |
Output is correct |
9 |
Correct |
78 ms |
84284 KB |
Output is correct |
10 |
Correct |
98 ms |
85700 KB |
Output is correct |
11 |
Correct |
81 ms |
85124 KB |
Output is correct |
12 |
Correct |
82 ms |
84736 KB |
Output is correct |
13 |
Correct |
93 ms |
85892 KB |
Output is correct |
14 |
Correct |
64 ms |
84864 KB |
Output is correct |
15 |
Correct |
63 ms |
84608 KB |
Output is correct |
16 |
Correct |
70 ms |
84616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
83468 KB |
Output is correct |
2 |
Correct |
35 ms |
83428 KB |
Output is correct |
3 |
Correct |
33 ms |
83360 KB |
Output is correct |
4 |
Correct |
32 ms |
83396 KB |
Output is correct |
5 |
Correct |
32 ms |
83404 KB |
Output is correct |
6 |
Correct |
33 ms |
83444 KB |
Output is correct |
7 |
Correct |
35 ms |
83496 KB |
Output is correct |
8 |
Correct |
35 ms |
83616 KB |
Output is correct |
9 |
Correct |
33 ms |
83488 KB |
Output is correct |
10 |
Correct |
61 ms |
84160 KB |
Output is correct |
11 |
Correct |
100 ms |
85004 KB |
Output is correct |
12 |
Correct |
90 ms |
85636 KB |
Output is correct |
13 |
Correct |
98 ms |
85884 KB |
Output is correct |
14 |
Correct |
88 ms |
85252 KB |
Output is correct |
15 |
Correct |
61 ms |
84612 KB |
Output is correct |
16 |
Correct |
65 ms |
84604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
83468 KB |
Output is correct |
2 |
Correct |
35 ms |
83428 KB |
Output is correct |
3 |
Correct |
33 ms |
83360 KB |
Output is correct |
4 |
Correct |
32 ms |
83396 KB |
Output is correct |
5 |
Correct |
32 ms |
83404 KB |
Output is correct |
6 |
Correct |
33 ms |
83444 KB |
Output is correct |
7 |
Correct |
35 ms |
83496 KB |
Output is correct |
8 |
Correct |
35 ms |
83616 KB |
Output is correct |
9 |
Correct |
33 ms |
83488 KB |
Output is correct |
10 |
Correct |
61 ms |
84160 KB |
Output is correct |
11 |
Correct |
100 ms |
85004 KB |
Output is correct |
12 |
Correct |
90 ms |
85636 KB |
Output is correct |
13 |
Correct |
98 ms |
85884 KB |
Output is correct |
14 |
Correct |
88 ms |
85252 KB |
Output is correct |
15 |
Correct |
61 ms |
84612 KB |
Output is correct |
16 |
Correct |
65 ms |
84604 KB |
Output is correct |
17 |
Correct |
80 ms |
84996 KB |
Output is correct |
18 |
Correct |
35 ms |
83492 KB |
Output is correct |
19 |
Correct |
32 ms |
83384 KB |
Output is correct |
20 |
Correct |
32 ms |
83396 KB |
Output is correct |
21 |
Correct |
32 ms |
83372 KB |
Output is correct |
22 |
Correct |
33 ms |
83360 KB |
Output is correct |
23 |
Correct |
34 ms |
83464 KB |
Output is correct |
24 |
Correct |
36 ms |
83532 KB |
Output is correct |
25 |
Correct |
36 ms |
83516 KB |
Output is correct |
26 |
Correct |
34 ms |
83520 KB |
Output is correct |
27 |
Correct |
63 ms |
84232 KB |
Output is correct |
28 |
Correct |
94 ms |
85636 KB |
Output is correct |
29 |
Correct |
95 ms |
85856 KB |
Output is correct |
30 |
Correct |
84 ms |
85316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
84548 KB |
Output is correct |
2 |
Correct |
32 ms |
83384 KB |
Output is correct |
3 |
Correct |
40 ms |
83396 KB |
Output is correct |
4 |
Correct |
32 ms |
83392 KB |
Output is correct |
5 |
Correct |
36 ms |
83460 KB |
Output is correct |
6 |
Correct |
34 ms |
83404 KB |
Output is correct |
7 |
Correct |
35 ms |
83472 KB |
Output is correct |
8 |
Correct |
70 ms |
84352 KB |
Output is correct |
9 |
Correct |
72 ms |
84292 KB |
Output is correct |
10 |
Correct |
54 ms |
83524 KB |
Output is correct |
11 |
Correct |
90 ms |
85124 KB |
Output is correct |
12 |
Correct |
32 ms |
83472 KB |
Output is correct |
13 |
Correct |
32 ms |
83384 KB |
Output is correct |
14 |
Correct |
33 ms |
83352 KB |
Output is correct |
15 |
Correct |
36 ms |
83396 KB |
Output is correct |
16 |
Correct |
32 ms |
83472 KB |
Output is correct |
17 |
Correct |
35 ms |
83524 KB |
Output is correct |
18 |
Correct |
38 ms |
83512 KB |
Output is correct |
19 |
Correct |
78 ms |
84284 KB |
Output is correct |
20 |
Correct |
98 ms |
85700 KB |
Output is correct |
21 |
Correct |
81 ms |
85124 KB |
Output is correct |
22 |
Correct |
82 ms |
84736 KB |
Output is correct |
23 |
Correct |
93 ms |
85892 KB |
Output is correct |
24 |
Correct |
64 ms |
84864 KB |
Output is correct |
25 |
Correct |
63 ms |
84608 KB |
Output is correct |
26 |
Correct |
70 ms |
84616 KB |
Output is correct |
27 |
Correct |
35 ms |
83468 KB |
Output is correct |
28 |
Correct |
35 ms |
83428 KB |
Output is correct |
29 |
Correct |
33 ms |
83360 KB |
Output is correct |
30 |
Correct |
32 ms |
83396 KB |
Output is correct |
31 |
Correct |
32 ms |
83404 KB |
Output is correct |
32 |
Correct |
33 ms |
83444 KB |
Output is correct |
33 |
Correct |
35 ms |
83496 KB |
Output is correct |
34 |
Correct |
35 ms |
83616 KB |
Output is correct |
35 |
Correct |
33 ms |
83488 KB |
Output is correct |
36 |
Correct |
61 ms |
84160 KB |
Output is correct |
37 |
Correct |
100 ms |
85004 KB |
Output is correct |
38 |
Correct |
90 ms |
85636 KB |
Output is correct |
39 |
Correct |
98 ms |
85884 KB |
Output is correct |
40 |
Correct |
88 ms |
85252 KB |
Output is correct |
41 |
Correct |
61 ms |
84612 KB |
Output is correct |
42 |
Correct |
65 ms |
84604 KB |
Output is correct |
43 |
Correct |
80 ms |
84996 KB |
Output is correct |
44 |
Correct |
35 ms |
83492 KB |
Output is correct |
45 |
Correct |
32 ms |
83384 KB |
Output is correct |
46 |
Correct |
32 ms |
83396 KB |
Output is correct |
47 |
Correct |
32 ms |
83372 KB |
Output is correct |
48 |
Correct |
33 ms |
83360 KB |
Output is correct |
49 |
Correct |
34 ms |
83464 KB |
Output is correct |
50 |
Correct |
36 ms |
83532 KB |
Output is correct |
51 |
Correct |
36 ms |
83516 KB |
Output is correct |
52 |
Correct |
34 ms |
83520 KB |
Output is correct |
53 |
Correct |
63 ms |
84232 KB |
Output is correct |
54 |
Correct |
94 ms |
85636 KB |
Output is correct |
55 |
Correct |
95 ms |
85856 KB |
Output is correct |
56 |
Correct |
84 ms |
85316 KB |
Output is correct |
57 |
Correct |
112 ms |
86836 KB |
Output is correct |
58 |
Correct |
71 ms |
84348 KB |
Output is correct |
59 |
Correct |
87 ms |
85168 KB |
Output is correct |