Submission #749126

#TimeUsernameProblemLanguageResultExecution timeMemory
749126marvinthangCyberland (APIO23_cyberland)C++17
100 / 100
2764 ms401156 KiB
#include "cyberland.h"
#include <bits/stdc++.h>
 
using namespace std;
 
#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif
 
template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

// end of template

const int MAX = 1e5 + 5;
const double INF = 1e15;
const double EPS = 1e-9;

vector <pair <int, int>> adj[MAX];

double solve(int N, int M, int K, int H, vector <int> x, vector <int> y, vector <int> c, vector <int> arr) {
    REP(i, N) adj[i].clear();
    K = min(K, 70);
    REP(i, M) {
        adj[x[i]].emplace_back(c[i], y[i]);
        adj[y[i]].emplace_back(c[i], x[i]);
    }
    vector <bool> visited(N);
    queue <int> q;
    q.emplace(0);
    visited[0] = true;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (auto [w, v]: adj[u]) if (!visited[v] && v != H) {
            visited[v] = true;
            q.emplace(v);
        }
    }
    vector <vector <vector <double>>> dp(N, vector<vector<double>>(K + 1, vector<double>(2, INF)));
    REP(i, N) if (visited[i] && (!i || !arr[i])) dp[i][0][0] = 0;
    REP(t, K + 1) {
        priority_queue <pair <double, int>, vector <pair <double, int>>, greater <pair <double, int>>> pq;
        REP(u, N) if (dp[u][t][0] < INF - EPS) {
            for (auto [c, v]: adj[u]) if (dp[v][t][1] > dp[u][t][0] + c - EPS) 
                pq.emplace(dp[v][t][1] = dp[u][t][0] + c, v);
        }
        while (!pq.empty()) {
            auto [du, u] = pq.top(); pq.pop();
            if (u == H || abs(du - dp[u][t][1]) > EPS) continue;
            for (auto [c, v]: adj[u]) if (dp[v][t][1] > dp[u][t][1] + c - EPS) pq.emplace(dp[v][t][1] = dp[u][t][1] + c, v);
        }
        if (t < K) REP(i, N) if (arr[i] == 2 && dp[i][t][1] < INF - EPS) dp[i][t + 1][0] = dp[i][t][1] / 2;
    }
    double res = INF;
    REP(i, K + 1) minimize(res, min(dp[H][i][0], dp[H][i][1]));
    return res < INF - EPS ? res : -1;
}

#ifdef LOCAL
#include <cassert>
#include <cstdio>

#include <vector>

int main() {
    file("cyberland");
  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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...