# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
567566 |
2022-05-23T17:38:07 Z |
_karan_gandhi |
Toll (BOI17_toll) |
C++17 |
|
177 ms |
199704 KB |
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define endl '\n'
#define pl(var) " [" << #var << ": " << (var) << "] "
#define ll long long
void solve() {
int k, n, m, o; cin >> k >> n >> m >> o;
// const ll int inf = 1e16;
const int LOG = 20;
ll int dp[n / k][LOG][5][5];
memset(dp, 1, sizeof dp);
// dp[i][i + 2 ^ k][x][y];
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < m; i++) {
int a, b; cin >> a >> b;
int c; cin >> c;
adj[a].emplace_back(b, c);
}
for (int i = 0; i < n; i++) {
for (auto [v, wt] : adj[i]) {
dp[i / k][0][i % k][v % k] = wt;
}
}
auto combine = [&](ll int target[5][5], ll int a[5][5], ll int b[5][5]) {
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
for (int l = 0; l < k; l++) {
target[i][j] = min(target[i][j], a[i][l] + b[l][j]);
}
}
}
};
for (int i = 1; i < LOG; i++) {
for (int j = 0; j < n / k; j++) {
if (j + (1 << (i - 1)) >= n / k) continue;
combine(dp[j][i], dp[j][i - 1], dp[j + (1 << (i - 1))][i - 1]);
// for (int ii = 0; ii < k; ii++) {
// for (int jj = 0; jj < k; jj++) {
// cout << pl(i) << pl(j) << pl(ii) << pl(jj) << pl(dp[j][i][ii][jj]) << endl;
// }
// }
}
}
auto query = [&](int start, int levels, int en) {
// cout << pl(start) << pl(en) << endl;
// int _start = start;
ll int res[5][5];
int level = start / k;
memset(res, 1, sizeof res);
for (int i = 0; i < k; i++) res[i][i] = 0;
// res[start % k][start % k] = 0;
int pow = LOG;
while (levels > 0) {
while (levels < (1 << pow)) pow--;
ll int new_res[5][5];
memset(new_res, 1, sizeof new_res);
combine(new_res, res, dp[level][pow]);
memcpy(res, new_res, sizeof res);
level += (1 << pow);
levels -= (1 << pow);
// cout << pl(levels) << endl;
// for (int ii = 0; ii < k; ii++) {
// for (int jj = 0; jj < k; jj++) {
// cout << pl(ii) << pl(jj) << pl(new_res[ii][jj]) << endl;
// }
// }
}
// for (int ii = 0; ii < k; ii++) {
// for (int jj = 0; jj < k; jj++) {
// cout << pl(ii) << pl(jj) << pl(res[ii][jj]) << endl;
// }
// }
// cout << pl(res[0][2]) << endl;
// cout << pl(start % k) << pl(en % k) << endl;
// cout << pl(start) << endl;
return res[start % k][en % k];
};
// cout << pl(dp[0][1][0][2]) << endl;
// cout << pl(dp[0][1][4][2]) << endl;
while (o--) {
int a, b; cin >> a >> b;
// cout << pl(a) << pl(b) << endl;
int levels = abs(a / k - b / k);
// cout << pl(levels) << endl;
if (levels == 0) {
cout << -1 << endl;
}
ll int res = query(a, levels, b);
// cout << pl(res) << endl;
if (res > 1e16) {
cout << -1 << endl;
} else {
cout << res << endl;
}
}
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--)
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
199704 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
4 ms |
4180 KB |
Output is correct |
6 |
Correct |
3 ms |
4300 KB |
Output is correct |
7 |
Correct |
3 ms |
4296 KB |
Output is correct |
8 |
Correct |
165 ms |
199664 KB |
Output is correct |
9 |
Correct |
177 ms |
199572 KB |
Output is correct |
10 |
Correct |
142 ms |
197304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
138 ms |
101732 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
199704 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
4 ms |
4180 KB |
Output is correct |
6 |
Correct |
3 ms |
4300 KB |
Output is correct |
7 |
Correct |
3 ms |
4296 KB |
Output is correct |
8 |
Correct |
165 ms |
199664 KB |
Output is correct |
9 |
Correct |
177 ms |
199572 KB |
Output is correct |
10 |
Correct |
142 ms |
197304 KB |
Output is correct |
11 |
Correct |
138 ms |
101732 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |