hades

[Baekjoon] 1504๋ฒˆ: ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ณธ๋ฌธ

๐Ÿ‘Š PS/Algorithm

[Baekjoon] 1504๋ฒˆ: ํŠน์ •ํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ

hades1 2024. 8. 3. 19:45

๐Ÿฅ… ๋ฌธ์ œ

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;
}