๋ฐฑ์ค€/Gold

[BOJ] 24888 : ๋…ธํŠธ ์กฐ๊ฐ

Jaeguk 2022. 3. 31. 11:46
๋ฌธ์ œ ๋งํฌ

24888๋ฒˆ: ๋…ธํŠธ ์กฐ๊ฐ (acmicpc.net)

 

24888๋ฒˆ: ๋…ธํŠธ ์กฐ๊ฐ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ $N$, ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ $M$์ด ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ž…๋ ฅ๋œ๋‹ค. ($2\leq N \leq 200\,000,\ N-1 \leq M \leq 200\,000$) ๋‹ค์Œ $M$๊ฐœ์˜ ์ค„์— $i$๋ฒˆ์งธ ๊ฐ„์„ ์ด ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ์ •์  $u_i, v_i$์™€ ๊ฐ„์„ ์˜ ๊ธธ์ด $w_

www.acmicpc.net

์ˆญ๊ณ ํ•œ Division2 F๋ฒˆ ๋ฌธ์ œ.

ํ’€์ด

jhnah917์€ ๋ฐ˜๋“œ์‹œ ์ตœ๋‹จ๊ฒฝ๋กœ๋กœ ๊ฐ€๊ธฐ๋•Œ๋ฌธ์— ์ฐธ๊ฐ€์ž lobo_prix๊ฐ€ ์šฐ์Šนํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ณต๋™์šฐ์Šน์„ ํ•˜๋Š” ์ˆ˜๋ฐ–์— ์—†๋‹ค.

์ฆ‰, ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ ์ค‘์— ๋ชจ๋“  ๋…ธํŠธ์กฐ๊ฐ์„ ์ค๊ณ  ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ.

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

dist๋ผ๋Š” ๋ฐฐ์—ด์„ pair๋กœ ์„ ์–ธํ•ด์„œ {๋น„์šฉ, ๋…ธํŠธ์กฐ๊ฐ ์ˆ˜} ์ด๋ ‡๊ฒŒ ์ €์žฅํ•ด๋‘์—ˆ๋‹ค. ์ดˆ๊ธฐ๊ฐ’์€ {INF, 0}์œผ๋กœ ์ดˆ๊ธฐํ™” ์‹œ์ผœ๋†“์€ ์ƒํƒœ์ด๋‹ค.

if (dist[now].first < cost)
		continue;
dist[now].first = cost;
if (dist[now].second <= cnt) {
	dist[now].second = cnt;
	Before[now] = before;
}
else continue;

์ด ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ๊ธฐ์กด์˜ ๊ฒฝ๋กœ์™€ ๋น„๊ตํ•ด ์œ ํšจํ•œ ๊ฒฝ๋กœ์ธ์ง€ ํŒ๋‹จํ•ด์ฃผ์—ˆ๋‹ค. ๋‹น์—ฐํžˆ ํ•ด๋‹น ์ •์ ์„ ์ฒ˜์Œ ๋ฐฉ๋ฌธํ•œ ๊ฒƒ์ด๋ผ๋ฉด ์œ ํšจํ•œ ๊ฒฝ๋กœ๊ฐ€ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  Before ๋ฐฐ์—ด์„ ์ด์šฉํ•ด์„œ ๋ฐฉ๋ฌธํ•œ ์ง€์ ์˜ ์ˆœ์„œ๋ฅผ ์—ญ์ถ”์ ํ–ˆ๋‹ค.

์ฒ˜์Œ์—๋Š” ๊ธฐ์กด์˜ ๊ฒฝ๋กœ์™€ ๋…ธํŠธ์กฐ๊ฐ์˜ ์ˆ˜๊ฐ€ ๊ฐ™์•„๋„ continue๋ฅผ ํ†ตํ•ด ์Šคํ‚ตํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜์ง€ ๋ชปํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋…ธํŠธ์กฐ๊ฐ์˜ ์ˆ˜๊ฐ€ ๊ฐ™์•„๋„ ์œ ํšจํ•œ ๊ฒฝ๋กœ๋ผ๊ณ  ํŒ๋‹จํ•˜๊ณ  ํƒ์ƒ‰์„ ์ด์–ด๊ฐ„๋‹ค.

