Submission #976888

# Submission time Handle Problem Language Result Execution time Memory
976888 2024-05-07T08:33:22 Z phcbaka Relay Marathon (NOI20_relaymarathon) C++17
17 / 100
146 ms 29380 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
using ii = pair <int, int>;

int n, m, k;


void read()
{
    cin >> n >> m >> k;
}

struct vet
{
    bool spec[511] = {false};
    int a[511][511];
    ii dp[511][511];

    void calc()
    {
        memset(a, 0x3f3f, sizeof(a));
        for (int i = 1; i <= n; ++i) a[i][i] = 0;

        for (int i = 1; i <= m; ++i)
        {
            int u, v, L;
            cin >> u >> v >> L;
            a[u][v] = min(a[u][v], L);
            a[v][u] = min(a[v][u], L);
        }

        for (int i = 1; i <= k; ++i)
        {
            int u;
            cin >> u;
            spec[u] = 1;
        }

        for (int k = 1; k <= n; ++k)
            for (int i = 1; i <= n; ++i)
                for (int j = 1; j <= n; ++j)
                    a[i][j] = min(a[i][j], a[i][k]+a[k][j]);

        for (int i = 1; i <= n; ++i)
        {
            for (int j = 1; j <= n; ++j)
                if (spec[i] == 0 or spec[j] == 0 or i == j)
                {
                    dp[i][j] = ii(1e9, j);
                }
                else dp[i][j] = ii(a[i][j], j);

            sort (dp[i]+1, dp[i]+n+1);
        }

        int ans = LLONG_MAX;

        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                for (int t = 1; t <= min(5ll, n); ++t)
                    for (int c = 1; c <= min(5ll, n); ++c)
                        if (i != j and i != dp[i][t].second and i != dp[j][c].second
                            and j != dp[i][t].second and j != dp[j][c].second
                            and dp[i][t].second != dp[j][c].second)
                        {
                            ans = min(ans, dp[i][t].first + dp[j][c].first);
                        }

        cout << ans;
        exit(0);
    }
}Vetcan;

