Submission #333693

# Submission time Handle Problem Language Result Execution time Memory
333693 2020-12-07T13:56:07 Z ncduy0303 Sightseeing (NOI14_sightseeing) C++17
25 / 25
2814 ms 195788 KB
#include <bits/stdc++.h>

using namespace std;

#define ar array
#define ll long long

const int MAX_N = 5e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;

int n, m, q;
vector<ar<int,3>> el;
vector<ar<int,2>> adj[MAX_N];
vector<int> dist;

struct DSU {
    vector<int> p, sz;
    DSU(int n) {
        p.resize(n); for (int i = 0; i < n; i++) p[i] = i;
        sz.assign(n, 1);
    }
    int find(int u) {return u == p[u] ? u : p[u] = find(p[u]);}
    bool same(int u, int v) {return find(u) == find(v);}
    void merge(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;
        if (sz[u] > sz[v]) swap(u, v);
        sz[v] += sz[u];
        p[u] = v;
    }
};

void dijk(int s) {
    dist.assign(n + 1, 0);
    priority_queue<ar<int,2>> pq;
    dist[s] = INF; pq.push({0, s});
    while (pq.size()) {
        auto [d, u] = pq.top(); pq.pop();
        if (d > dist[u]) continue;
        for (auto [v, w] : adj[u]) {
            if (dist[v] < min(dist[u], w)) {
                dist[v] = min(dist[u], w);
                pq.push({dist[v], v});
            }
        }
    } 
}

void solve() {
    cin >> n >> m >> q;
    while (m--) {
        int u, v, w; cin >> u >> v >> w;
        el.push_back({w, u, v});
    }
    sort(el.rbegin(), el.rend());
    DSU uf(n + 1);
    for (auto[ w, u, v] : el) {
        if (uf.same(u, v)) continue;
        uf.merge(u, v);
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    dijk(1);
    while (q--) {
        int u; cin >> u;
        cout << dist[u] << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t  << ": ";
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12012 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12268 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 15460 KB Output is correct
2 Correct 40 ms 15076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2024 ms 126808 KB Output is correct
2 Correct 2814 ms 195788 KB Output is correct