2 min read

๋ฐฑ์ค€ 17835๋ฒˆ ๋ฉด์ ‘๋ณด๋Š” ์Šน๋ฒ”์ด๋„ค

๋ฌธ์ œ ๋งํฌ

๋ชจ๋“  ์ •์ ์—์„œ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋Œ๋ฆฐ๋‹ค๊ฑฐ๋‚˜, ํ”Œ๋กœ์ด๋“œ๋ฅผ ์จ์„œ ๊ฐ ์ •์ ์—์„œ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๋ชจ๋‘ ๊ตฌํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ๋ฐ›๋Š”๋‹ค.

๊ฐ€์ƒ์˜ ์ •์ ์„ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ , ๋ฉด์ ‘์žฅ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ 0์ด๋ผ ํ•˜์ž. ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์„œ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๋Œ๋ฆฌ๋ฉด ๊ฐ ์ •์ ์—์„œ ๋ฉด์ ‘์žฅ๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์–‘๋ฐฉํ–ฅ ๊ฐ„์„ ์ด ์•„๋‹˜์— ์ฃผ์˜ํ•˜์ž.

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n, m, k;
    cin >> n >> m >> k;

    vector<vector<pair<int, int>>> adj(n + 1);  // cost, next
    vector<int> mv(k);

    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        // ๊ธธ์„ ๋ฐ˜๋Œ€๋กœ ๋„ฃ์–ด์ค€๋‹ค
        adj[b].emplace_back(c, a);
    }

    for (auto& x : mv) {
        cin >> x;
    }

    vector<ll> dist(n + 1);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
        pq;

    fill(dist.begin(), dist.end(), 1e18);

    // ๋ฉด์ ‘์žฅ๊นŒ์ง€ ๊ฑฐ๋ฆฌ 0์œผ๋กœ ์„ค์ •
    // ๊ธธ ๋ฐ˜๋Œ€๋กœ ๋„ฃ๊ณ  ๋ชจ๋“  ๋ฉด์ ‘์žฅ์—์„œ ์ถœ๋ฐœํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ํ’€๊ธฐ
    for (auto& x : mv) {
        pq.emplace(0, x);
        dist[x] = 0;
    }

    while (!pq.empty()) {
        auto [cost, cur] = pq.top();
        pq.pop();
        if (dist[cur] < cost) continue;
        for (auto& [c, n] : adj[cur]) {
            if (dist[n] > cost + c) {
                dist[n] = cost + c;
                pq.emplace(dist[n], n);
            }
        }
    }

    ll idx = 0, d = 0;

    for (int i = 1; i <= n; i++) {
        if (dist[i] > d) {
            d = dist[i];
            idx = i;
        }
    }

    cout << idx << '\n' << d << '\n';

    return 0;
}

long long ์จ์•ผํ•œ๋‹ค. ๊ฑฐ๋ฆฌ์˜ ์ตœ๋Œ“๊ฐ’์ด int ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ„๋‹ค.