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>
#define f first
#define s second
using namespace std;
//g++ cyberland.cpp cyberland.h stub.cpp
const int N = 1e5 + 69;
const long long inf = 1e18;
vector<pair<long long, long long>> g[N];
double dis[N][33];
int vis[N], b[N];
void dfs(int v) {
vis[v] = 1;
for (auto u : g[v]) {
if (vis[u.f])
continue;
if (b[u.f] == 0) {
for (int j = 0;j <= 30;j++)
dis[u.f][j] = 0;
}
else if (b[u.f] == 2) {
for (int j = 0;j <= 30;j++) {
dis[u.f][j] = min(dis[u.f][j], dis[v][j] + u.s);
if (j < 30)
dis[u.f][j + 1] = min(dis[u.f][j + 1], (dis[v][j] + u.s) / 2);
}
}
else {
for (int j = 0;j <= 30;j++)
dis[u.f][j] = min(dis[u.f][j], dis[v][j] + u.s);
}
dfs(u.f);
}
}
double solve(int n, int m, int k, int s, vector<int> x, vector<int> y, vector<int> c, vector<int> a) {
//reset
for (int i = 0;i < n;i++) {
g[i].clear();
b[i] = a[i];
vis[i] = 0;
}
int ok = 1;
for (int i = 0;i < m;i++) {
g[x[i]].push_back({y[i], c[i]});
g[y[i]].push_back({x[i], c[i]});
ok &= (x[i] + 1 == y[i]);
}
for (int i = 0;i < n;i++) {
for (int j = 0;j <= k;j++)
dis[i][j] = inf;
}
if (ok) {
double ans = inf;
dis[0][0] = 0;
dfs(0);
for (int i = 0;i <= k;i++)
ans = min(ans, dis[s][i]);
if (ans >= inf)
ans = -1;
assert(ans != -1);
return ans;
}
priority_queue<tuple<double, int, int>> pq;
pq.push({0, 0, 0});
while (!pq.empty()) {
auto x = pq.top();
pq.pop();
int v, j;
double d;
tie(d, v, j) = x;
d = abs(d);
if (a[v] == 0) {
for (int j = 0;j <= k;j++)
dis[v][j] = 0;
d = 0;
}
for (auto u : g[v]) {
if (dis[u.f][j] > d + u.s) {
dis[u.f][j] = d + u.s;
if (u.f != s)
pq.push({-dis[u.f][j], u.f, j});
}
if (a[u.f] == 2) {
if (j < k && dis[u.f][j + 1] > (d + u.s) / 2) {
dis[u.f][j + 1] = (d + u.s) / 2;
if (u.f != s)
pq.push({-dis[u.f][j + 1], u.f, j + 1});
}
}
}
}
double ans = inf;
for (int i = 0;i <= k;i++)
ans = min(ans, dis[s][i]);
if (ans == inf)
ans = -1;
return ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |