본문 바로가기
PS 문제 풀이/Baekjoon

[BOJ] 백준 2660 회장뽑기

by 까망 하르방 2021. 4. 3.
반응형

출처https://www.acmicpc.net/problem/2660

Approach

플로이드 와샬 (Floyd Warshall)을 이용할 수 있다.

 

플로이드 와샬 (Floyd Warshall)

개념 아래와 같은 가중 그래프에서 최단 경로를 찾는 알고리즘. ex) 정점 ③ → ⑤로 가는 최단 경로는 다음과 같습니다.    경로Path: ③ → ② → ④ → ① → ⑤ 비용Cost:  +4 +1 +2  -4 = 3..

zoosso.tistory.com


#include <stdio.h>
const int MAX_N = 55;
int N, s, e, ans = MAX_N;
int dist[MAX_N][MAX_N], score[MAX_N];
int arr[MAX_N], acnt;

void input() {
    scanf("%d", &N);
    for (int i = 1; i <= N; ++i) {
        for (int j = 1; j <= N; ++j) {
            dist[i][j] = MAX_N;
        }
    }

    while (1) {
        scanf("%d %d", &s, &e);
        if (s < 0) break;
        dist[s][e] = dist[e][s] = 1;
    }
}

void Floyd() {
    int i, j, k;
    for (k = 1; k <= N; ++k) {
        for (i = 1; i <= N; ++i) {
            for (j = 1; j <= N; ++j) {
                if (dist[i][j] > dist[i][k] + dist[k][j]) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }
}

void process() {
    int i, j;
    for (i = 1; i <= N; ++i) {
        for (j = 1; j <= N; ++j) {
            if (i != j) {
                if (score[i] < dist[i][j]) {
                    score[i] = dist[i][j];
                }
            }
        }
        if (ans > score[i]) ans = score[i];
    }
}

void output() {
    for (int i = 1; i <= N; ++i) {
        if (score[i] == ans) {
            arr[acnt++] = i;
        }
    }
    printf("%d %d\n", ans, acnt);
    for (int i = 0; i < acnt; ++i) {
        printf("%d ", arr[i]);
    }
    puts("");
}

int main() {
    // freopen("input.txt", "r", stdin);
    input();
    Floyd();
    process();
    output();
}

 

반응형

'PS 문제 풀이 > Baekjoon' 카테고리의 다른 글

[BOJ] 백준 2439 별 찍기 - 2  (0) 2021.04.18
[BOJ] 백준 2438 별 찍기 - 1  (0) 2021.04.17
[BOJ] 백준 2463 비용  (0) 2021.04.03
[BOJ] 백준 2529 부등호  (0) 2021.04.01
[BOJ] 백준 2665 미로만들기  (0) 2021.03.31

댓글