Submission #772777

# Submission time Handle Problem Language Result Execution time Memory
772777 2023-07-04T11:13:02 Z boris_mihov Cyberland (APIO23_cyberland) C++17
49 / 100
35 ms 8104 KB
#include "cyberland.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>

typedef long long llong;
const int MAXN = 100000 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;

int n, m, k, h;
bool vis[MAXN];
llong dist[MAXN];
int ability[MAXN];
std::vector <std::pair <int,int>> g[MAXN];
std::priority_queue <std::pair <llong,int>> pq;
double toReach[MAXN];

void reset()
{
    for (int i = 1 ; i <= n ; ++i)
    {
        g[i].clear();
        dist[i] = ability[i] = 0;
        toReach[i] = 0.0;
        vis[i] = false;
    }

    while (!pq.empty())
    {
        pq.pop();
    }
}

void dfs(int node)
{
    vis[node] = true;
    if (node == h)
    {
        return;
    }

    for (const auto &[u, e] : g[node])
    {
        if (vis[u])
        {
            continue;
        }

        dfs(u);
    }
}

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) 
{
    reset();
    n = N;
    m = M;
    k = K;
    h = H + 1;

    for (int i = 0 ; i < m ; ++i)
    {
        g[x[i] + 1].emplace_back(y[i] + 1, c[i]);
        g[y[i] + 1].emplace_back(x[i] + 1, c[i]);
    }

    dfs(1);
    if (!vis[h])
    {
        return -1;
    }

    for (int i = 2 ; i <= n ; ++i)
    {
        ability[i] = arr[i - 1];
    }

    std::fill(dist + 1, dist + 1 + n, INF);
    for (int i = 1 ; i <= n ; ++i)
    {
        if (!vis[i]) continue;
        if (ability[i] == 0)
        {
            dist[i] = 0;
            pq.push({0, i});
        }
    }

    std::fill(vis + 1, vis + 1 + n, false);
    while (!pq.empty())
    {
        auto [currDist, node] = pq.top();
        pq.pop();

        if (vis[node])
        {
            continue;
        }
        
        vis[node] = true;
        if (node != h)
        {
            for (const auto &[u, e] : g[node])
            {
                if (dist[u] > dist[node] + e)
                {
                    dist[u] = dist[node] + e;
                    pq.push({-dist[u], u});
                }
            }
        }
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        toReach[i] = dist[i];
        if (ability[i] == 2)
        {
            int minEdge = INTINF;
            for (const auto &[u, e] : g[i])
            {
                if (u == h)
                {
                    continue;
                }

                minEdge = std::min(minEdge, e);
            }

            toReach[i] /= 2.0;
            if (k > 60)
            {
                toReach[i] = std::min(toReach[i], (double)2*minEdge);
                break;
            }

            for (int j = 1 ; j < k && toReach[i] >= 2*minEdge ; ++j)
            {
                toReach[i] /= 2.0;
                toReach[i] += minEdge;
            }
        }
    }

    std::fill(vis + 1, vis + 1 + n, false);
    std::fill(dist + 1, dist + 1 + n, INF);
    pq.push({0, h});
    dist[h] = 0;

    while (!pq.empty())
    {
        auto [currDist, node] = pq.top();
        pq.pop();

        if (vis[node])
        {
            continue;
        }
        
        vis[node] = true;
        for (const auto &[u, e] : g[node])
        {
            if (dist[u] > dist[node] + e)
            {
                dist[u] = dist[node] + e;
                pq.push({-dist[u], u});
            }
        }
    }

    double ans = INF;
    for (int i = 1 ; i <= n ; ++i)
    {
        if (dist[i] == INF || toReach[i] == INF)
        {
            continue;
        }

        ans = std::min(ans, (double)dist[i] + toReach[i]);
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 14 ms 2772 KB Correct.
2 Correct 18 ms 2768 KB Correct.
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2844 KB Correct.
2 Correct 22 ms 2780 KB Correct.
3 Correct 21 ms 2832 KB Correct.
4 Correct 22 ms 2836 KB Correct.
5 Correct 34 ms 2772 KB Correct.
6 Correct 20 ms 3592 KB Correct.
7 Correct 28 ms 3616 KB Correct.
8 Correct 12 ms 4524 KB Correct.
9 Correct 21 ms 2736 KB Correct.
10 Correct 20 ms 2740 KB Correct.
# Verdict Execution time Memory Grader output
1 Correct 34 ms 2808 KB Correct.
2 Correct 24 ms 2836 KB Correct.
3 Correct 24 ms 2876 KB Correct.
4 Correct 26 ms 2724 KB Correct.
5 Correct 23 ms 2732 KB Correct.
6 Correct 7 ms 3524 KB Correct.
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 8104 KB Wrong Answer.
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 21 ms 2804 KB Correct.
2 Correct 31 ms 2888 KB Correct.
3 Correct 22 ms 2880 KB Correct.
4 Correct 26 ms 3808 KB Correct.
5 Correct 18 ms 2744 KB Correct.
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2916 KB Correct.
2 Correct 17 ms 2820 KB Correct.
3 Correct 35 ms 7724 KB Correct.
4 Correct 16 ms 3680 KB Correct.
5 Correct 19 ms 2748 KB Correct.
6 Correct 20 ms 2876 KB Correct.
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 2884 KB Wrong Answer.
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 2880 KB Wrong Answer.
2 Halted 0 ms 0 KB -