#include <bits/stdc++.h>
using namespace std ;
#define i64 int64_t // typecast using i64(x)
#define int int64_t
#define ld long double
#define f(i,a,b) for(int i=a; i<b; ++i)
#define endl '\n'
#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound
#define fr first
#define sc second
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define sz(x) ((int)(x).size())
//#ifndef ONLINE_JUDGE
template<typename T>
void __p(T a) {
cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
cout<<"{";
__p(a.first);
cout<<",";
__p(a.second);
cout<<"}\n";
}
template<typename T>
void __p(std::vector<T> a) {
cout<<"{";
for(auto it=a.begin(); it<a.end(); it++)
__p(*it),cout<<",}\n"[it+1==a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
__p(a1);
__p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";
__p(arg1);
cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(;; i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<" | ";
__f(comma+1,args...);
}
//#endif
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin.exceptions(cin.failbit);
if(sz(s)){
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
}
void solve(){
}
const int INF = 1e12 ;
signed main(){
setIO() ;
int K, N, M, O; cin >> K >> N >> M >> O ;
int LOG = log2(N) + 1 ;
int dis[N][K][LOG] ;
f(i,0,N) f(j,0,K) f(k,0,LOG) dis[i][j][k] = INF ;
f(_,0,M){
int a, b, t; cin >> a >> b >> t ;
dis[a][b%K][0] = t ;
}
f(k,1,LOG){
f(i,0,N){
f(j,0,K){
f(u,0,K){
int mid = K * ( i/K + (1 << (k-1)) ) + u ;
if(mid >= N) continue ;
dis[i][j][k] = min(dis[i][j][k], dis[i][u][k-1] + dis[mid][j][k-1]) ;
}
}
}
}
/*f(i,0,N) f(j,0,K) f(k,0,LOG){
__f("i,j,k,dis[i][j][k]",i,j,k,dis[i][j][k]) ;
} */
while(O--){
int a, b; cin >> a >> b ; assert(a != b) ;
int d = b/K - a/K ;
if(d <= 0){ cout << "-1" << endl ; continue ;}
d--;
vector<int> cur(K), D(K) ;
f(i,0,K){
cur[i] = K * (a/K + 1) + i ;
D[i] = dis[a][i][0] ;
}
//__f("d,cur,D",d,cur,D) ;
for(int k = LOG-1; k >= 0 ; --k){
if( (1 << k) <= d){
vector<int> newD(K,INF) ;
f(i,0,K){ // we reach at j from i
// what this i updates ;
f(j,0,K){
if(cur[i] >= N) continue ;
newD[j] = min(newD[j], dis[cur[i]][j][k] + D[i] ) ;
}
}
int Q = cur[0]/K ;
f(i,0,K) cur[i] = K * ( Q + (1 << k) ) + i ;
D = newD ;
d -= (1 << k) ;
}
}
assert(cur[b%K] == b) ;
if( D[b%K] >= INF){
cout << "-1" << endl ; continue ;
}
cout << D[b%K] << endl ;
}
}
Compilation message
toll.cpp: In function 'void setIO(std::string)':
toll.cpp:74:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
74 | freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:75:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
75 | freopen((s+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
7564 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
48 ms |
7532 KB |
Output is correct |
9 |
Correct |
43 ms |
7532 KB |
Output is correct |
10 |
Correct |
26 ms |
6764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
14572 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
7 ms |
620 KB |
Output is correct |
9 |
Correct |
39 ms |
7532 KB |
Output is correct |
10 |
Correct |
129 ms |
21484 KB |
Output is correct |
11 |
Correct |
99 ms |
14700 KB |
Output is correct |
12 |
Correct |
102 ms |
20460 KB |
Output is correct |
13 |
Correct |
126 ms |
20460 KB |
Output is correct |
14 |
Correct |
64 ms |
12524 KB |
Output is correct |
15 |
Correct |
96 ms |
19180 KB |
Output is correct |
16 |
Correct |
101 ms |
19180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
3 ms |
748 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
34 ms |
7404 KB |
Output is correct |
11 |
Correct |
81 ms |
14444 KB |
Output is correct |
12 |
Correct |
117 ms |
21356 KB |
Output is correct |
13 |
Correct |
130 ms |
21484 KB |
Output is correct |
14 |
Correct |
111 ms |
20972 KB |
Output is correct |
15 |
Correct |
109 ms |
19180 KB |
Output is correct |
16 |
Correct |
94 ms |
19180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
3 ms |
748 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
34 ms |
7404 KB |
Output is correct |
11 |
Correct |
81 ms |
14444 KB |
Output is correct |
12 |
Correct |
117 ms |
21356 KB |
Output is correct |
13 |
Correct |
130 ms |
21484 KB |
Output is correct |
14 |
Correct |
111 ms |
20972 KB |
Output is correct |
15 |
Correct |
109 ms |
19180 KB |
Output is correct |
16 |
Correct |
94 ms |
19180 KB |
Output is correct |
17 |
Correct |
85 ms |
14444 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
3 ms |
492 KB |
Output is correct |
24 |
Correct |
3 ms |
620 KB |
Output is correct |
25 |
Correct |
5 ms |
876 KB |
Output is correct |
26 |
Correct |
4 ms |
748 KB |
Output is correct |
27 |
Correct |
35 ms |
7404 KB |
Output is correct |
28 |
Correct |
121 ms |
21356 KB |
Output is correct |
29 |
Correct |
129 ms |
21740 KB |
Output is correct |
30 |
Correct |
114 ms |
21120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
7564 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
48 ms |
7532 KB |
Output is correct |
9 |
Correct |
43 ms |
7532 KB |
Output is correct |
10 |
Correct |
26 ms |
6764 KB |
Output is correct |
11 |
Correct |
86 ms |
14572 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
7 ms |
620 KB |
Output is correct |
19 |
Correct |
39 ms |
7532 KB |
Output is correct |
20 |
Correct |
129 ms |
21484 KB |
Output is correct |
21 |
Correct |
99 ms |
14700 KB |
Output is correct |
22 |
Correct |
102 ms |
20460 KB |
Output is correct |
23 |
Correct |
126 ms |
20460 KB |
Output is correct |
24 |
Correct |
64 ms |
12524 KB |
Output is correct |
25 |
Correct |
96 ms |
19180 KB |
Output is correct |
26 |
Correct |
101 ms |
19180 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
3 ms |
748 KB |
Output is correct |
35 |
Correct |
2 ms |
748 KB |
Output is correct |
36 |
Correct |
34 ms |
7404 KB |
Output is correct |
37 |
Correct |
81 ms |
14444 KB |
Output is correct |
38 |
Correct |
117 ms |
21356 KB |
Output is correct |
39 |
Correct |
130 ms |
21484 KB |
Output is correct |
40 |
Correct |
111 ms |
20972 KB |
Output is correct |
41 |
Correct |
109 ms |
19180 KB |
Output is correct |
42 |
Correct |
94 ms |
19180 KB |
Output is correct |
43 |
Correct |
85 ms |
14444 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
3 ms |
492 KB |
Output is correct |
50 |
Correct |
3 ms |
620 KB |
Output is correct |
51 |
Correct |
5 ms |
876 KB |
Output is correct |
52 |
Correct |
4 ms |
748 KB |
Output is correct |
53 |
Correct |
35 ms |
7404 KB |
Output is correct |
54 |
Correct |
121 ms |
21356 KB |
Output is correct |
55 |
Correct |
129 ms |
21740 KB |
Output is correct |
56 |
Correct |
114 ms |
21120 KB |
Output is correct |
57 |
Correct |
198 ms |
28764 KB |
Output is correct |
58 |
Correct |
41 ms |
7532 KB |
Output is correct |
59 |
Correct |
94 ms |
14700 KB |
Output is correct |