Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2-wnsmir #8

Merged
merged 1 commit into from
Oct 28, 2024
Merged

2-wnsmir #8

merged 1 commit into from
Oct 28, 2024

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Oct 4, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/1916

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

25m

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ตœ์†Œ๋น„์šฉ๋ฌธ์ œ๋ฅผ ๋ณด์ž๋งˆ์ž ์ง€๋‚œํ•™๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์‹œ๊ฐ„์— ๋ฐฐ์šด
๋‹ค์ต์ŠคํŠธ๋ผ, ๋ฒจ๋งŒํฌ๋“œ, ํ”Œ๋กœ์ด๋“œ์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ค‘
์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผํ• ์ง€ ๊ณ ๋ฏผ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์„ธ๊ฐ€์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์ˆœ์„œ๋Œ€๋กœ O(E \log V), O(VE), O(V^3)์ธ๋ฐ
์Œ์ˆ˜๊ฐ€์ค‘์น˜๊ฐ€ ์—†๊ธฐ๋•Œ๋ฌธ์— ๋ฒจ๋งŒํฌ๋“œ๋Š” ์ œ์™ธํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ , ์‹œ์ž‘๊ณผ ๋์ ์ด ์ •ํ•ด์ ธ ๋ชจ๋“  ์Œ์„ ์•Œ ํ•„์š”๋Š” ์—†์—ˆ๊ธฐ์— ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์ตœ์ ์˜ ๋ฌธ์ œํ’€์ด๋ฒ•์ด๊ฒ ๋‹ค ์‹ถ์—ˆ์Šต๋‹ˆ๋‹ค.

  1. graph์— ๋ชจ๋“  ๊ฐ„์„ data ์ €์žฅ
  2. ์ถœ๋ฐœ์ ์—์„œ ๊ฐ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด์— ์ €์žฅ ๋ฐ ์ดˆ๊ธฐํ™”
  3. ์šฐ์„ ์ˆœ์œ„ํ ์‚ฌ์šฉ (๋น„์šฉ, ๋…ธ๋“œ)ํ˜•์‹์œผ๋กœ push => ๋น„์šฉ์ด ์ž‘์€๊ฒƒ๋ถ€ํ„ฐ, ํฐ๊ฒƒ์€ ๋ฌด์‹œ
  4. ํ์— ํŠœํ”Œ์ด ์กด์žฌํ•œ๋‹ค๋ฉด, ๋น„์šฉ์ด ๊ฐ€์žฅ์ ์€ ๋…ธ๋“œ์—์„œ ์ธ์ ‘๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ๋”ํ•œ ํ›„ distance๊ฐ’ ๊ฐฑ์‹ 
  5. ํ๊ฐ€ ๋นŒ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํ์— ํŠœํ”Œํ˜•์‹์œผ๋กœ ์ €์žฅํ•˜๋ฉด ์ตœ์†Œํž™์€ ํŠœํ”Œ์˜ ์ฒซ๋ฒˆ์งธ ์›์†Œ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ๋‹ค๋Š”๊ฒƒ์„ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
ํž™์„ ์ด์šฉํ•œ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(E \log V)์ธ ์ด์œ ๋Š” ๋จผ์ € ํž™์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(E \log V)์ด๊ณ  ์ตœ๋Œ€๋ฅผ ๊ฐ€์ •ํ—ธ์„๋•Œ O(E \log V),
์ด๋ฅผ ๋ชจ๋“  ๊ฐ„์„ ์˜ ์ˆ˜ E๋งŒํผ ๋ฐ˜๋ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„์„ ์ˆ˜์— ๋” ํฐ ์˜ํ–ฅ์„ ๋ฐ›์œผ๋ฏ€๋กœ ์ตœ์ข…์ ์œผ๋กœ O(E \log V)๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

@wnsmir wnsmir changed the title ์ตœ์†Œ๋น„์šฉ๊ตฌํ•˜๊ธฐ 2-wnsmir Oct 4, 2024
@wnsmir wnsmir self-assigned this Oct 4, 2024
Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ทธ๋ ‡๋‹ค์น˜๊ณ  ๊ทธ๋ž˜ํ”„๋ฅผ ํ™œ์šฉํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋„ ์˜ค๋žœ๋งŒ์ด๋„ค์š”.
์ข‹์€ ๋ฌธ์ œ ์†Œ๊ฐœ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ „ ์ด๊ฑฐ ์–ด๋ ต๋˜๋ฐ ์—„์ฒญ ๋นจ๋ฆฌ ํ‘ธ์…จ๋„ค์š”..

