[BOJ] 24888 : ๋ ธํธ ์กฐ๊ฐ
๋ฌธ์ ๋งํฌ
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 |
๋ช๋ช ๋ฌธ์ ๋ค์ ์ถฉ๋ถํ ์๊ฐ์ ๊ฐ๊ณ ํ๋ฉด ํ ์ ์๋ ๋ฌธ์ ๋ค์ธ๋ฐ ๋ํ๋๋ ์๋ฐ๊ฐ๋๋ฌธ์ธ์ง ๋ชปํ์๋ค. ์๊ฐ๋ด์ ๋ฌธ์ ๋ฅผ ํธ๋ ์ฐ์ต์ ํด์ผํ ๊ฒ๊ฐ๋ค. ใ ใ ใ !