-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathicpc1504.cpp
More file actions
55 lines (51 loc) · 1.42 KB
/
icpc1504.cpp
File metadata and controls
55 lines (51 loc) · 1.42 KB
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
#include <stdio.h>
#define MAX_V 810
int min(int x, int y){
return (x < y) ? x : y;
}
void dijkstra(int n, int s, int graph[][MAX_V], int dist[]){
const int INF = 2400000;
int now = s;
int visited[MAX_V] = { 0, };
dist[s] = 0;
while(now){
for(int next = 1; next <= n; next++)
dist[next] = min(dist[next], dist[now] + graph[now][next]);
visited[now] = 1;
int d = INF;
now = 0;
for(int i = 1; i <= n; i++)
if(!visited[i] && dist[i] < d){
now = i;
d = dist[i];
}
}
}
int main(){
const int INF = 2400000;
int n, m;
int p, q;
int graph[MAX_V][MAX_V];
int distFrom1[MAX_V], distFromN[MAX_V], distpq[MAX_V];
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++){
distFrom1[i] = distFromN[i] = distpq[i] = INF;
for(int j = 1; j <= n; j++)
graph[i][j] = INF;
}
for(int i = 0; i < m; i++){
int u, v, d;
scanf("%d %d %d", &u, &v, &d);
graph[u][v] = graph[v][u] = min(graph[u][v], d);
}
scanf("%d %d", &p, &q);
dijkstra(n, 1, graph, distFrom1);
dijkstra(n, p, graph, distpq);
dijkstra(n, n, graph, distFromN);
int Answer = distFrom1[p] + distpq[q] + distFromN[q];
Answer = min(distFrom1[q] + distpq[q] + distFromN[p], Answer);
if(Answer > INF)
Answer = -1;
printf("%d", Answer);
return 0;
}