์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- thymeleaf
- ๋ฐฑํธ๋ํน
- Reversing
- ์์ ์ ๋ ฌ
- ๊ทธ๋ฆฌ๋
- ์ด๋ถ ํ์
- Spring
- ์คํ
- BFS
- ๋ฐ์ดํฌ์คํธ๋ผ
- error
- java
- c++
- GCP
- ๋ถํ ์ ๋ณต
- ๋งต
- web
- JPA
- dfs
- dynamic debugging
- ๊ตฌํ
- ์ต๋จ ๊ฒฝ๋ก
- ๋์ ํฉ
- CVE
- ์๋ฎฌ๋ ์ด์
- DP
- ์ฌ๊ท
- ๋ฌธ์์ด
- OS
- ์ฐ์ ์์ ํ
- Today
- Total
hades
[Baekjoon] 1504๋ฒ: ํน์ ํ ์ต๋จ ๊ฒฝ๋ก ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1504
๐ ์ค๊ณ
1๋ถํฐ N๊น์ง์ ๊ฒฝ๋ก ์ค ์ต๋จ ๊ฒฝ๋ก๋ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๊ตฌํ ์ ์๋ค. ํ์ง๋ง ๋ ์ ์ ๋ฌด์กฐ๊ฑด ์ง๋์ผ ํ๋ค. 1๋ถํฐ ์ด๋ค ์ ์ ๊น์ง ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ตฌํ๊ณ ๊ฐฑ์ ํ๋ ๊ณผ์ ์์ ์๋ฌด๋ฐ ๊ตฌ๋ถ ์์ด ๋น์ฉ ํ๋๋ง์ผ๋ก ๊ฐฑ์ ํ๋ฉด ์๋๋ค. ๋ ์ ์ ์ง๋ฌ๋์ง ์ฌ๋ถ๋ฅผ ์ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค.
์ด๋ค ์ ์ ์ ๋๋ฌํ๊ธฐ๊น์ง ๋ ์ ์ ์ง๋ฌ๋์ง์ ์ฌ๋ถ์ ๋ฐ๋ผ ๋์ค๋ ๊ฒฝ์ฐ์ ์๋ ์๋์ ๊ฐ์ด 4์ด๋ค.
0) v1, v2๋ฅผ ๋ชจ๋ ์ง๋์ง ์์
1) v2๋ง ์ง๋จ
2) v1๋ง ์ง๋จ
3) v1, v2 ๋ชจ๋ ์ง๋จ
๋ฐ๋ผ์ ๋น์ฉ์ ์ ์ฅํ ๋๋ 4๊ฐ์ง ๊ฒฝ์ฐ๋ก ๋๋์ด ์ ์ฅํ๋ค.
โ ์์์ ์ธ 1์ v1์ผ ์๋ ์๊ณ , ์๋ ์๋ ์์ผ๋ฏ๋ก, ๊ฒฝ์ฐ์ ๋ฐ๋ผ ํ์ ๋ฃ๋ ์ด๊น๊ฐ์ ์ ๋ฃ์ด์ผ ํ๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
class Info {
public:
int cost;
int x;
bool v1_ok;
bool v2_ok;
Info(int cost, int x, bool v1_ok, bool v2_ok) {
this->cost = cost;
this->x = x;
this->v1_ok = v1_ok;
this->v2_ok = v2_ok;
}
};
struct compare {
bool operator() (Info a, Info b){
return a.cost > b.cost;
}
};
int n, e, a, b, c, v1, v2;
vector<vector<pair<int, int>>> adj_list(801);
vector<vector<int>> dist(801, vector<int>(4, 1e9 + 7));
priority_queue<Info, vector<Info>, compare> pq;
void dijkstra() {
if (1 == v1) {
pq.push(Info(0, 1, true, false));
dist[1][2] = 0;
}
else {
pq.push(Info(0, 1, false, false));
dist[1][0] = 0;
}
while (!pq.empty()) {
Info cur_info = pq.top();
pq.pop();
int cur_cost = cur_info.cost;
int cur_x = cur_info.x;
bool cur_v1_ok = cur_info.v1_ok;
bool cur_v2_ok = cur_info.v2_ok;
for (int i = 0; i < adj_list[cur_x].size(); i++) {
int next_cost = cur_cost + adj_list[cur_x][i].first;
int next_x = adj_list[cur_x][i].second;
bool next_v1_ok = cur_v1_ok;
bool next_v2_ok = cur_v2_ok;
if (next_x == v1) {
next_v1_ok = true;
}
if (next_x == v2) {
next_v2_ok = true;
}
if (!next_v1_ok && !next_v2_ok && dist[next_x][0] > next_cost) {
dist[next_x][0] = next_cost;
pq.push(Info(next_cost, next_x, next_v1_ok, next_v2_ok));
}
else if (!next_v1_ok && next_v2_ok && dist[next_x][1] > next_cost) {
dist[next_x][1] = next_cost;
pq.push(Info(next_cost, next_x, next_v1_ok, next_v2_ok));
}
else if (next_v1_ok && !next_v2_ok && dist[next_x][2] > next_cost) {
dist[next_x][2] = next_cost;
pq.push(Info(next_cost, next_x, next_v1_ok, next_v2_ok));
}
else if (next_v1_ok && next_v2_ok && dist[next_x][3] > next_cost) {
dist[next_x][3] = next_cost;
pq.push(Info(next_cost, next_x, next_v1_ok, next_v2_ok));
}
}
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> e;
for (int i = 0; i < e; i++) {
cin >> a >> b >> c;
adj_list[a].push_back({ c, b });
adj_list[b].push_back({ c, a });
}
cin >> v1 >> v2;
dijkstra();
if (dist[n][3] == 1e9 + 7) {
cout << -1 << "\n";
}
else {
cout << dist[n][3] << "\n";
}
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1753๋ฒ: ์ต๋จ๊ฒฝ๋ก (0) | 2024.08.05 |
---|---|
[Baekjoon] 1181๋ฒ: ๋จ์ด ์ ๋ ฌ (0) | 2024.08.04 |
[Baekjoon] 1238๋ฒ: ํํฐ (0) | 2024.08.03 |
[Baekjoon] 1629๋ฒ: ๊ณฑ์ (0) | 2024.08.03 |
[Baekjoon] 1167๋ฒ: ํธ๋ฆฌ์ ์ง๋ฆ (0) | 2024.08.02 |