๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Gold

[BOJ] 5901 : Relocation

by Jaeguk 2022. 4. 25.
๋ฌธ์ œ ๋งํฌ

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

 

5901๋ฒˆ: Relocation

Input Details There are 5 towns, with towns 1, 2, and 3 having markets. There are 6 roads. Output Details FJ builds his farm in town 5. His daily schedule takes him through towns 5-1-2-3-2-1-5, for a total distance of 12.

www.acmicpc.net

๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฌธ์ œ

๋ฌธ์ œ ํ•ด์„

๋ฌธ์ œ๋ฅผ ๊ทธ๋Œ€๋กœ ํ•ด์„ํ•œ๊ฒŒ ์•„๋‹ˆ๋ผ ํ•ด์„ํ•ด์„œ ์ •๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค.

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

ํ’€์ด

๋งˆ์ผ“์ด 5๊ฐœ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ ์ž„์˜์˜ ๋†์žฅ Farm์— ๋Œ€ํ•ด์„œ ์กด์˜ ์ผ๊ณผ ์ง„ํ–‰ ๊ฒฝ๋กœ๋Š” ์™ผ์ชฝ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค. ์กด์ด ๊ฐ€์žฅ ์งง์€ ๊ฑฐ๋ฆฌ๋กœ ์ผ๊ณผ๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฐ ๋…ธ๋“œ ์‚ฌ์ด๋ฅผ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋กœ ์ง„ํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋งˆํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š” ์ˆœ์„œ๋Š” ์ƒ๊ด€์ด ์—†๋‹ค๊ณ  ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์–ด๋–ค ๊ฒฝ๋กœ๊ฐ€ ์ตœ์ ์˜ ๊ฒฝ๋กœ์ผ์ง€ ๋ชจ๋ฅด๋‹ˆ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋‹ค ๋Œ์•„๋ด์•ผํ•œ๋‹ค. next_permutation(์ˆœ์—ด)์„ ์ด์šฉํ•ด์„œ ๋งˆํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š” ์ˆœ์„œ๋ฅผ ์ •ํ•ด์ฃผ์—ˆ๋‹ค. ์ฆ‰ KํŒฉํ† ๋ฆฌ์–ผ ๊ฐœ์˜ ๊ฒฝ์šฐ๋ฅผ ๋‹ค ๋Œ์•„๋ณด์•˜๋‹ค. ๋งˆํŠธ๊ฐ€ ์ตœ๋Œ€ 5๊ฐœ๋ผ๊ณ  ํ–ˆ์œผ๋ฏ€๋กœ ์ตœ๋Œ€ 5! = 120๊ฐ€์ง€์˜ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

 

์ž„์˜์˜ ๋†์žฅ Farm์— ๋Œ€ํ•ด์„œ ์ผ๊ณผ์‹œ๊ฐ„ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ ๋งˆํŠธ ์‚ฌ์ด์˜ ์ตœ๋‹จ๊ฒฝ๋กœ์™€ ์ž„์˜์˜ ๋งˆํŠธ์™€ ๋†์žฅ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์•Œ์•„์•ผํ•œ๋‹ค.

์ฒ˜์Œ์—๋Š” dist๋ผ๋Š” ๋ฐฐ์—ด์„ 2์ฐจ์›์œผ๋กœ ๋งŒ๋“ค์–ด์„œ dist[๋งˆํŠธ๊ฐ€ ์žˆ๋Š” ๋งˆ์„์˜ ๋ฒˆํ˜ธ][์ž„์˜์˜ ๋งˆ์„] ์ด๋ ‡๊ฒŒ ๋งˆํŠธ์—์„œ ๋งˆ์„๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ๊ณ„์‚ฐํ•ด์„œ ์ €์žฅํ•ด๋‘๋ ค๊ณ  ํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์˜ˆ์ œ๋ฌธ์ œ๋Š” ์ž˜ ์ถœ๋ ฅ๋์ง€๋งŒ 10000 X 10000 ๊ฐœ์˜ int ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์•ผํ•ด์„œ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค.