bool minimize(int &a, int b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

struct fullSub
{
    vector <ii> g[100011];
    int spec[100011];

    tuple <int, int, int> best_pair()
    {
            vector <int> d(n+1, INT_MAX);
            vector <int> head(n+1, 0);

            priority_queue <ii, vector <ii>, greater <ii>> pq;

            for (int i = 1; i <= n; ++i)
            {
                if (spec[i])
                {
                    spec[i] = i;
                    d[i] = 0;
                    pq.emplace(d[i], i);
                }
            }
        while (!pq.empty())
        {
            auto [du, u] = pq.top();
            pq.pop();

            if (d[u] != du)
                continue;

            for (auto [v, w] : g[u])
            {
                if (d[v] > d[u] + w)
                {
                    d[v] = d[u] + w;
                    head[v] = head[u];
                    pq.emplace(d[v], v);
                }
            }
        }

        int best = INT_MAX;
        int best_u = -1, best_v = -1;

        for (int u = 1; u <= n; ++u)
        {
            for (auto [v, w] : g[u])
            {
                if (d[u] != INT_MAX and d[v] != INT_MAX
                    and head[u] != head[v])
                {
                    if (minimize(best, d[u] + d[v] + w))
                    {
                        best = d[u] + d[v] + w;
                        best_u = head[u];
                        best_v = head[v];
                    }
                }
            }
        }

        return {best, best_u, best_v};
    }

    ii nearest_spec(int s)
    {
        vector <int> d(n+1, INT_MAX);
        priority_queue <ii, vector <ii>, greater <ii>> pq;

        d[s] = 0;
        pq.emplace(d[s], s);

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

            if (d[u] != du)
                continue;

            if (spec[u])
            {
                return {d[u], u};
            }

            for (auto [v, w] : g[u])
            {
                if (d[v] > d[u] + w)
                {
                    d[v] = d[u] + w;
                    pq.emplace(d[v], v);
                }
            }
        }

        return ii (INT_MAX, 0); /// khong toi duoc
    }

    void readMore()
    {
        while (m--)
        {
            int u, v, w;
            cin >> u >> v >> w;
            g[u].push_back(ii (v, w));
            g[v].push_back(ii (u, w));
        }
        while (k--)
        {
            int u;
            cin >> u;
            spec[u] = 1;
        }
    }

    void calc()
    {
        readMore();

        /// giả sử tìm được đường đi ngắn nhất giữa 2 đỉnh bất kỳ
        /// là từ u -> v
        /// thì trong 4 số x1, y1, x2, y2 kiểu gì cũng có 1 số là u, 1 số là v
        /// tại nếu không có thì thay thành (u, v) dĩ nhiên tốt hơn
        /// để tìm từ u -> v thì push hết vào pq rồi dijsktra

        auto [min_d, x, y] = best_pair();

        int best = INT_MAX;
        spec[x] = spec[y] = 0;

        /// nếu (x1, x2) = (u, v) thì dijsktra giống như lúc tìm (u, v) để tính đán án
        /// nếu x1 = u và y1 = v thì dijstra từ u và v để tìm đỉnh đặc biệt gần nó nhất

        /// a = x, b = y
        {
            auto [sec_min_d, z, t] = best_pair();
            minimize(best, min_d + sec_min_d);
        }
        /// a = x, c = y
        {
            auto [nearest_a, z] = nearest_spec(x);
            spec[z] = 0;
            auto [nearest_b, t] = nearest_spec(y);
            minimize(best, nearest_a + nearest_b);
            spec[z] = 1;
        }

        {
            auto [nearest_a, z] = nearest_spec(y);
            spec[z] = 0;
            auto [nearest_b, t] = nearest_spec(x);
            minimize(best, nearest_a + nearest_b);
            spec[z] = 1;
        }

        cout << best;
    }
}full;
signed main()
{
    cin.tie(0)->sync_with_stdio(0);

    #define task "cities"
    if (fopen(task ".inp", "r"))
    {
        freopen(task ".inp", "r", stdin);
        freopen(task ".out", "w", stdout);
    }

    read ();

    if (n <= 500)
    {
        Vetcan.calc();
    }

    full.calc();
    return 0;
}

Compilation message

RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:250:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  250 |         freopen(task ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:251:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  251 |         freopen(task ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9560 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 3 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 3 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 3 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9728 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 3 ms 9564 KB Output is correct
17 Correct 3 ms 9564 KB Output is correct
18 Correct 3 ms 9564 KB Output is correct
19 Correct 2 ms 9560 KB Output is correct
20 Correct 3 ms 9564 KB Output is correct
21 Correct 2 ms 9724 KB Output is correct
22 Correct 2 ms 9564 KB Output is correct
23 Correct 3 ms 9564 KB Output is correct
24 Correct 2 ms 9560 KB Output is correct
25 Correct 2 ms 9564 KB Output is correct
26 Correct 2 ms 9564 KB Output is correct
27 Correct 3 ms 9564 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 2 ms 9560 KB Output is correct
30 Correct 3 ms 9732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9560 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 3 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 3 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 3 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9728 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 3 ms 9564 KB Output is correct
17 Correct 3 ms 9564 KB Output is correct
18 Correct 3 ms 9564 KB Output is correct
19 Correct 2 ms 9560 KB Output is correct
20 Correct 3 ms 9564 KB Output is correct
21 Correct 2 ms 9724 KB Output is correct
22 Correct 2 ms 9564 KB Output is correct
23 Correct 3 ms 9564 KB Output is correct
24 Correct 2 ms 9560 KB Output is correct
25 Correct 2 ms 9564 KB Output is correct
26 Correct 2 ms 9564 KB Output is correct
27 Correct 3 ms 9564 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 2 ms 9560 KB Output is correct
30 Correct 3 ms 9732 KB Output is correct
31 Correct 116 ms 9564 KB Output is correct
32 Correct 124 ms 9708 KB Output is correct
33 Correct 125 ms 9728 KB Output is correct
34 Correct 119 ms 9704 KB Output is correct
35 Correct 120 ms 9700 KB Output is correct
36 Correct 122 ms 9564 KB Output is correct
37 Correct 128 ms 9564 KB Output is correct
38 Correct 118 ms 9716 KB Output is correct
39 Correct 142 ms 10648 KB Output is correct
40 Correct 124 ms 9816 KB Output is correct
41 Correct 117 ms 9564 KB Output is correct
42 Correct 131 ms 9816 KB Output is correct
43 Correct 122 ms 9736 KB Output is correct
44 Correct 120 ms 9560 KB Output is correct
45 Correct 125 ms 9564 KB Output is correct
46 Correct 141 ms 10844 KB Output is correct
47 Correct 125 ms 9820 KB Output is correct
48 Correct 130 ms 10560 KB Output is correct
49 Correct 146 ms 10672 KB Output is correct
50 Correct 121 ms 9564 KB Output is correct
51 Correct 127 ms 9712 KB Output is correct
52 Correct 119 ms 9732 KB Output is correct
53 Correct 133 ms 10228 KB Output is correct
54 Correct 139 ms 10720 KB Output is correct
55 Correct 120 ms 9560 KB Output is correct
56 Correct 132 ms 9560 KB Output is correct
57 Correct 122 ms 9716 KB Output is correct
58 Correct 137 ms 10844 KB Output is correct
59 Correct 118 ms 9564 KB Output is correct
60 Correct 124 ms 9712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 50 ms 29380 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9560 KB Output is correct
2 Correct 2 ms 9564 KB Output is correct
3 Correct 3 ms 9564 KB Output is correct
4 Correct 2 ms 9564 KB Output is correct
5 Correct 3 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 3 ms 9564 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9728 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9564 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 3 ms 9564 KB Output is correct
17 Correct 3 ms 9564 KB Output is correct
18 Correct 3 ms 9564 KB Output is correct
19 Correct 2 ms 9560 KB Output is correct
20 Correct 3 ms 9564 KB Output is correct
21 Correct 2 ms 9724 KB Output is correct
22 Correct 2 ms 9564 KB Output is correct
23 Correct 3 ms 9564 KB Output is correct
24 Correct 2 ms 9560 KB Output is correct
25 Correct 2 ms 9564 KB Output is correct
26 Correct 2 ms 9564 KB Output is correct
27 Correct 3 ms 9564 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 2 ms 9560 KB Output is correct
30 Correct 3 ms 9732 KB Output is correct
31 Correct 116 ms 9564 KB Output is correct
32 Correct 124 ms 9708 KB Output is correct
33 Correct 125 ms 9728 KB Output is correct
34 Correct 119 ms 9704 KB Output is correct
35 Correct 120 ms 9700 KB Output is correct
36 Correct 122 ms 9564 KB Output is correct
37 Correct 128 ms 9564 KB Output is correct
38 Correct 118 ms 9716 KB Output is correct
39 Correct 142 ms 10648 KB Output is correct
40 Correct 124 ms 9816 KB Output is correct
41 Correct 117 ms 9564 KB Output is correct
42 Correct 131 ms 9816 KB Output is correct
43 Correct 122 ms 9736 KB Output is correct
44 Correct 120 ms 9560 KB Output is correct
45 Correct 125 ms 9564 KB Output is correct
46 Correct 141 ms 10844 KB Output is correct
47 Correct 125 ms 9820 KB Output is correct
48 Correct 130 ms 10560 KB Output is correct
49 Correct 146 ms 10672 KB Output is correct
50 Correct 121 ms 9564 KB Output is correct
51 Correct 127 ms 9712 KB Output is correct
52 Correct 119 ms 9732 KB Output is correct
53 Correct 133 ms 10228 KB Output is correct
54 Correct 139 ms 10720 KB Output is correct
55 Correct 120 ms 9560 KB Output is correct
56 Correct 132 ms 9560 KB Output is correct
57 Correct 122 ms 9716 KB Output is correct
58 Correct 137 ms 10844 KB Output is correct
59 Correct 118 ms 9564 KB Output is correct
60 Correct 124 ms 9712 KB Output is correct
61 Runtime error 50 ms 29380 KB Execution killed with signal 6
62 Halted 0 ms 0 KB -