๊ทธ๋Ÿฐ๋ฐ ๋ญ”๊ฐ€ ๋ฌธ์ œ์™€ ํ’€์ด์— ์ถ”๊ฐ€ ์„ค๋ช…์ด ์ข€ ๋” ์žˆ์œผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์„œ ์กฐ๊ธˆ ๋‚จ๊ฒจ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋Š” ์Œ์ˆ˜ ๊ฐ€์ค‘์น˜๊ฐ€ ์—†๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๋ฅผ ์‚ฌ์šฉํžˆ์—ฌ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
(์ถœ๋ฐœํ•˜๋Š” ๋„์‹œ๋ฅผ ์ง„์ถœ๊ฐ„์„ ์ด ์žˆ๋Š” ๋…ธ๋“œ, ๋„์ฐฉํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์ง„์ž…๊ฐ„์„ ์ด ์žˆ๋Š” ๋…ธ๋“œ, ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” ๋ฒ„์Šค๋น„)

๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹์—๋Š” 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. (์ œ๊ฐ€ ๋ฐฐ์šฐ๊ธฐ๋กœ๋Š” 2๊ฐ€์ง€)

  1. ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ
  2. ์ธ์ ‘ ํ–‰๋ ฌ
    (ํ˜น์‹œ ์ €์ฒ˜๋Ÿผ ๊ฐ€๋ฌผ๊ฐ€๋ฌผํ•˜์‹  ๋ถ„๋“ค์€ ๋‹ค์‹œ ์ฐพ์•„๋ณด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.)

๋‹ค์ต์ŠคํŠธ๋ผ ๊ตฌํ˜„์—์„œ๋Š” ํ˜„์žฌ ๋…ธ๋“œ์˜ ์ง„์ถœ๊ฐ„์„ ์„ ๋‹ค ๋”ฐ์ ธ ๋ด์•ผํ•˜๋Š” ๋ถ€๋ถ„์ด ์žˆ๊ณ ,
๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด์žˆ์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋”์šฑ ํšจ์œจ ์ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ž˜ํ”„๊ฐ€ ๊ตฌํ˜„๋˜๊ณ ๋‚˜์„œ๋Š” ์ „ํ˜•์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ตฌํ˜„๋ฉ๋‹ˆ๋‹ค.
๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋งŽ์€ ์„ค๋ช…์ด ํ•„์š”ํ•˜๋‹ˆ ์‚ฌ์ง„์œผ๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2024-10-05 02 10 49
(์‚ฌ์ง„ ์ถœ์ฒ˜๋Š” ๊ถŒ์˜คํ  ๊ต์ˆ˜๋‹˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ•์˜์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. ํ˜น์‹œ ๋ฌธ์ œ๋˜์ง„์•Š๊ฒ ์ฃ ..?)

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์ด๊ฒƒ์˜ ๊ธฐ๋ณธ ์…‹ํŒ…์€ ์ตœ๋Œ€ ํž™์ž…๋‹ˆ๋‹ค.
C++์—์„œ๋Š” ์ตœ๋Œ€ ํž™์ด๊ณ  ๋‹ค๋ฅธ ์–ธ์–ด๋Š” ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์šฐ์„ ์ˆœ์œ„ ํ ์ƒ์„ฑ ์‹œ์— firstํ•ญ๋ชฉ์— ๋ฒ„์Šค ๋น„์šฉ์„ ๋„ฃ๊ณ  greater ์กฐ๊ฑด์„ ๋„ฃ์–ด์„œ firstํ•ญ๋ชฉ์— ๋Œ€ํ•œ ์ตœ์†Œํž™์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค๋ฉด...
๋ฒ„์Šค ๋น„์šฉ์ด ์ ์€ ์ˆœ์„œ๋กœ ์ •๋ ฌ์ด ๋ฉ๋‹ˆ๋‹ค.

์ด ์ •๋„๊ฐ€ ์ด๋ฒˆ ๋ฌธ์ œ์—์„œ ์•Œ์•„์•ผํ•˜๋Š” ํ•ต์‹ฌ์ธ ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค.

#include <iostream>
#include <queue>
#include <vector>
#include <limits.h>

using namespace std;

int dijkstra(vector<vector<pair<int, int> > >& graph,int start, int end) {
    int N = graph.size();

    vector<int> dist(N, INT_MAX);
    priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;

    dist[start] = 0;
    pq.push(make_pair(0, start));

    while (!pq.empty()) {
        int currentCost = pq.top().first;
        int currentNode = pq.top().second;
        pq.pop();

        if (currentCost > dist[currentNode]) continue;

        for (auto next : graph[currentNode]) {
            int next_node = next.first;
            int next_cost = next.second;

            if (dist[next_node] > dist[currentNode] + next_cost) {
                dist[next_node] = dist[currentNode] + next_cost;
                pq.push(make_pair(dist[next_node], next_node));
            }
        }
    }
    return dist[end];
}

