#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPD(i, n) for (int i = (n) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX_NODE 50005
#define MAX_EDGE 500005
int numNode, numEdge, numQuery, k;
struct Matrix{
int N, M;
vector<vector<int>> F;
Matrix(): N(0), M(0){}
void init(int _N, int _M){
this -> N = _N, this -> M = _M;
F.resize(N, vector<int>(M, INF));
}
friend Matrix operator * (const Matrix &a, const Matrix &b){
Matrix res; res.init(a.N, b.M);
REP(i, a.N) REP(j, b.M) REP(k, a.M){
minimize(res.F[i][j], a.F[i][k] + b.F[k][j]);
}
return res;
}
};
Matrix dp[MAX_NODE][17];
void process(void){
cin >> k >> numNode >> numEdge >> numQuery;
int n = (numNode - 1) / k;
for (int i = 0; i <= n; ++i){
for (int j = 0; j < 16; ++j) dp[i][j].init(k, k);
}
for (int i = 1; i <= numEdge; ++i){
int u, v, w; cin >> u >> v >> w;
minimize(dp[u / k][0].F[u % k][v % k], w);
}
for (int k = 1; k < 16; ++k){
for (int i = 0; i + MASK(k) - 1 <= n; ++i){
dp[i][k] = dp[i][k - 1] * dp[i + MASK(k - 1)][k - 1];
}
}
for (int i = 1; i <= numQuery; ++i){
int u, v; cin >> u >> v;
int l = u / k, r = v / k;
int dif = r - l;
Matrix res; res.init(k, k);
REP(x, k) res.F[x][x] = 0;
for (int j = 0; j < 16; ++j) if(BIT(dif, j)){
res = res * dp[l][j];
l += MASK(j);
}
cout << (res.F[u % k][v % k] >= INF ? -1 : res.F[u % k][v % k]) << '\n';
}
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
83696 KB |
Output is correct |
2 |
Correct |
6 ms |
33624 KB |
Output is correct |
3 |
Correct |
6 ms |
33628 KB |
Output is correct |
4 |
Correct |
6 ms |
33672 KB |
Output is correct |
5 |
Correct |
8 ms |
34756 KB |
Output is correct |
6 |
Correct |
8 ms |
34652 KB |
Output is correct |
7 |
Correct |
8 ms |
34724 KB |
Output is correct |
8 |
Correct |
154 ms |
84564 KB |
Output is correct |
9 |
Correct |
182 ms |
84580 KB |
Output is correct |
10 |
Correct |
142 ms |
83940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
83804 KB |
Output is correct |
2 |
Correct |
6 ms |
33624 KB |
Output is correct |
3 |
Correct |
6 ms |
33628 KB |
Output is correct |
4 |
Correct |
6 ms |
33748 KB |
Output is correct |
5 |
Correct |
6 ms |
33628 KB |
Output is correct |
6 |
Correct |
6 ms |
33628 KB |
Output is correct |
7 |
Correct |
13 ms |
34652 KB |
Output is correct |
8 |
Correct |
13 ms |
34648 KB |
Output is correct |
9 |
Correct |
147 ms |
83640 KB |
Output is correct |
10 |
Correct |
144 ms |
79696 KB |
Output is correct |
11 |
Correct |
160 ms |
83792 KB |
Output is correct |
12 |
Correct |
140 ms |
79628 KB |
Output is correct |
13 |
Correct |
104 ms |
68176 KB |
Output is correct |
14 |
Correct |
85 ms |
61268 KB |
Output is correct |
15 |
Correct |
94 ms |
68088 KB |
Output is correct |
16 |
Correct |
93 ms |
68176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33684 KB |
Output is correct |
2 |
Correct |
5 ms |
33628 KB |
Output is correct |
3 |
Correct |
7 ms |
33628 KB |
Output is correct |
4 |
Correct |
6 ms |
33628 KB |
Output is correct |
5 |
Correct |
8 ms |
33628 KB |
Output is correct |
6 |
Correct |
8 ms |
34748 KB |
Output is correct |
7 |
Correct |
8 ms |
34652 KB |
Output is correct |
8 |
Correct |
8 ms |
34952 KB |
Output is correct |
9 |
Correct |
8 ms |
34908 KB |
Output is correct |
10 |
Correct |
143 ms |
84564 KB |
Output is correct |
11 |
Correct |
143 ms |
85328 KB |
Output is correct |
12 |
Correct |
140 ms |
81844 KB |
Output is correct |
13 |
Correct |
147 ms |
82004 KB |
Output is correct |
14 |
Correct |
146 ms |
81236 KB |
Output is correct |
15 |
Correct |
94 ms |
69464 KB |
Output is correct |
16 |
Correct |
86 ms |
69456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33684 KB |
Output is correct |
2 |
Correct |
5 ms |
33628 KB |
Output is correct |
3 |
Correct |
7 ms |
33628 KB |
Output is correct |
4 |
Correct |
6 ms |
33628 KB |
Output is correct |
5 |
Correct |
8 ms |
33628 KB |
Output is correct |
6 |
Correct |
8 ms |
34748 KB |
Output is correct |
7 |
Correct |
8 ms |
34652 KB |
Output is correct |
8 |
Correct |
8 ms |
34952 KB |
Output is correct |
9 |
Correct |
8 ms |
34908 KB |
Output is correct |
10 |
Correct |
143 ms |
84564 KB |
Output is correct |
11 |
Correct |
143 ms |
85328 KB |
Output is correct |
12 |
Correct |
140 ms |
81844 KB |
Output is correct |
13 |
Correct |
147 ms |
82004 KB |
Output is correct |
14 |
Correct |
146 ms |
81236 KB |
Output is correct |
15 |
Correct |
94 ms |
69464 KB |
Output is correct |
16 |
Correct |
86 ms |
69456 KB |
Output is correct |
17 |
Correct |
146 ms |
85360 KB |
Output is correct |
18 |
Correct |
6 ms |
33624 KB |
Output is correct |
19 |
Correct |
6 ms |
33628 KB |
Output is correct |
20 |
Correct |
6 ms |
33628 KB |
Output is correct |
21 |
Correct |
6 ms |
33628 KB |
Output is correct |
22 |
Correct |
6 ms |
33628 KB |
Output is correct |
23 |
Correct |
9 ms |
34652 KB |
Output is correct |
24 |
Correct |
9 ms |
34648 KB |
Output is correct |
25 |
Correct |
13 ms |
34908 KB |
Output is correct |
26 |
Correct |
11 ms |
34908 KB |
Output is correct |
27 |
Correct |
144 ms |
84436 KB |
Output is correct |
28 |
Correct |
142 ms |
81744 KB |
Output is correct |
29 |
Correct |
164 ms |
82076 KB |
Output is correct |
30 |
Correct |
164 ms |
81744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
83696 KB |
Output is correct |
2 |
Correct |
6 ms |
33624 KB |
Output is correct |
3 |
Correct |
6 ms |
33628 KB |
Output is correct |
4 |
Correct |
6 ms |
33672 KB |
Output is correct |
5 |
Correct |
8 ms |
34756 KB |
Output is correct |
6 |
Correct |
8 ms |
34652 KB |
Output is correct |
7 |
Correct |
8 ms |
34724 KB |
Output is correct |
8 |
Correct |
154 ms |
84564 KB |
Output is correct |
9 |
Correct |
182 ms |
84580 KB |
Output is correct |
10 |
Correct |
142 ms |
83940 KB |
Output is correct |
11 |
Correct |
153 ms |
83804 KB |
Output is correct |
12 |
Correct |
6 ms |
33624 KB |
Output is correct |
13 |
Correct |
6 ms |
33628 KB |
Output is correct |
14 |
Correct |
6 ms |
33748 KB |
Output is correct |
15 |
Correct |
6 ms |
33628 KB |
Output is correct |
16 |
Correct |
6 ms |
33628 KB |
Output is correct |
17 |
Correct |
13 ms |
34652 KB |
Output is correct |
18 |
Correct |
13 ms |
34648 KB |
Output is correct |
19 |
Correct |
147 ms |
83640 KB |
Output is correct |
20 |
Correct |
144 ms |
79696 KB |
Output is correct |
21 |
Correct |
160 ms |
83792 KB |
Output is correct |
22 |
Correct |
140 ms |
79628 KB |
Output is correct |
23 |
Correct |
104 ms |
68176 KB |
Output is correct |
24 |
Correct |
85 ms |
61268 KB |
Output is correct |
25 |
Correct |
94 ms |
68088 KB |
Output is correct |
26 |
Correct |
93 ms |
68176 KB |
Output is correct |
27 |
Correct |
5 ms |
33684 KB |
Output is correct |
28 |
Correct |
5 ms |
33628 KB |
Output is correct |
29 |
Correct |
7 ms |
33628 KB |
Output is correct |
30 |
Correct |
6 ms |
33628 KB |
Output is correct |
31 |
Correct |
8 ms |
33628 KB |
Output is correct |
32 |
Correct |
8 ms |
34748 KB |
Output is correct |
33 |
Correct |
8 ms |
34652 KB |
Output is correct |
34 |
Correct |
8 ms |
34952 KB |
Output is correct |
35 |
Correct |
8 ms |
34908 KB |
Output is correct |
36 |
Correct |
143 ms |
84564 KB |
Output is correct |
37 |
Correct |
143 ms |
85328 KB |
Output is correct |
38 |
Correct |
140 ms |
81844 KB |
Output is correct |
39 |
Correct |
147 ms |
82004 KB |
Output is correct |
40 |
Correct |
146 ms |
81236 KB |
Output is correct |
41 |
Correct |
94 ms |
69464 KB |
Output is correct |
42 |
Correct |
86 ms |
69456 KB |
Output is correct |
43 |
Correct |
146 ms |
85360 KB |
Output is correct |
44 |
Correct |
6 ms |
33624 KB |
Output is correct |
45 |
Correct |
6 ms |
33628 KB |
Output is correct |
46 |
Correct |
6 ms |
33628 KB |
Output is correct |
47 |
Correct |
6 ms |
33628 KB |
Output is correct |
48 |
Correct |
6 ms |
33628 KB |
Output is correct |
49 |
Correct |
9 ms |
34652 KB |
Output is correct |
50 |
Correct |
9 ms |
34648 KB |
Output is correct |
51 |
Correct |
13 ms |
34908 KB |
Output is correct |
52 |
Correct |
11 ms |
34908 KB |
Output is correct |
53 |
Correct |
144 ms |
84436 KB |
Output is correct |
54 |
Correct |
142 ms |
81744 KB |
Output is correct |
55 |
Correct |
164 ms |
82076 KB |
Output is correct |
56 |
Correct |
164 ms |
81744 KB |
Output is correct |
57 |
Correct |
189 ms |
96580 KB |
Output is correct |
58 |
Correct |
155 ms |
84712 KB |
Output is correct |
59 |
Correct |
191 ms |
85588 KB |
Output is correct |