๊ทธ๋ƒฅ ๋งˆํŠธ์˜ ์ธ๋ฑ์Šค๋ฅผ 0,1,2,3,4 ๋กœ ์ •ํ•˜๊ณ  dist[5][MAX_N]์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆํŠธ๊ฐ€ ์žˆ๋Š” ๋งˆ์„์˜ ๋ฒˆํ˜ธ๋Š” Market[5] ๋ฐฐ์—ด์„ ์„ ์–ธํ•ด์„œ ๊ฐ ์ธ๋ฑ์Šค๋งˆ๋‹ค ๋งˆ์„์˜ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅํ•ด๋‘์—ˆ๋‹ค.

๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์ด์šฉํ•ด์„œ ๋งˆํŠธ์™€ ๋งˆํŠธ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ, ์ž„์˜์˜ ๋งˆํŠธ์™€ ์ž„์˜์˜ ๋งˆ์„ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋ชจ๋‘ ๊ตฌํ•ด๋†“์€ ๋‹ค์Œ ์ˆœ์—ด์„ ์ด์šฉํ•ด์„œ ๋งˆํŠธ๋ฐฉ๋ฌธ ์ˆœ์„œ๋ฅผ ์ •ํ•˜๊ณ  ๊ฒฝ๋กœ์˜ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ์˜ ์ตœ์†Œ๋ฅผ ์ฐพ์•„์ฃผ๋ฉด ๋œ๋‹ค. ์ž„์˜์˜ ๋†์žฅ Farm์€ K๊ฐœ์˜ ๋งˆ์„์„ ์ œ์™ธํ•œ ๋ชจ๋“  N-K๊ฐœ์˜ ๋งˆ์„์ด ๋  ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 10000 + 5;
int Market[5];
int N, M, K; // ์ •์ , ๊ฐ„์„ , ๋งˆํŠธ
vector<pair<intint>> Edge[MAX_N];
int dist[5][MAX_N];
bool is_market[MAX_N];
int ans = INF;
 
void get_distance() {
    int order[5= { 0,1,2,3,4 }; // ๋งˆํŠธ์˜ ์ธ๋ฑ์Šค
    do {
        int cost = 0;
        for (int i = 0; i < K - 1; i++) {
            cost += dist[order[i]][Market[order[i + 1]]];
        }
        for (int i = 1; i <= N; i++) {
            if (!is_market[i]) // ์ถœ๋ฐœ์ ์œผ๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด
                ans = min(ans, cost + dist[order[K-1]][i] + dist[order[0]][i]);
        }
    } while (next_permutation(order, order + K));
}
 
void dijkstra(int st) {
    Priority_queue pq;
    pq.push({ 0, Market[st]});
    while (!pq.empty()) {
        int now = pq.top().second;
        int cost = pq.top().first;
        pq.pop();
        if (cost >= ans)
            break;
        if (dist[st][now] != INF)
            continue;
        if(now != Market[st])
            dist[st][now] = cost;
        for (int i = 0; i < Edge[now].size(); i++) {
            int next = Edge[now][i].first;
            int acost = cost + Edge[now][i].second;
            if (dist[st][next] != INF)
                continue;
            pq.push({ acost, next });
        }
    }
}
void Init() {
    for (int i = 0; i <5; i++)
        for (int j = 0; j <= N; j++)
            dist[i][j] = INF;
}
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M >> K;
    Init();
    for (int i = 0; i < K; i++) {
        int market; cin >> market;
        Market[i] = market;
        is_market[market] = true;
    }
    sort(Market, Market + K);
    for (int i = 0; i < M; i++) {
        int a, b, w;
        cin >> a >> b >> w;
        Edge[a].push_back({ b,w });
        Edge[b].push_back({ a,w });
    }
    for (int i = 0; i < K; i++)
        dijkstra(i);
    get_distance();
    cout << ans << "\n";
}
cs

728x90

'๋ฐฑ์ค€ > Gold' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ] 10037 : Decorating the Pastures  (0) 2022.04.27
[BOJ] 13595 : Mania de Par  (0) 2022.04.27
[BOJ] 9874 : Wormholes  (0) 2022.04.22
[BOJ] 1800 : ์ธํ„ฐ๋„ท ์„ค์น˜  (0) 2022.04.20
[BOJ] 16639 : ๊ด„ํ˜ธ ์ถ”๊ฐ€ํ•˜๊ธฐ 3  (0) 2022.04.19