Submission #1312769

#TimeUsernameProblemLanguageResultExecution timeMemory
1312769nguyengiabach1201Sightseeing (NOI14_sightseeing)C++20
15 / 25
3589 ms31296 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define el '\n'
#define FNAME "tinhcldd"
#define ll long long
#define int long long
#define MOD (int)(1e9 + 7)
#define INF (ll)(1e18 + 1)

void setup()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    if (fopen(FNAME ".inp", "r"))
    {
        freopen(FNAME ".inp", "r", stdin);
        freopen(FNAME ".out", "w", stdout);
    }

    return;
}

const int N = 5e5 + 5;
const int M = 1e6 + 5; 
int n, m, q;
int head[N], to[M], nxt[M], weight[M], edge_cnt;
int p[N], ans[N];
bool checked[N];

void add_edge(int u, int v, int w) {
    to[++edge_cnt] = v;
    weight[edge_cnt] = w;
    nxt[edge_cnt] = head[u];
    head[u] = edge_cnt;
}

struct cmp
{
    bool operator()(const array<int, 2> &a, const array<int, 2> &b) const
    {
        if (a[1] != b[1])
            return a[1] < b[1];
        return a[0] < b[0];
    }
};

void dijkstra()
{
    for (int i = 0; i <= n; i++) ans[i] = -INF, checked[i] = false;

    priority_queue<array<int, 2>, vector<array<int, 2>>, cmp> pq;
    pq.push({1, INF}), ans[1] = INF;

    while (!pq.empty())
    {
        int u = pq.top()[0], val = pq.top()[1];
        pq.pop();

        if (checked[u])
            continue;

        checked[u] = true;

        for (int i = head[u]; i; i = nxt[i])
        {
            int v = to[i], w = weight[i];

            if (!checked[v] && min(val, w) > ans[v])
            {
                ans[v] = min(val, w);
                pq.push({v, ans[v]});
            }
        }
    }
}

void solve()
{
    cin >> n >> m >> q;

    edge_cnt = 0;
    for (int i = 1; i <= n; i++) head[i] = 0;

    for (int x, y, z, i = 1; i <= m; ++i) {
        cin >> x >> y >> z;
        add_edge(x, y, z);
        add_edge(y, x, z);
    }

    for (int i = 1; i <= q; ++i)
        cin >> p[i];

    dijkstra();

    for (int i = 1; i <= q; ++i)
        cout << (ans[p[i]] == -INF ? -1 : ans[p[i]]) << el;

    return;
}

signed main()
{
    setup();
    solve();
    return 0;
}

Compilation message (stderr)

sightseeing.cpp: In function 'void setup()':
sightseeing.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen(FNAME ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
sightseeing.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         freopen(FNAME ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...