-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1389.java
More file actions
73 lines (60 loc) · 1.27 KB
/
1389.java
File metadata and controls
73 lines (60 loc) · 1.27 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
package algo;
import java.util.Arrays;
import java.util.Scanner;
class Task {
private int[][] friends;
private int N;
private int M;
private final Scanner sc = new Scanner(System.in);
private final int INF = 987654321;
private void input() {
N = sc.nextInt();
M = sc.nextInt();
friends = new int[N+1][N+1];
for(int i=1; i<=N; i++) {
for(int j=1; j<=N; j++) {
if(i==j) friends[i][j] = 0;
else friends[i][j] = INF;
}
}
for(int i=0; i<M; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
friends[from][to] = 1;
friends[to][from] = 1;
}
}
private void floydWarshall() {
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
for(int k=1; k<=N; k++)
if(friends[i][k] + friends[k][j] < friends[i][j])
friends[i][j] = friends[i][k] + friends[k][j];
}
private void print() {
int minVal = INF;
int minNum = 0;
for(int i=1; i<=N; i++) {
int sum = 0;
for(int j=1; j<=N; j++) {
if(friends[i][j] != INF) sum += friends[i][j];
}
if(sum < minVal) {
minVal = sum;
minNum = i;
}
}
System.out.println(minNum);
}
public void run() {
input();
floydWarshall();
print();
}
}
public class Main {
public static void main(String[] args) {
Task task = new Task();
task.run();
}
}