본문 바로가기

algorithm

Six Degrees of Kevin Bacon

https://en.wikipedia.org/wiki/Six_Degrees_of_Kevin_Bacon

 

Six Degrees of Kevin Bacon - Wikipedia

From Wikipedia, the free encyclopedia Jump to navigation Jump to search Parlour game on degrees of separation Six Degrees of Kevin Bacon or Bacon's Law is a parlor game where players challenge each other to arbitrarily choose an actor and then connect them

en.wikipedia.org

Its a game  영화 배우 한 명을 골라서 그 사람과 같은 영화를 출연한 다른 배우를 고르기를 반복하여

Kevin Bacon을 고르게 되는 가장 짧은 경로를 구하는

KEvin Bacon 얼마나 많은 영화에 출연했길래ㅎㅎ 최대 6번만에 그와 엮인다는 가정이있다고 한다.

the film The River Wild that "he had worked with everybody in Hollywood or someone who's worked with them."

허걱!! 최대 6이라는 것은 kevin과의 최단 경로의 거리가 maximum 6라는 의미로 해석할 수 있다

초록색 배우들이 bacon # 1 (pic from wikipedia)

https://www.acmicpc.net/problem/1389

 

1389번: 케빈 베이컨의 6단계 법칙

첫째 줄에 유저의 수 N (2 ≤ N ≤ 100)과 친구 관계의 수 M (1 ≤ M ≤ 5,000)이 주어진다. 둘째 줄부터 M개의 줄에는 친구 관계가 주어진다. 친구 관계는 A와 B로 이루어져 있으며, A와 B가 친구라는 뜻

www.acmicpc.net

어떤 사람 i 를 kevin으로 가정하고 다른 모든 사람과의 최단 경로의 합을 케빈 베이컨 수라고 정의하고 있다.

이렇게 다른 모든 사람과의 최단 거리를 구하는 데에 최적인 알고리즘은 플로이드 와샬이며, N이 100이하로 매우 적기 때문에도 그렇게 추론할 수 있다.

#include <iostream>

using namespace std;
int n,m;
int floyd[101][101];
int MAX = 5001;
int ans,sum = 5001;
int main()
{
    cin>>n>>m;
    for(int i = 1; i <= n; i++) for(int j = 1; j<= n ; j++) floyd[i][j] = MAX;
    while(m--){
        int a,b;
        cin>>a>>b;
        floyd[a][b] = floyd[b][a] = 1;
    }
    for(int k=1; k<=n;k++)for(int i = 1; i <= n; i++) for(int j=1; j <= n; j++)  floyd[i][j] = min(floyd[i][j], floyd[i][k] + floyd[k][j]);
    
    for(int i =1; i<= n; i++){
        int tmp = 0;
        for(int j = 1; j<= n; j++){
            if(i==j) continue;
            tmp += floyd[i][j];
        }
        if(tmp < sum){
            sum = tmp;
            ans = i;
        }
    }
    cout<<ans;
    return 0;
}

 

 

 

 

'algorithm' 카테고리의 다른 글

14391 종이조각  (0) 2023.01.10
1018 체스판 다시 칠하기  (0) 2023.01.07
[Codeforces] suffix array  (0) 2022.10.10
[BOJ 9660] 돌 게임 6  (0) 2022.10.08
[BOJ] 2467용액  (0) 2022.08.27