20250731 baekjoon
This commit is contained in:
parent
599c65fc89
commit
8b2a92e8f3
57
code_study/Baekjoon/c/1389.c
Normal file
57
code_study/Baekjoon/c/1389.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int BFS(int person, int (*linked)[101], int N);
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int linked[101][101] = {0,};
|
||||||
|
int N,M;
|
||||||
|
scanf("%d %d",&N, &M);
|
||||||
|
for(int i=0; i<M; i++){
|
||||||
|
int a, b;
|
||||||
|
scanf("%d %d",&a, &b);
|
||||||
|
linked[a][b] = 1;
|
||||||
|
linked[b][a] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int min_kevin_bacon_num = 1000000;
|
||||||
|
int result_person;
|
||||||
|
|
||||||
|
for(int i=1; i<=N; i++){
|
||||||
|
int current_kevin_bacon_num = BFS(i, linked, N);
|
||||||
|
if(current_kevin_bacon_num < min_kevin_bacon_num) {
|
||||||
|
min_kevin_bacon_num = current_kevin_bacon_num;
|
||||||
|
result_person = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%d\n",result_person);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int BFS(int person, int (*linked)[101], int N){
|
||||||
|
int front = 0, rear = 0;
|
||||||
|
int queue[101];
|
||||||
|
|
||||||
|
int distance[101] = {0,};
|
||||||
|
|
||||||
|
queue[rear++] = person;
|
||||||
|
distance[person] = 1;
|
||||||
|
|
||||||
|
while(front<rear){
|
||||||
|
int current_person = queue[front++];
|
||||||
|
for(int i=1; i<=N; i++){
|
||||||
|
if(linked[current_person][i] == 1 && distance[i] == 0){
|
||||||
|
distance[i] = distance[current_person] + 1;
|
||||||
|
queue[rear++] = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int total_distance = 0;
|
||||||
|
for(int i=1; i<=N; i++){
|
||||||
|
total_distance += (distance[i] - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return total_distance;
|
||||||
|
}
|
||||||
50
code_study/Baekjoon/java/_1389.java
Normal file
50
code_study/Baekjoon/java/_1389.java
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class _1389 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String[] input = sc.nextLine().split(" ");
|
||||||
|
int N = Integer.parseInt(input[0]);
|
||||||
|
int M = Integer.parseInt(input[1]);
|
||||||
|
|
||||||
|
int[][] distance = new int[N+1][N+1];
|
||||||
|
for(int i=0; i<M; i++){
|
||||||
|
String[] linked_input = sc.nextLine().split(" ");
|
||||||
|
int a = Integer.parseInt(linked_input[0]);
|
||||||
|
int b = Integer.parseInt(linked_input[1]);
|
||||||
|
distance[a][b] = 1;
|
||||||
|
distance[b][a] = 1;
|
||||||
|
}
|
||||||
|
sc.close();
|
||||||
|
|
||||||
|
int maxValue = 1000000;
|
||||||
|
for(int i=1; i<=N; i++){
|
||||||
|
for(int j=1; j<=N; j++){
|
||||||
|
if(i==j) distance[i][j] = 0;
|
||||||
|
else if(distance[i][j] == 0) distance[i][j] = maxValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int k=1; k<=N; k++){
|
||||||
|
for(int i=1; i<=N; i++){
|
||||||
|
for(int j=1; j<=N; j++){
|
||||||
|
distance[i][j] = Math.min(distance[i][j], distance[i][k] + distance[k][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int[] result = new int[N+1];
|
||||||
|
int minDistance = maxValue;
|
||||||
|
int result_person = 0;
|
||||||
|
for(int i=1; i<=N; i++){
|
||||||
|
for (int j = 1; j <= N; j++) {
|
||||||
|
result[i] += distance[i][j];
|
||||||
|
}
|
||||||
|
if(minDistance>result[i]){
|
||||||
|
result_person = i;
|
||||||
|
minDistance = result[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(result_person);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user