/
Main.java
107 lines (90 loc) · 2.98 KB
/
Main.java
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
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args){
Solve solve = new Solve();
solve.run();
}
}
class Pair implements Comparable<Pair>{
public int first, second;
public Pair(int first, int second){
set(first, second);
}
public void set(int first, int second){
this.first = first;
this.second = second;
}
@Override
public int compareTo(Pair pair){
if(this.first > pair.first) return 1;
else if(this.first < pair.first) return -1;
return 0;
}
}
class Solve{
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static int N, P, K;
private ArrayList<ArrayList<Pair> > edge = new ArrayList<ArrayList<Pair> >();
public void run(){
input();
System.out.println(solve());
}
private void input(){
try{
String temp[] = br.readLine().split(" ");
N = Integer.parseInt(temp[0]);
P = Integer.parseInt(temp[1]);
K = Integer.parseInt(temp[2]);
for(int i = 0; i < N+5; i++) edge.add(new ArrayList<Pair>());
for(int i = 0; i < P; i++){
int from, to, weight;
temp = br.readLine().split(" ");
from = Integer.parseInt(temp[0]);
to = Integer.parseInt(temp[1]);
weight = Integer.parseInt(temp[2]);
edge.get(from).add(new Pair(weight, to));
edge.get(to).add(new Pair(weight, from));
}
} catch (IOException ioe){}
}
private int solve(){
int ret = -1;
int low = 1, max = 1000000;
for(int i = 1; i <= 21; i++){
int mid = (low + max) / 2;
if(connect(mid)){
ret = mid;
max = mid - 1;
}
else low = mid + 1;
}
return ret;
}
private boolean connect(int maximumCost){
int[] check = new int[N+5];
fill(check);
check[1] = 0;
PriorityQueue<Pair> pq = new PriorityQueue<Pair>(); // K사용횟수, 현재 노드
pq.add(new Pair(0,1));
while(!pq.isEmpty()){
int restK = pq.peek().first;
int node = pq.peek().second;
pq.poll();
if(check[node] < restK || restK > K) continue;
if(node == N) return true;
for(int i = 0; i < edge.get(node).size(); i++){
int nextNode = edge.get(node).get(i).second;
int nextK = restK;
if(edge.get(node).get(i).first > maximumCost) nextK++;
if(check[nextNode] <= nextK) continue;
check[nextNode] = nextK;
pq.add(new Pair(nextK, nextNode));
}
}
return false;
}
private void fill(int[] target){
for(int i = 0; i <= N+2; i++) target[i] = 987654321;
}
}