Submission #905087

#TimeUsernameProblemLanguageResultExecution timeMemory
905087Fake4FunCyberland (APIO23_cyberland)C++17
100 / 100
110 ms15624 KiB
#include "cyberland.h"
#include <bits/stdc++.h>
 
#define ll long long
#define pa pair<ll, int>
#define fi first
#define se second
using namespace std;
const int maxN = 1e5;
const double oo = 1e18, eps = 1e-7;
int n, fin;
short abi[maxN];
vector<pa> adj[maxN];
ll dist[maxN];
bool mini(ll &a, ll b) {
    if (a > b || a == -1) {
        a = b;
        return true;
    }
    return false;
}
void Dijkstra(int start) {
    for (int i = 0; i < n; i++) dist[i] = -1;
    priority_queue<pa, vector<pa>, greater<pa> > pq;
    dist[start] = 0; pq.emplace(0, start);
    ll D; int node;
    while (!pq.empty()) {
        tie(D, node) = pq.top(); pq.pop();
        if (D != dist[node]) continue;
        for (pa o : adj[node])
            if (mini(dist[o.se], D + o.fi) && o.se != fin) pq.emplace(dist[o.se], o.se);
    }
}
double rDist[2][maxN];
void Ford_Bellman(vector<int> &v) {
    queue<pair<double, int> > q;
    for (int x : v) {
        rDist[0][x] = rDist[1][x];
        q.emplace(rDist[0][x], x);
    }
    double D; int node;
    while (!q.empty()) {
        tie(D, node) = q.front(); q.pop();
        if (D > rDist[0][node]) continue;
        for (pa o : adj[node]) {
            if (o.se == fin) continue;
            if (abi[o.se] == 2) rDist[1][o.se] = min(rDist[1][o.se], (D + o.fi) / 2);
            if (rDist[0][o.se] > D + o.fi + eps) {
                rDist[0][o.se] = D + o.fi;
                q.emplace(rDist[0][o.se], o.se);
            }
        }
    }
}
double solve(int N, int M, int K, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) {
    n = N; fin = H;
    for (int i = 0; i < n; i++) {
        adj[i].clear();
        abi[i] = arr[i];
    }
    for (int i = 0; i < M; i++) {
        adj[x[i]].emplace_back(c[i], y[i]);
        adj[y[i]].emplace_back(c[i], x[i]);
    }
    Dijkstra(0);
    if (dist[fin] == -1) return -1;
    vector<int> posGo;
    posGo.push_back(0);
    for (int i = 0; i < n; i++)
        if (!abi[i] && dist[i] != -1) posGo.push_back(i);
    Dijkstra(fin);
    fill(rDist[0], rDist[0] + n, oo);
    fill(rDist[1], rDist[1] + n, oo);
    double ans = oo;
    for (int i : posGo) {
        ans = min(ans, (double)dist[i]);
        rDist[1][i] = 0;
    }
    // Travel 1st time
    Ford_Bellman(posGo);
    // Update posGo
    posGo.clear();
    for (int i = 0; i < n; i++)
        if (abi[i] == 2 && rDist[1][i] < oo) posGo.push_back(i);
    // Travel K-1 times
    K = min(K, 70); /// important;
    for (int t = 2; t <= K; t++) Ford_Bellman(posGo);
    for (int i : posGo) ans = min(ans, rDist[1][i] + dist[i]);
    return ans;
}
#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...