-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ1167_0.java
More file actions
67 lines (54 loc) · 1.89 KB
/
Copy pathJ1167_0.java
File metadata and controls
67 lines (54 loc) · 1.89 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
package TIL;
import java.util.*;
import java.io.*;
public class J1167_0 {
static LinkedList<int[]>[] A;
static boolean[] visited;
static int maxNode;
static int maxNum;
static int[] distance;
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
A = new LinkedList[num+1];
distance = new int[num+1];
visited = new boolean[num+1];
for(int i = 0 ; i < num+1; i++){
A[i] = new LinkedList<>();
}
for(int i = 1; i <= num; i++){
int node = scanner.nextInt();
int edge = scanner.nextInt();
while(edge != -1){
int len = scanner.nextInt();
A[node].add(new int[]{edge,len});
edge = scanner.nextInt();
}
}
BFS(1, 0 );
visited = new boolean[num+1];
distance = new int[num+1];
BFS(maxNode, distance[maxNode]);
System.out.println(maxNum);
}
private static void BFS(int node, int len){
visited[node] = true;
Queue<Integer> queue = new LinkedList<>();
queue.add(node);
distance[node] = len;
while(!queue.isEmpty()){
node = queue.poll();
for(int[] i : A[node]){
if(!visited[i[0]]){
visited[i[0]] = true;
distance[i[0]] = distance[node]+i[1];
if(maxNum < distance[i[0]]){
maxNum = distance[i[0]];
maxNode = i[0];
}
queue.add(i[0]);
}
}
}
}
}