This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int main(const int argc, const char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
const int INF = 1e9;
int k, n, m, o, group;
cin >> k >> n >> m >> o;
group = n/k;
vector <int> lg(group+1);
lg[1] = 0;
for(int i = 2; i <= group; ++i) {
lg[i] = lg[i>>1] + 1;
}
vector <vector <vector <vector <int>>>> dp(group+1, vector <vector <vector <int>>> (k, vector <vector <int>> (k, vector <int> (lg[group]+1, INF))));
for(int i = 0; i < m; ++i) {
int a, b, w;
cin >> a >> b >> w;
dp[a/k][a%k][b%k][0] = w;
}
for(int l = 1; l <= lg[group]; ++l) {
for(int i = 0; i <= group; ++i) {
for(int y = 0; y < k; ++y) {
for(int z = 0; z < k; ++z) {
for(int mid = 0; mid < k; ++mid) {
int next = i + (1<<(l-1));
if (next > group) continue;
dp[i][y][z][l] = min(dp[i][y][z][l], dp[i][y][mid][l-1] + dp[next][mid][z][l-1]);
}
}
}
}
}
while (o--) {
int a, b;
cin >> a >> b;
int group_a = a/k;
int group_b = b/k;
int left = group_b - group_a;
int source = a % k;
int destination = (left == 1? 1:lg[left])*k + b % k;
int node_cnt = (left == 1? k<<1: (lg[left] + 1)*k);
vector <vector <pii>> adj(node_cnt);
vector <int> indegree(node_cnt, 0);
int ptr = 0;
int tmp_left = left;
for(int l = lg[group]; l >= 0; --l) {
if ((1<<l) <= tmp_left) {
tmp_left -= (1<<l);
for(int y = 0; y < k; ++y) {
for(int z = 0; z < k; ++z) {
int u = ptr*k + y;
int v = (ptr+1)*k + z;
assert(v < node_cnt);
adj[u].push_back({v, dp[group_a][y][z][l]});
indegree[v]++;
}
}
group_a += (1<<l);
ptr++;
}
}
queue <int> Q;
vector <int> dist(node_cnt, INF);
Q.push(source);
dist[source] = 0;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for(auto edge : adj[u]) {
int v = edge.first;
int w = edge.second;
dist[v] = min(dist[v], dist[u] + w);
indegree[v]--;
if (indegree[v] == 0) Q.push(v);
}
}
cout << (dist[destination] == INF? -1LL:dist[destination]) << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |