백준공부/java
[백준] 1939번 중량제한 (gold 3
gomduri43
2024. 5. 16. 12:29
import java.io.*;
import java.util.*;
class Point{
int y;
int v;
public Point(int y, int v){
this.y=y;
this.v=v;
}
}
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
int n=Integer.valueOf(st.nextToken());
int m=Integer.valueOf(st.nextToken());
ArrayList<Point>[] map=new ArrayList[n+1];
for(int i=1; i<=n; i++){
map[i]=new ArrayList<>();
}
for(int i=0; i<m; i++){
st=new StringTokenizer(br.readLine());
int x=Integer.valueOf(st.nextToken());
int y=Integer.valueOf(st.nextToken());
int v=Integer.valueOf(st.nextToken());
map[x].add(new Point(y,v));
map[y].add(new Point(x,v));
}
PriorityQueue<Point> que = new PriorityQueue<>(new Comparator<Point>(){
public int compare(Point o1, Point o2){
return -(o1.v-o2.v);
}
});
st=new StringTokenizer(br.readLine());
int start=Integer.valueOf(st.nextToken());
int end=Integer.valueOf(st.nextToken());
que.offer(new Point(start, Integer.MAX_VALUE));
//다익스트라 알고리즘 이용.
//최단경로의 경로 가중치를 이문제에서는 무게로 설정.
//다익스트라 알고리즘에서, 다리를 건너는 상황에 현재 무게와
//건너려는 다리의 무게를 비교해서, 다리의 무게가 더크면 현재 그대로,
//다리의 무게가 더 작으면, 다리의 무게를 넣는 방식으로 진행.
//이렇게 최대의 값을 구하면 답을 구할 수 있다.
int[] visit=new int[n+1];
while(!que.isEmpty()){
Point temp=que.poll();
if(temp.v<=visit[temp.y]){
continue;
}
visit[temp.y]=temp.v;
for(int i=0; i<map[temp.y].size(); i++){
Point p=map[temp.y].get(i);
int value=0;
if(temp.v<=p.v){
value=temp.v;
}
else{
value=p.v;
}
if(visit[p.y]<value){
que.offer(new Point(p.y, value));
}
}
}
System.out.print(visit[end]+"");
}
}