์ด๋ ‡๊ฒŒ ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งˆ์นœ ํ›„ N ์ •์ ๊นŒ์ง€ ์™”์„ ๋•Œ ์ฃผ์šธ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋…ธํŠธ์กฐ๊ฐ์˜ ์ˆ˜๊ฐ€ ๋ชจ๋“  ๋…ธํŠธ์กฐ๊ฐ์˜ ์ˆ˜์™€ ๊ฐ™์œผ๋ฉด ์ฐธ๊ฐ€์ž๊ฐ€ ์šฐ์Šนํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ๋‹ค.

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
50
51
52
53
54
55
56
57
58
59
#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 1000000007
using namespace std;
typedef long long ll;
typedef priority_queue<pair<pair<ll,int>pair<int,int>>vector<pair<pair<ll,int>pair<int,int>>>, greater<pair<pair<ll,int>pair<int,int>>>> Priority_queue;
const long long INF = LLONG_MAX;
const int MAX_N = 200000 + 5;
int N, M;
vector<pair<ll,int>> dist;
vector<pair<int, ll >> Edge[MAX_N];
int Before[MAX_N];
int Note[MAX_N];
 
void dijkstra(int st, int ed);
void Init();
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M;
    dist.resize(N + 1);
    for (int i = 0; i < M; i++) {
        int a, b;
        ll w;
        cin >> a >> b >> w;
        Edge[a].push_back({ b,w });
        Edge[b].push_back({ a,w });
    }
    int cnt_Note = 0;
    for (int i = 1; i <= N; i++) {
        cin >> Note[i];
        if (Note[i])
            cnt_Note++;
    }
    Init();
    dijkstra(1, N);
 
    if (dist[N].second == cnt_Note) {
        vector<int> track;
        int idx = N;
        while (idx != -1) {
            track.push_back(idx);
            idx = Before[idx];
        }
        cout << track.size() << "\n";
        for (int i = track.size() - 1; i >= 0; i--)
            cout << track[i] << " ";
        cout << "\n";
    }
    else cout << "-1\n";
    return 0;
}
 
void Init() {
    for (int i = 0; i <= N; i++) {
        dist[i].first = INF; //๊ฑฐ๋ฆฌ
        dist[i].second = 0//๋…ธํŠธ ์กฐ๊ฐ
    }
}
 
void dijkstra(int st, int ed) {
    Priority_queue pq;
    if (Note[st])
        pq.push({ {0,st},{1,-1} });
    else pq.push({ {0,st},{0,-1} });
 
    while (!pq.empty()) {
        int now = pq.top().first.second;
        ll cost = pq.top().first.first;
        int cnt = pq.top().second.first;
        int before = pq.top().second.second;
        pq.pop();
 
        if (dist[now].first < cost)
            continue;
        dist[now].first = cost;
        if (dist[now].second <= cnt) {
            dist[now].second = cnt;
            Before[now] = before;
        }
        else continue;
        for (int i = 0; i < Edge[now].size(); i++) {
            int next = Edge[now][i].first;
            ll acost = cost + Edge[now][i].second;
            int next_cnt = cnt;
            if (Note[next])
                next_cnt++;
            if (dist[next].first < acost)
                continue;
            if (dist[next].second > next_cnt)
                continue;
            pq.push({ {acost,next},{next_cnt, now} });
        }
    }
}
 
cs

๋ช‡๋ช‡ ๋ฌธ์ œ๋“ค์€ ์ถฉ๋ถ„ํžˆ ์‹œ๊ฐ„์„ ๊ฐ–๊ณ  ํ’€๋ฉด ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋“ค์ธ๋ฐ ๋Œ€ํšŒ๋•Œ๋Š” ์••๋ฐ•๊ฐ๋•Œ๋ฌธ์ธ์ง€ ๋ชปํ’€์—ˆ๋‹ค. ์‹œ๊ฐ„๋‚ด์— ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ์—ฐ์Šต์„ ํ•ด์•ผํ•  ๊ฒƒ๊ฐ™๋‹ค. ใ…ใ…‡ใ…Œ!

728x90