`
다익스트라 방식으로 풀었고 거리계산에서 다음과 같이 처리
dist[next] = max(dist[next], min(dist[cur],cost(cur,next))
우선순위 큐 활용해야 $O(VlogE)$ 에 가능
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
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class B13905 {
static int N,M,S,E;
static class Edge implements Comparable<Edge>{
int end;
int value;
Edge(int end, int value) {
this.end = end;
this.value = value;
}
@Override
public int compareTo(Edge o) {
// 내림차순으로 해야 정확한 비교가 가능함
return o.value - value;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N= Integer.parseInt(st.nextToken());
M= Integer.parseInt(st.nextToken());
ArrayList<Edge> edges[] = new ArrayList[N+1];
int[] dist = new int[N+1];
boolean visit[] = new boolean[N+1];
for(int i = 0;i<=N;i++) {
edges[i] = new ArrayList<Edge>();
}
st = new StringTokenizer(br.readLine());
S= Integer.parseInt(st.nextToken());
E= Integer.parseInt(st.nextToken());
dist[S] = Integer.MAX_VALUE;
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
edges[start].add(new Edge(end,w));
edges[end].add(new Edge(start,w));
}
PriorityQueue <Edge> pq = new PriorityQueue <Edge>();
pq.add(new Edge(S,0));
while(!pq.isEmpty()) {
Edge cur = pq.poll();
if(visit[cur.end]) continue;
visit[cur.end] = true;
// 모든 길을 보고 각 루트의 최소값 구하고 최대값으로 update
for(int i =0 ;i < edges[cur.end].size();i++) {
int next = edges[cur.end].get(i).end;
int cost = edges[cur.end].get(i).value;
dist[next]= Math.max(dist[next], Math.min(dist[cur.end], cost));
pq.add(edges[cur.end].get(i));
}
}
System.out.println(dist[E]);
}
}
|