๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

11779๋ฒˆ: ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ 2

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ n(1≤n≤1,000)์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ m(1≤m≤100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ m+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ ๋ฒ„์Šค

www.acmicpc.net

 

๐Ÿง‘‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

๊ธฐ๋ณธ์ ์ธ ํ‹€์€ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ๋ฐ, ์ตœ๋‹จ ๊ฒฝ๋กœ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ ๋„์‹œ๋“ค ๋ฒˆํ˜ธ๋ฅผ ์–ด๋–ป๊ฒŒ ์ €์žฅํ•  ์ง€๊ฐ€ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. ๋ถ„๊ธฐ๊ฐ€ ์ด๋ฃจ์–ด์ง€๋Š” ๊ฐ ๊ฒฝ๋กœ๋งˆ๋‹ค ์ž๊ธฐ๊ฐ€ ๊ฑฐ์ณ ์˜จ ๋„์‹œ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„๊นŒ ์ƒ๊ฐํ–ˆ๋‹ค๊ฐ€, ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์ด ๋“ค ๊ฒƒ์€ ๋ฌผ๋ก  ๋ณต์‚ฌ ๋น„์šฉ์ด ๋งŒ๋งŒ์น˜ ์•Š์„ ๊ฒƒ ๊ฐ™์•„ ํฌ๊ธฐํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ๋‹ค๋ฅธ ๋ถ„์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ๋Š”๋ฐ, ๋ฐฐ์—ด์„ ํ†ตํ•ด ์ด์ „ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๊ธธ์„ ์ถ”์ ํ•˜๋Š” ์ฐธ์‹ ํ•œ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‘ธ์…”์„œ ๋†€๋ž์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, arr[a] = b ๋ผ๋Š” ๊ฒƒ์€ a ๋„์‹œ๋กœ ์˜ค๊ธฐ ์œ„ํ•œ ์ด์ „ ๋„์‹œ๋Š” b ๋„์‹œ๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜์ฃ .

 

์ตœ๋‹จ ๊ฒฝ๋กœ์— ํ•ด๋‹นํ•˜๋Š” ์ตœ์†Œ ๋น„์šฉ์„ ๊ฐฑ์‹ ํ•  ๋•Œ, ํ•ด๋‹น ๋„์‹œ์— ๋Œ€ํ•ด ๊ฑฐ์น˜๋Š” ์ด์ „ ๋„์‹œ ์ •๋ณด๋งŒ ๊ฐฑ์‹ ํ•ด์ฃผ๋ฉด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋ชฉ์ ์ง€ ๋„์‹œ์—์„œ ์ถœ๋ฐœ ๋„์‹œ๋กœ ๊ฑฐ๊พธ๋กœ ์ถ”์ ํ•ด ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ์Šคํƒ(Stack)์— ๋‹ด์•„ ์ถœ๋ ฅํ•˜์˜€๊ตฌ์š”.

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ์ 

 

์˜ˆ์ œ์˜ 1๋ฒˆ ๋„์‹œ ๐Ÿ‘‰๐Ÿป 5๋ฒˆ ๋„์‹œ๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋Š” 1 ๐Ÿ‘‰๐Ÿป 3 ๐Ÿ‘‰๐Ÿป 5 ๋ผ๊ณ  ๋˜์–ด ์žˆ๋Š”๋ฐ, 1 ๐Ÿ‘‰๐Ÿป 4 ๐Ÿ‘‰๐Ÿป 5 ๋„ ์ •๋‹ต์ธ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค. ์ € ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” 1 ๐Ÿ‘‰๐Ÿป 4 ๐Ÿ‘‰๐Ÿป 5 ๊ฐ€ ์ถœ๋ ฅ์ด ๋˜์—ˆ๋Š”๋ฐ ์ •๋‹ต ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๊ฑธ ๋ณด๋‹ˆ, ์—ฌ๋Ÿฌ ์ตœ๋‹จ ๊ฒฝ๋กœ ์ค‘ ํ•˜๋‚˜๋งŒ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๋“ฏํ•ด ๋ณด์ž…๋‹ˆ๋‹ค. 

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <vector>
#include <queue>
#include <stack>

using namespace std;
using uint32 = unsigned int;
using Info = pair<uint32, uint32>;    // <๋น„์šฉ, ๋ชฉ์ ์ง€>

#define FAST_IO ios::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define MAX 3000000000

vector<uint32> Costs;

stack<int> Dijkstra(const vector<vector<Info>>& routes, const uint32 N, uint32 startCity, uint32 destinationCity) {
    vector<uint32> traces(N + 1);
    priority_queue<Info, vector<Info>, greater<Info>> pq;

    Costs[startCity] = 0;
    pq.emplace(0, startCity);

    while (!pq.empty()) {
        uint32 cost = pq.top().first;
        uint32 city = pq.top().second;
        pq.pop();

        if (Costs[city] < cost)
            continue;
        
        for (const auto& adjacentCity : routes[city]) {
            uint32 nextCost = cost + adjacentCity.first;
            uint32 nextCity = adjacentCity.second;

            if (nextCost < Costs[nextCity]) {
                Costs[nextCity] = nextCost;
                traces[nextCity] = city;
                pq.emplace(nextCost, nextCity);
            }
        }
    }

    stack<int> answers;
    uint32 city = destinationCity;
    answers.push(city);

    while (traces[city] != 0) {
        answers.push(traces[city]);
        city = traces[city];
    }

    return answers;
}

int main() {
    FAST_IO
    uint32 n, m, from, to, cost;
    cin >> n >> m;

    vector<vector<Info>> busRoutes(n + 1, vector<Info>());
    Costs.resize(n + 1, MAX);

    for (int i = 0; i < m; i++) {
        cin >> from >> to >> cost;
        busRoutes[from].emplace_back(cost, to);
    }

    uint32 startCity, destinationCity;
    cin >> startCity >> destinationCity;

    stack<int> routes = Dijkstra(busRoutes, n, startCity, destinationCity);
    
    cout << Costs[destinationCity] << "\n";
    cout << routes.size() << "\n";

    while (!routes.empty()) {
        cout << routes.top() << " ";
        routes.pop();
    }

    return 0;
}

 

 

728x90
๋ฐ˜์‘ํ˜•