# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
959289 | Braabebo10 | Cyberland (APIO23_cyberland) | C++17 | 0 ms | 0 KiB |
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 "cyberland.h"
#include<bits/stdc++.h>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
int a[N];
double d[31][N];
bool vis[31][N];
bool _vis[N];
vector<int> start;
vector<pair<int, int> > adj[N];
int n, h, k;
#define ii pair<double, int>
priority_queue<ii, vector<ii>, greater<ii> > q[31];
void dfs(int u) {
_vis[u] = 1;
if (a[u] == 0 || u == 0) q[0].push({d[0][u] = 0, u});
for(auto &[v, w] : adj[u]) if (!_vis[v] && v != h) dfs(v);
}
double solve(int N, int M, int K, int H, std::vector<int> x, std::vector<int> y, std::vector<int> c, std::vector<int> arr) {
n = N;
h = H;
k = K;
for(int i = 0; i < n; i++) adj[i].clear(), _vis[i] = 0;
for(int i = 0; i < x.size(); i++) {
adj[x[i]].emplace_back(y[i], c[i]);
adj[y[i]].emplace_back(x[i], c[i]);
}
for(int j = 0; j <= k; j++) for(int i = 0; i < n; i++) d[j][i] = 1e17, vis[j][i] = 0;
for(int i = 0; i < n; i++) a[i] = arr[i];
dfs(0);
while (!q[j].empty()) {
auto [cost, u] = q[j].top(); q[j].pop();
if (vis[j][u]) continue;
vis[j][u] = 1;
if (u == h) continue;
for(auto &[v, w] : adj[u]) {
if (j < k && a[u] == 2 && d[j + 1][v] > d[j][u] / 2 + w) q[j + 1].push({d[j + 1][v] = d[j][u] / 2 + w, v});
if (d[j][v] > d[j][u] + w) q[j].push({d[j][v] = d[j][u] + w, v});
}
}
double ans = 1e18;
for(int j = 0; j <= k; j++) ans = min(ans, d[j][h]);
if (ans > 1e16) ans = -1;
return ans;
}
#ifdef ngu
int main() {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
int T;
assert(1 == scanf("%d", &T));
while (T--){
int N,M,K,H;
assert(4 == scanf("%d %d %d\n%d", &N, &M, &K, &H));
std::vector<int> x(M);
std::vector<int> y(M);
std::vector<int> c(M);
std::vector<int> arr(N);
for (int i=0;i<N;i++)
assert(1 == scanf("%d", &arr[i]));
for (int i=0;i<M;i++)
assert(3 == scanf("%d %d %d", &x[i], &y[i], &c[i]));
printf("%.12lf\n", solve(N, M, K, H, x, y, c, arr));
}
}
#endif // ngu