#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);
}
}
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]) ;
}
}
}
}
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] ;
}
for(int k = LOG-1; k >= 0 ; --k){
if( (1 << k) <= d){
vector<int> newD(K,INF) ;
f(i,0,K){
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) ;
}
}
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:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
73 | freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
6764 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 |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
42 ms |
6764 KB |
Output is correct |
9 |
Correct |
45 ms |
6764 KB |
Output is correct |
10 |
Correct |
24 ms |
6764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
13036 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 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 |
7 ms |
492 KB |
Output is correct |
8 |
Correct |
7 ms |
620 KB |
Output is correct |
9 |
Correct |
41 ms |
6764 KB |
Output is correct |
10 |
Correct |
134 ms |
19308 KB |
Output is correct |
11 |
Correct |
104 ms |
13040 KB |
Output is correct |
12 |
Correct |
105 ms |
19308 KB |
Output is correct |
13 |
Correct |
138 ms |
18028 KB |
Output is correct |
14 |
Correct |
63 ms |
11116 KB |
Output is correct |
15 |
Correct |
103 ms |
18028 KB |
Output is correct |
16 |
Correct |
108 ms |
18028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 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 |
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 |
36 ms |
6764 KB |
Output is correct |
11 |
Correct |
79 ms |
13164 KB |
Output is correct |
12 |
Correct |
122 ms |
19180 KB |
Output is correct |
13 |
Correct |
122 ms |
19180 KB |
Output is correct |
14 |
Correct |
117 ms |
19180 KB |
Output is correct |
15 |
Correct |
92 ms |
18028 KB |
Output is correct |
16 |
Correct |
98 ms |
18028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 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 |
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 |
36 ms |
6764 KB |
Output is correct |
11 |
Correct |
79 ms |
13164 KB |
Output is correct |
12 |
Correct |
122 ms |
19180 KB |
Output is correct |
13 |
Correct |
122 ms |
19180 KB |
Output is correct |
14 |
Correct |
117 ms |
19180 KB |
Output is correct |
15 |
Correct |
92 ms |
18028 KB |
Output is correct |
16 |
Correct |
98 ms |
18028 KB |
Output is correct |
17 |
Correct |
98 ms |
13036 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 |
2 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 |
36 ms |
6764 KB |
Output is correct |
28 |
Correct |
131 ms |
19436 KB |
Output is correct |
29 |
Correct |
128 ms |
19308 KB |
Output is correct |
30 |
Correct |
121 ms |
19300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
6764 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 |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
42 ms |
6764 KB |
Output is correct |
9 |
Correct |
45 ms |
6764 KB |
Output is correct |
10 |
Correct |
24 ms |
6764 KB |
Output is correct |
11 |
Correct |
89 ms |
13036 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
384 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 |
7 ms |
492 KB |
Output is correct |
18 |
Correct |
7 ms |
620 KB |
Output is correct |
19 |
Correct |
41 ms |
6764 KB |
Output is correct |
20 |
Correct |
134 ms |
19308 KB |
Output is correct |
21 |
Correct |
104 ms |
13040 KB |
Output is correct |
22 |
Correct |
105 ms |
19308 KB |
Output is correct |
23 |
Correct |
138 ms |
18028 KB |
Output is correct |
24 |
Correct |
63 ms |
11116 KB |
Output is correct |
25 |
Correct |
103 ms |
18028 KB |
Output is correct |
26 |
Correct |
108 ms |
18028 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 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 |
36 ms |
6764 KB |
Output is correct |
37 |
Correct |
79 ms |
13164 KB |
Output is correct |
38 |
Correct |
122 ms |
19180 KB |
Output is correct |
39 |
Correct |
122 ms |
19180 KB |
Output is correct |
40 |
Correct |
117 ms |
19180 KB |
Output is correct |
41 |
Correct |
92 ms |
18028 KB |
Output is correct |
42 |
Correct |
98 ms |
18028 KB |
Output is correct |
43 |
Correct |
98 ms |
13036 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 |
2 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 |
36 ms |
6764 KB |
Output is correct |
54 |
Correct |
131 ms |
19436 KB |
Output is correct |
55 |
Correct |
128 ms |
19308 KB |
Output is correct |
56 |
Correct |
121 ms |
19300 KB |
Output is correct |
57 |
Correct |
187 ms |
25580 KB |
Output is correct |
58 |
Correct |
45 ms |
6764 KB |
Output is correct |
59 |
Correct |
94 ms |
13036 KB |
Output is correct |