int main() {
    // 1) input
    int N, M;
    cin >> N >> M;

    vector<vector<pair<int, int> > > graph(N + 1);
    for (int i = 0; i < M; i++) {
        int s, e, w;
        cin >> s >> e >> w;
        graph[s].push_back(make_pair(e, w));
    }

    int start, end;
    cin >> start >> end;
    
    // 2) solution
    cout << dijkstra(graph, start, end) << "\n";

    return 0;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋•๋ถ„์— ๊นŒ๋งฃ๊ฒŒ ์žŠ๊ณ ์žˆ์—ˆ๋˜ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋‹ค์‹œ ๊ณต๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค.
๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ์ค€์šฉ๋‹˜์˜ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
๊ฐœ๋…์„ ๋‹ค์‹œ ํ•™์Šตํ•˜๋ฉด์„œ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋‹ˆ 1์‹œ๊ฐ„์ด ๋„˜์–ด๊ฐ”๋„ค์š”.

์ „ํ˜•์ ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ ์–ธ์–ด๊ฐ€ ๋‹ฌ๋ผ๋„ ํ˜•ํƒœ๋Š” ๋น„์Šทํ•˜๋„ค์š”.

์ด ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ๊ทธ๋ž˜ํ”„ ๊ตฌํ˜„์„ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ ํ•˜๋Š” ๊ฒƒ์ด ์ธ์ ‘ ๋ฐฐ์—ด๋ณด๋‹ค 2๊ฐ€์ง€ ์ธก๋ฉด์—์„œ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

  1. ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„ ์ ˆ์•ฝ
  2. ์ด์–ด์ง„ ๋ชจ๋“  ๋…ธ๋“œ ํƒ์ƒ‰ ์‹œ๊ฐ„ ์ ˆ์•ฝ

์ค€์šฉ๋‹˜์˜ ํ’€์ด์—์„œ ๊ทธ๋ž˜ํ”„๋„ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ์ž˜ ๊ตฌํ˜„๋œ ๊ฒƒ ๊ฐ™๋„ค์š”. ์ข‹์Šต๋‹ˆ๋‹ท.

์“ธ๋ฐ์—†๋Š” ๋ง์ด์ง€๋งŒ c++์—์„œ๋Š” make_pair๋กœ ๋ฌถ์–ด์ค˜์•ผ push๊ฐ€ ๊ฐ€๋Šฅํ•œ๋ฐ ํŒŒ์ด์ฌ์€ ๊ทธ๋ƒฅ ๊ด„ํ˜ธ๋กœ ๋ฌถ๋Š”๊ฑฐ ๋ถ€๋Ÿฝ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ธ์ ‘ํ–‰๋ ฌ๋ณด๋‹จ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๊ฐ€ ์‹œ๊ฐ„๋ณต์žก๋„๋ฉด์—์„œ ๊ฐ„์„ ์ด ๋งŽ์„์ˆ˜๋ก ์œ ๋ฆฌํ•˜๋‹ค ์ƒ๊ฐํ•˜์—ฌ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

๋‹ค๋งŒ ์ด์ƒ๊ฐ์ด ๋„ˆ๋ฌด ๊ตณ์–ด์ ธ ๊ทธ๋ž˜ํ”„๋ผ๋ฉด ์ธ์ ‘ํ–‰๋ ฌ๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๋Š”๋ฐ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ๋งŒ ๋„ˆ๋ฌด ๋‹น์—ฐํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ๋˜๊ฒƒ ๊ฐ™์•„ ๋’ค๋Œ์•„๋ณด๊ฒŒ ๋˜๋Š” ์ฝ”๋ฉ˜ํŠธ์˜€์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ํ’€์–ด๋ดค์”๋‹ˆ๋‹น. ์žŠ์—ˆ๋˜ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ๋Š” ์ข‹์€ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค

code
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
using namespace std;

int n, m;
vector<vector<pair<int, int>>> graph; // pair = { node, dist }

int dijkstra(int src, int dst) {
	vector<int> dist(n+1, INT_MAX);

	// pair = { dist, node } -> ์ตœ๋Œ€ ํž™์˜ ๊ธฐ์ค€์€ pair์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๊ฐ€ ๋จ
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;

	dist[src] = 0;
	pq.push({0, src});

	while (!pq.empty()) {
		int cur_node = pq.top().second;
		int cur_dist = pq.top().first;
		pq.pop();

		//์ด๋ฏธ ์ตœ์ ํ™”๋œ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋  ๋•Œ
		if (cur_dist > dist[cur_node]) continue;

		for (auto adj : graph[cur_node]) {
			int nxt_node = adj.first;
			int nxt_dist = adj.second;

			if (dist[nxt_node] > dist[cur_node] + nxt_dist) {
				dist[nxt_node] = dist[cur_node] + nxt_dist;
				pq.push({dist[nxt_node], nxt_node});
			}
		}
	}
	return dist[dst];
}

int main() {
	cin >> n >> m;

	int src, dst, dist;
	graph.resize(n+1);

	for (int i = 0; i < m; i++) {
		cin >> src >> dst >> dist;
		graph[src].push_back(make_pair(dst, dist));
	}

	cin >> src >> dst;
	
	cout<< dijkstra(src, dst);
	return 0;
}

@kokeunho
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants