흙금이네 블로그

[BOJ] 11265 - 끝나지 않는 파티 (Python, JavaScript) 본문

알고리즘

[BOJ] 11265 - 끝나지 않는 파티 (Python, JavaScript)

흙금 2023. 5. 6. 20:48

 

 

아이디어

 

플로이드-워셜 알고리즘으로 모든 파티장 간의 이동 시간을 구한다.

 

 

풀이 #1 (Python)

 

import sys

input = sys.stdin.readline

def solution():
    N, M = map(int, input().split())
    graph = []
    for _ in range(N):
        graph.append(list(map(int, input().split())))
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if graph[i][k]+graph[k][j] < graph[i][j]:
                    graph[i][j] = graph[i][k]+graph[k][j]
    for _ in range(M):
        A, B, C = map(int, input().split())
        if graph[A-1][B-1] <= C:
            print('Enjoy other party')
        else:
            print('Stay here')

solution()

 

 

 

풀이 #2 (JavaScript)

 

const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().split('\n');

function solution() {
    const [N, M] = input[0].split(' ').map(Number);
    let graph = [];
    for (let i=1; i<=N; i++) {
        graph.push(input[i].split(' ').map(Number));
    }
    for (let k=0; k<N; k++) {
        for (let i=0; i<N; i++) {
            for (let j=0; j<N; j++) {
                if (graph[i][k]+graph[k][j] < graph[i][j]) {
                    graph[i][j] = graph[i][k]+graph[k][j];
                }
            }
        }
    }
    for (let i=N+1; i<=N+M; i++) {
        const [A, B, C] = input[i].split(' ').map(Number);
        if (graph[A-1][B-1] <= C) {
            console.log('Enjoy other party');
        }
        else {
            console.log('Stay here');
        }
    }
}

solution();

 

Comments