BOJ ๋งํฌ



๋ฌธ์ œ

image



์ฝ”๋“œ

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

vector<pair<int, int>> graph[801];
int cost[801];

int dijkstra(int st, int target)
{
	// init
	fill(cost, cost + 801, 1e9);

	priority_queue<pair<int, int>> pq;
	pq.push({ 0, st });
	cost[st] = 0;

	while (!pq.empty())
	{
		int dist = -pq.top().first;
		int now = pq.top().second;
		pq.pop();

		if (cost[now] < dist) continue;

		// implement
		for (int i = 0; i < graph[now].size(); i++)
		{
			int cal = dist + graph[now][i].second;
			int next = graph[now][i].first;

			if (cal < cost[next])
			{
				cost[next] = cal;
				pq.push({ -cal, next });
			}
		}
	}

	if (cost[target] == 1e9)
	{
		return -2e9;
	}
	return cost[target];
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	// input & init
	int n, e;

	cin >> n >> e;

	for (int i = 0; i < e; i++)
	{
		int a, b, c;

		cin >> a >> b >> c;
		graph[a].push_back({ b, c });
		graph[b].push_back({ a, c });

	}

	int v1, v2;
	cin >> v1 >> v2;

	//dijkstra
	int common = dijkstra(v1, v2);
	int route1 = dijkstra(1, v1) + dijkstra(v2, n)  + common;
	int route2 = dijkstra(1, v2) + dijkstra(v1, n) + common;
	
	// output
	if ((route1 < 0 && route2 < 0))
	{
		cout << "-1";
	}
	else if (route1 > route2)
	{
		cout << route2;
	}
	else
	{
		cout << route1;
	}
	
	return 0;
}
}

Leave a comment