#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <chrono>
#include <random>
#include <complex>
#include <numeric>
#include <assert.h>
#include <functional>
#include <climits>
#include <cstring>
#include <iterator>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using ushort = unsigned short;
#ifndef LOCAL
#define endl "\n"
#endif
#define f first
#define s second
#define vec vector
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
template<typename T1, typename T2>
istream& operator>> (istream &in, pair<T1, T2> &p) { in >> p.f >> p.s; return in; }
template<typename T1, typename T2>
ostream& operator<< (ostream &out, pair<T1, T2> p) { out << "(" << p.f << " " << p.s << ")"; return out; }
#define printv(v) cerr << #v << " "; for (auto &x : v) { cerr << x << " "; } cerr << endl;
#define printvv(v) cerr << #v << " "; for (auto &x : v) { for (auto &y : x) { cerr << y << " "; } cerr << endl; }
#define debug(x) cerr << #x << " " << x << endl;
template<typename T>
istream& operator>> (istream &in, vector<T> &v) {
for (auto &x : v) {
in >> x;
}
return in;
}
template<typename T>
ostream& operator<< (ostream &out, vector<T> v) {
for (auto &x : v) {
out << x << " ";
}
return out;
}
template<typename T>
void operator-=(vector<T> &v, int delta) {
for (auto &x : v) {
x -= delta;
}
}
template<typename T>
void operator+=(vector<T> &v, int delta) {
for (auto &x : v) {
x += delta;
}
}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
struct DSU {
vector<int> pr;
DSU(int n) {
pr.resize(n);
iota(all(pr), 0);
}
int get(int v) {
if (pr[v] != v) {
pr[v] = get(pr[v]);
}
return pr[v];
}
bool unite(int a, int b) {
a = get(a);
b = get(b);
if (a == b) {
return false;
}
pr[a] = b;
return true;
}
};
double solve(int n, int m, int k, int h, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) {
vector<vector<pii>> graph(n);
for (int i = 0; i < m; ++i) {
graph[x[i]].pb(mp(y[i], c[i]));
graph[y[i]].pb(mp(x[i], c[i]));
}
queue<int> qq;
qq.push(0);
vector<int> ok(n);
bool was = false;
while (!qq.empty()) {
int v = qq.front();
qq.pop();
ok[v] = true;
if (v == h) {
was = true;
continue;
}
for (auto &pu : graph[v]) {
if (!ok[pu.f]) {
ok[pu.f] = 1;
qq.push(pu.f);
}
}
}
if (!was) {
return -1;
}
priority_queue<pair<int, pair<ld, int>>> q;
vector<ld> d(n * min(101, (k + 1)), 1e18);
vector<int> marked(n * min(101, (k + 1)));
q.push(mp(0, mp(0, 0)));
d[0] = 0;
for (int i = 1; i < n; ++i) {
if (ok[i]) {
if (k > 100 && arr[i] == 2) {
int e = 2e9 + 11;
for (auto &x : graph[i]) {
if (x.f == h) {
continue;
}
if (arr[x.f] == 2) {
e = min(e, x.s);
} else {
e = min(e, x.s * 2);
}
}
q.push(mp(0, mp(-e, i)));
d[i] = e;
}
if (arr[i] == 0) {
q.push(mp(0, mp(0, i)));
d[i] = 0;
}
}
}
k = min(k, 100);
while (!q.empty()) {
int vk = q.top().s.s;
int v = vk % n;
q.pop();
if (marked[vk]) {
continue;
}
marked[vk] = true;
if (v == h) {
continue;
}
for (auto &pu : graph[v]) {
if (d[pu.f + vk - v] > d[vk] + pu.s) {
d[pu.f + vk - v] = d[vk] + pu.s;
q.emplace(v - vk, mp(-d[pu.f + vk - v], pu.f + vk - v));
}
if (vk + n < n * (k + 1) && arr[pu.f] == 2) {
if (d[pu.f + vk - v + n] > (d[vk] + pu.s) / 2) {
d[pu.f + vk - v + n] = (d[vk] + pu.s) / 2;
q.emplace(v - vk - n, mp(-d[pu.f + vk - v + n], pu.f + vk - v + n));
}
}
}
}
ld ans = d[h];
for (int j = 0; j <= k; ++j) {
ans = min(ans, d[h + j * n]);
}
return ans;
}
#ifdef LOCAL
int main() {
freopen("in.txt", "r", stdin);
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
# | 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... |