-
Notifications
You must be signed in to change notification settings - Fork 2
/
1504_josueyeon.cpp
118 lines (97 loc) · 2.16 KB
/
1504_josueyeon.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<pair<int, int>> graph[801];
int dist[801];
void dijkstra(int start)
{
priority_queue<pair<int, int>> pq;
// {가중치, 노드}
pq.push({0, start});
dist[start] = 0;
while(!pq.empty())
{
int w = pq.top().first * -1;
int now = pq.top().second;
pq.pop();
if (dist[now] < w)
continue;
for(int i = 0;i < graph[now].size();i++)
{
int cost = w + graph[now][i].second;
if (cost < dist[graph[now][i].first])
{
dist[graph[now][i].first] = cost;
pq.push({cost * -1, graph[now][i].first});
}
}
}
}
int main()
{
// N(node), E(edge)
int N, E;
cin>>N>>E;
for(int i = 0;i < E;i++)
{
int a, b, c;
cin>>a>>b>>c;
// {(a,b)를 잇는 간선 가중치 c}
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
// 반드시 지나가야 하는 정점 2개
int temp1, temp2;
cin>>temp1>>temp2;
// 1->temp1->temp2->N or 1->temp2->temp1->N 두가지 경우 존재
int result = 0;
fill_n(dist, N + 2, 1e9);
// temp1-temp2 거리
dijkstra(temp1);
result += dist[temp2];
if (result >= 1e9)
{
cout<<-1<<"\n";
return 0;
}
// 1-temp1 temp2-N거리
int t1 = 0;
fill_n(dist, N + 2, 1e9);
dijkstra(1);
t1 = dist[temp1];
if (t1 >= 1e9)
{
cout<<-1<<"\n";
return 0;
}
fill_n(dist, N + 2, 1e9);
dijkstra(temp2);
t1 += dist[N];
if (t1 >= 1e9)
{
cout<<-1<<"\n";
return 0;
}
// 1-temp2 temp1-N 거리
int t2 = 0;
fill_n(dist, N + 2, 1e9);
dijkstra(1);
t2 = dist[temp2];
if (t2 >= 1e9)
{
cout<<-1<<"\n";
return 0;
}
fill_n(dist, N + 2, 1e9);
dijkstra(temp1);
t2 += dist[N];
if (t2 >= 1e9)
{
cout<<-1<<"\n";
return 0;
}
result += min(t1, t2);
cout<<result<<"\n";
return 0;
}