๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ 2021 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ํ…Œ์ŠคํŠธ level3

by Jouureee 2021. 5. 2.

๋ฌธ์ œ :

programmers.co.kr/learn/courses/30/lessons/72413

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ

6 4 6 2 [[4, 1, 10], [3, 5, 24], [5, 6, 2], [3, 1, 41], [5, 1, 24], [4, 6, 50], [2, 4, 66], [2, 3, 22], [1, 6, 25]] 82 7 3 4 1 [[5, 7, 9], [4, 6, 4], [3, 6, 1], [3, 2, 3], [2, 1, 6]] 14 6 4 5 6 [[2,6,6], [6,3,7], [4,6,7], [6,5,11], [2,5,12], [5,3,20], [2,4

programmers.co.kr

 

๋ถ„์„ :

์ฒซ๋ฒˆ์งธ ์ ‘๊ทผ 

๋ฌธ์ œ๋ฅผ ๋ณด์•„ํ•˜๋‹ˆ a์™€ b๊ฐ€ ๋”ฐ๋กœ ๊ฐ”์„๋•Œ์™€ ์ค‘๊ฐ„ ์ง€์  ๊ฑธ์ณ์„œ ๋„์ฐฉํ•œ ์š”๊ธˆ ์ค‘ ์ตœ์†Œ ์š”๊ธˆ์„ ์ถœ๋ ฅํ•˜๋ผ๋Š” ๋ง์„ ๋ณด๊ณ  ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๋– ์˜ฌ๋ ธ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋˜๊ฒ ๊ตฌ๋‚˜ ํ•˜๊ณ  

s ์ถœ๋ฐœ์ง€์ ์—์„œ   int cost =  dijkstra(s, a) + dijkstra(s, b); a๋Š” a ๋„์ฐฉ์ง€๊นŒ์ง€ ์ตœ๋‹จ ๊ฒฝ๋กœ + b๋Š” b ๋„์ฐฉ์ง€๊นŒ์ง€ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๋”ํ•œ cost๊ฐ’๊ณผ

์ค‘๊ฐ„ ์ง€์ ์„ ๊ฑฐ์ณ ๊ฐ„ ๊ฒฝ์šฐ cost = min(cost, dijkstra(i, a) + dijkstra(i, b) + dijkstra(s, i)); for๋ฌธ์„ ๋Œ๋ ค ๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•ด i ์ค‘๊ฐ„ ์ง€์ ์„ ๊ฑฐ์ณ ๊ฐ„ ๊ฒฝ์šฐ ์ค‘ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜๋ฉด ๋˜๊ฒ ๋‹ค ํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ .. no ...

 

๊ทธ๋ž˜์„œ ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ๋กœ ์ ‘๊ทผํ•˜๋Š”๊ฒŒ ์˜คํžˆ๋ ค ๋น ๋ฅผ ์ˆ˜๋„ ์žˆ๋‹ค ํ•˜๋Š”๋ฐ 

๋‚ด ์ฝ”๋“œ์—์„œ ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์„ ์ฐพ์•„๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. 

main ํ•จ์ˆ˜์—์„œ ๋‹ค์ต์ŠคํŠธ๋ผ ํ•จ์ˆ˜๋ฅผ ๋ถ€๋ฅผ๋•Œ 3์ค‘ for๋ฌธ์ด ๋˜๋ฏ€๋กœ ์ด ๋ถ€๋ถ„์—์„œ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์ ๊ฒŒ ๋ถ€๋ฅด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ƒ๊ฐํ•˜๋ฉด ๋ ๊ฒƒ ๊ฐ™์•˜๋‹ค.

 

c++ ์ฝ”๋“œ : ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋‚œ ์ฝ”๋“œ ๐Ÿ˜ต 

//
//  [KAKAO] 2021_sharedTaxi.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/04/28.
//

#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>

#define INF 1e9

using namespace std;
priority_queue<pair<int, int>> pq;
vector<pair<int, int>> vertex[201];

int d[201]; //๊ฑฐ๋ฆฌ์˜ ํ•ฉ

int dijkstra(int start, int end){
    fill(d, d+201, INF);
    pq.push({0, start});
    d[start] = 0;
    while(!pq.empty()){
        int dist = -pq.top().first;
        int now = pq.top().second;
        pq.pop();

        for (int i = 0; i< vertex[now].size(); i++) {
            int cost = dist + vertex[now][i].second;
            if(cost <d[vertex[now][i].first]) // ๋น„์šฉ์ด ๋” ์ž‘๋‹ค๋ฉด ์ตœ๋‹จ๊ฒฝ๋กœ ํ…Œ์ด๋ธ” ๊ฐ’์„ ๊ฐฑ์‹ .
            {
                d[vertex[now][i].first] = cost;
                pq.push(make_pair(-cost, vertex[now][i].first));
            }

        }
    }


    return d[end];
}

int main(){
    // n - ์ง€์ ์˜ ๊ฐœ์ˆ˜, s - ์‹œ์ž‘, a - a ๋„์ฐฉ , b - b ๋„์ฐฉ
    int n = 6; int s =4; int a = 5; int b = 6;
//    vector<vector<int>> fares = {{4, 1, 10}, {3, 5, 24}, {5, 6, 2}, {3, 1, 41}, {5, 1, 24}, {4, 6, 50}, {2, 4, 66}, {2, 3, 22}, {1, 6, 25}};

//    vector<vector<int>> fares = {{5, 7, 9}, {4, 6, 4}, {3, 6, 1}, {3, 2, 3}, {2, 1, 6}};

    vector<vector<int>> fares = {{2, 6, 6}, {6, 3, 7}, {4, 6, 7}, {6, 5, 11}, {2, 5, 12}, {5, 3, 20}, {2, 4, 8}, {4, 3, 9}};

    for(int i = 0; i < fares.size(); i++){//9
        vertex[fares[i][0]].push_back({fares[i][1], fares[i][2]});
        vertex[fares[i][1]].push_back({fares[i][0], fares[i][2]});
    }

    //๋”ฐ๋กœ ๊ฐ”์„๋•Œ์˜ ๋น„์šฉ
    int cost =  dijkstra(s, a) + dijkstra(s, b);
    //์ค‘๊ฐ„ ์ง€์  ๊ฑฐ์ณ ๊ฐ”์„๋•Œ์˜ ๋น„์šฉ
    for(int i = 1; i<= n; i++){
        if (i != s){//4๊ฐ€ ์•„๋‹๋•Œ
            if(dijkstra(i, a) == 1000000000 || dijkstra(i, b) == 1000000000) continue; //๋–จ์–ด์ ธ ์žˆ์œผ๋ฏ€๋กœ ๋ชป๊ฐ

            cost = min(cost, dijkstra(i, a) + dijkstra(i, b) + dijkstra(s, i));
        }
    }
    cout << cost << '\n';

    return 0;
}

 

2๊ฐœ์˜ ์‹œ๊ฐ„ ์ดˆ๊ณผ ์™ธ์˜ ์ง„์ „๋œ ์ฝ”๋“œ ๐Ÿ†—๐Ÿ™†๐Ÿป

 

๊ฐœ์„ ํ•œ ์‚ฌํ•ญ : ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•  ๋•Œ ๋„์ฐฉ์ง€๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” int ํ˜• ํ•จ์ˆ˜์—์„œ void ํ•จ์ˆ˜๋กœ ๋ฐ”๊พธ์—ˆ๊ณ  main ํ•จ์ˆ˜ ๋‚ด์— ํ•„์š”ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ตฌํ•˜๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์—ฌ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์—†์ด ๋ฐฐ์—ด์˜ ๊ฐ’ ์ฐธ์กฐ ํ˜•์‹์œผ๋กœ ๋ฐ”๊พธ์—ˆ๋‹ค.

//
//  [KAKAO] 2021_sharedTaxi.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/04/28.
//

#include <stdio.h>
#include <iostream>
#include <queue>
#include <algorithm>

#define INF 1e9

using namespace std;
priority_queue<pair<int, int>> pq;
vector<pair<int, int>> vertex[201];


void dijkstra(int d[], int start){
    fill(d, d+201, INF);
    pq.push({0, start});
    d[start] = 0;
    while(!pq.empty()){
        int dist = -pq.top().first;
        int now = pq.top().second;
        pq.pop();
   
        for (int i = 0; i< vertex[now].size(); i++) {
            int cost = dist + vertex[now][i].second;
            if(cost <d[vertex[now][i].first]) // ๋น„์šฉ์ด ๋” ์ž‘๋‹ค๋ฉด ์ตœ๋‹จ๊ฒฝ๋กœ ํ…Œ์ด๋ธ” ๊ฐ’์„ ๊ฐฑ์‹ .
            {
                d[vertex[now][i].first] = cost;
                pq.push(make_pair(-cost, vertex[now][i].first));
            }

        }
    }

}

int main(){
    // n - ์ง€์ ์˜ ๊ฐœ์ˆ˜, s - ์‹œ์ž‘, a - a ๋„์ฐฉ , b - b ๋„์ฐฉ
    int n = 6; int s =4; int a = 5; int b = 6;
//    vector<vector<int>> fares = {{4, 1, 10}, {3, 5, 24}, {5, 6, 2}, {3, 1, 41}, {5, 1, 24}, {4, 6, 50}, {2, 4, 66}, {2, 3, 22}, {1, 6, 25}};
    
//    vector<vector<int>> fares = {{5, 7, 9}, {4, 6, 4}, {3, 6, 1}, {3, 2, 3}, {2, 1, 6}};
    
    vector<vector<int>> fares = {{2, 6, 6}, {6, 3, 7}, {4, 6, 7}, {6, 5, 11}, {2, 5, 12}, {5, 3, 20}, {2, 4, 8}, {4, 3, 9}};
    
    for(int i = 0; i < fares.size(); i++){//9
        vertex[fares[i][0]].push_back({fares[i][1], fares[i][2]});
        vertex[fares[i][1]].push_back({fares[i][0], fares[i][2]});
    }
    
    // - ์ถ”๊ฐ€ํ•œ ์‚ฌํ•ญ -
    int sd[201]; //๊ฑฐ๋ฆฌ์˜ ํ•ฉ
    int id[201]; //๊ฑฐ๋ฆฌ์˜ ํ•ฉ
    dijkstra(sd, s);
    //๋”ฐ๋กœ ๊ฐ”์„๋•Œ์˜ ๋น„์šฉ
    int cost =  sd[a] + sd[b];
    //์ค‘๊ฐ„ ์ง€์  ๊ฑฐ์ณ ๊ฐ”์„๋•Œ์˜ ๋น„์šฉ
    for(int i = 1; i<= n; i++){
    // (์ด ๋ถ€๋ถ„์—์„œ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š” ๋“ฏ ํ•˜๋‹ค)
        dijkstra(id, i);
        if (i != s){//4๊ฐ€ ์•„๋‹๋•Œ
            if(id[a] == 1000000000 || id[b] == 1000000000 ) continue; //๋–จ์–ด์ ธ ์žˆ์œผ๋ฏ€๋กœ ๋ชป๊ฐ
            
            cost = min(cost, id[a] + id[b] + sd[i]);
        }
    }
    cout << cost << '\n';

    return 0;
}

 

๊ฒฐ๊ณผ :

 

๋‘๊ฐœ๊ฐ€ ๋ง˜์— ๊ฑธ๋ฆฌ์ง€๋งŒ .. ๋‹ค์Œ์— ์™€์„œ ๋” ํšจ์œจ์ ์ธ ์ฝ”๋“œ๋กœ ํ’€๊ธฐ๋กœ ๐Ÿฅฒ

๋Œ“๊ธ€