ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

11265๋ฒˆ: ๋๋‚˜์ง€ ์•Š๋Š” ํŒŒํ‹ฐ

์ž…๋ ฅ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ํŒŒํ‹ฐ์žฅ์˜ ํฌ๊ธฐ N(5 ≤ N ≤ 500)๊ณผ ์„œ๋น„์Šค๋ฅผ ์š”์ฒญํ•œ ์†๋‹˜์˜ ์ˆ˜ M(1 ≤ M ≤ 10,000) ์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ๊ฐ์˜ ํŒŒํ‹ฐ์žฅ์€ 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์—ฌ์ ธ ์žˆ๋‹ค. ๋‹ค์Œ์—๋Š” N๊ฐœ์˜ ์ค„์— ๊ฑธ

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/11265
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m int
	fmt.Fscanln(reader, &n, &m)

	graph := make([][]int, n)
	for i := 0; i < n; i++ {
		graph[i] = make([]int, n)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			fmt.Fscanf(reader, "%d ", &graph[i][j])
		}
	}

	graph = floydWarshall(graph)

	for i := 0; i < m; i++ {
		var a, b, c int
		fmt.Fscanln(reader, &a, &b, &c)
		if graph[a-1][b-1] <= c {
			fmt.Fprintln(writer, "Enjoy other party")
		} else {
			fmt.Fprintln(writer, "Stay here")
		}
	}
}

func floydWarshall(graph [][]int) [][]int {
	for i := 0; i < len(graph); i++ { // j->k ๊ฒฝ๋กœ๋ณด๋‹ค j->i->k ๊ฒฝ๋กœ๊ฐ€ ๋น ๋ฅธ ๊ฒฝ์šฐ ๊ฐฑ์‹ 
		for j := 0; j < len(graph); j++ {
			for k := 0; k < len(graph); k++ {
				if graph[j][i]+graph[i][k] < graph[j][k] {
					graph[j][k] = graph[j][i] + graph[i][k]
				}
			}
		}
	}
	return graph
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/11265
import sys

def floyd_warshall(graph):
    for i in range(len(graph)):
        for j in range(len(graph)):
            for k in range(len(graph)):
                if graph[j][i] + graph[i][k] < graph[j][k]:
                    graph[j][k] = graph[j][i] + graph[i][k]
    return graph

if __name__ == "__main__":
    n, m = list(map(int, sys.stdin.readline().split()))
    graph = []
    for i in range(n):
        graph.append(list(map(int, sys.stdin.readline().split())))

    graph = floyd_warshall(graph)

    for i in range(m):
        a, b, c = list(map(int, sys.stdin.readline().split()))
        if graph[a-1][b-1] <= c:
            print("Enjoy other party")
        else:
            print("Stay here")
728x90
๋Œ“๊ธ€