-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ1176_2.java
More file actions
63 lines (58 loc) · 1.79 KB
/
Copy pathJ1176_2.java
File metadata and controls
63 lines (58 loc) · 1.79 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
package TIL;
import java.util.*;
import java.io.*;
public class J1176_2 {
static LinkedList<int[]>[] A;
static boolean[] visited;
static int node;
static int[] distance;
static int max = 0;
public static void main(String[] args) throws IOException{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
visited = new boolean[num+1];
A = new LinkedList[num+1];
for(int i = 1; i <= num; i++){
A[i] = new LinkedList<>();
}
int cnt = 1;
for(int i = 1; i <= num; i++){
int n = scanner.nextInt();
while(true) {
int e = scanner.nextInt();
if (e == -1) {
break;
}
int dis = scanner.nextInt();
A[n].add(new int[] {e, dis});
}
}
distance = new int[num+1];
BFS(1, 0);
visited = new boolean[num+1];
distance = new int[num+1];
BFS(node, distance[node]);
System.out.println(max);
}
public static void BFS(int n, int dis){
Queue<Integer> queue = new LinkedList<>();
visited[n] = true;
queue.add(n);
distance[n] = dis;
while(!queue.isEmpty()){
int now = queue.poll();
for(int[] i : A[now]){
if(!visited[i[0]]){
visited[i[0]] = true;
distance[i[0]] = i[1]+distance[now];
if(distance[i[0]] > max){
max = distance[i[0]];
node = i[0];
}
queue.add(i[0]);
}
}
}
}
}