forked from SSAFY-09-D7/term1-group4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Baek22865.java
95 lines (79 loc) · 2.11 KB
/
Baek22865.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
package baek;
import java.io.*;
import java.util.*;
public class Baek22865 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;
static class Point implements Comparable<Point>{
int e,w;
public Point(int e, int w) {
super();
this.e = e;
this.w = w;
}
@Override
public int compareTo(Point o) {
// TODO Auto-generated method stub
return this.w-o.w;
}
}
static int N,M;
static ArrayList<Integer> people=new ArrayList<Integer>();
static ArrayList<Point>[]array;
static int[] dist;
static int[] sum;
public static void main(String[] args) throws IOException {
N=Integer.parseInt(br.readLine());
st=new StringTokenizer(br.readLine());
for(int i=0;i<3;i++) {
people.add(Integer.parseInt(st.nextToken())-1);
}
M=Integer.parseInt(br.readLine());
array=new ArrayList[N];
for(int i=0;i<N;i++) {
array[i]=new ArrayList<>();
}
for(int i=0;i<M;i++) {
st=new StringTokenizer(br.readLine());
int s=Integer.parseInt(st.nextToken())-1;
int e=Integer.parseInt(st.nextToken())-1;
int w=Integer.parseInt(st.nextToken());
array[s].add(new Point(e, w));
array[e].add(new Point(s, w));
}
dist=new int[N];
sum=new int[N];
Arrays.fill(sum,Integer.MAX_VALUE);
PriorityQueue<Point> q=new PriorityQueue<>();
for(int man:people) {
Arrays.fill(dist, Integer.MAX_VALUE);
dist[man]=0;
q.add(new Point(man,0));
while(!q.isEmpty()) {
Point p=q.poll();
if(dist[p.e]<p.w) continue;
for(Point np:array[p.e]) {
int nw=dist[p.e]+np.w;
if(nw<dist[np.e]) {
dist[np.e]=nw;
q.add(new Point(np.e, nw));
}
}
}
for(int i=0;i<N;i++) {
sum[i]=Math.min(sum[i], dist[i]);
}
}
int max=0;
int idx=0;
for(int i=0;i<N;i++) {
if(people.contains(i)||sum[i]==Integer.MAX_VALUE) continue;
if(max<sum[i]) {
max=sum[i];
idx=i;
}
}
System.out.println(idx+1);
}
}