-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
102 lines (85 loc) · 3.03 KB
/
Main.java
File metadata and controls
102 lines (85 loc) · 3.03 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
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
static int N;
static int start;
static int end;
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());
int M = Integer.parseInt(st.nextToken());
HashMap<Integer, ArrayList<Bridge>> map = new HashMap<Integer, ArrayList<Bridge>>();
int minWeight = 1000000000;
int maxWeight = 1;
for(int i=0; i<M; i++){
st = new StringTokenizer(br.readLine());
int island1 = Integer.parseInt(st.nextToken());
int island2 = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
if(map.containsKey(island1)){
map.get(island1).add(new Bridge(island2, weight));
} else {
ArrayList<Bridge> tmp = new ArrayList<Bridge>();
tmp.add(new Bridge(island2, weight));
map.put(island1, tmp);
}
if(map.containsKey(island2)){
map.get(island2).add(new Bridge(island1, weight));
} else {
ArrayList<Bridge> tmp = new ArrayList<Bridge>();
tmp.add(new Bridge(island1, weight));
map.put(island2, tmp);
}
minWeight = Math.min(minWeight, weight);
maxWeight = Math.max(maxWeight, weight);
}
st = new StringTokenizer(br.readLine());
start = Integer.parseInt(st.nextToken());
end = Integer.parseInt(st.nextToken());
int result = minWeight;
while(minWeight <= maxWeight){
int midWeight = (minWeight+maxWeight)/2;
if(bfs(midWeight, map)){
result = midWeight;
minWeight = midWeight+1;
} else {
maxWeight = midWeight-1;
}
}
System.out.println(result);
br.close();
}
public static boolean bfs(int midWeight, HashMap<Integer, ArrayList<Bridge>> map){
Queue<Integer> q = new LinkedList<Integer>();
Boolean[] visited = new Boolean[N+1];
Arrays.fill(visited, Boolean.FALSE);
q.add(start);
visited[start] = true;
while(!q.isEmpty()){
int island = q.poll();
for(Bridge adj : map.get(island)){
if(!visited[adj.to] && adj.weight >= midWeight){
visited[adj.to] = true;
q.add(adj.to);
}
}
}
return visited[end];
}
}
class Bridge {
int to;
int weight;
public Bridge (int to, int weight){
this.to = to;
this.weight = weight;
}
}