結果

問題 No.160 最短経路のうち辞書順最小
ユーザー tnoda_tnoda_
提出日時 2015-04-08 20:54:05
言語 Go1.4
(1.4.2)
結果
AC  
実行時間 62 ms / 5,000 ms
コード長 1,262 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 32,512 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-03 20:12:34
合計ジャッジ時間 2,844 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 56 ms
5,376 KB
testcase_05 AC 58 ms
5,376 KB
testcase_06 AC 61 ms
5,376 KB
testcase_07 AC 53 ms
5,376 KB
testcase_08 AC 53 ms
5,376 KB
testcase_09 AC 56 ms
5,376 KB
testcase_10 AC 54 ms
5,376 KB
testcase_11 AC 52 ms
5,376 KB
testcase_12 AC 52 ms
5,376 KB
testcase_13 AC 53 ms
5,376 KB
testcase_14 AC 54 ms
5,376 KB
testcase_15 AC 52 ms
5,376 KB
testcase_16 AC 52 ms
5,376 KB
testcase_17 AC 53 ms
5,376 KB
testcase_18 AC 53 ms
5,376 KB
testcase_19 AC 55 ms
5,376 KB
testcase_20 AC 54 ms
5,376 KB
testcase_21 AC 52 ms
5,376 KB
testcase_22 AC 53 ms
5,376 KB
testcase_23 AC 53 ms
5,376 KB
testcase_24 AC 53 ms
5,376 KB
testcase_25 AC 53 ms
5,376 KB
testcase_26 AC 52 ms
5,376 KB
testcase_27 AC 52 ms
5,376 KB
testcase_28 AC 62 ms
5,376 KB
testcase_29 AC 52 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)

func next() string {
	sc.Split(bufio.ScanWords)
	if !sc.Scan() {
		panic("could not scan a word from the reader")
	}
	return sc.Text()
}

func nextInt() int {
	i, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return i
}

var inf = 1 << 29

func min(x, y int) int {
	if y < x {
		return y
	}
	return x
}

var d, e [210][210]int

func main() {
	N, M, S, G := nextInt(), nextInt(), nextInt(), nextInt()
	for i := 0; i < N; i++ {
		for j := 0; j < N; j++ {
			if i != j {
				d[i][j] = inf
			}
		}
	}
	for i := 0; i < M; i++ {
		A, B, C := nextInt(), nextInt(), nextInt()
		d[A][B], d[B][A], e[A][B], e[B][A] = C, C, C, C
	}
	for k := 0; k < N; k++ {
		for i := 0; i < N; i++ {
			for j := 0; j < N; j++ {
				d[i][j] = min(d[i][j], d[i][k]+d[k][j])
			}
		}
	}
	var res []int
	res = append(res, S)
	cur := S
	t := 0
	for cur != G {
		for next := 0; next < N; next++ {
			if e[cur][next] > 0 && t+e[cur][next]+d[next][G] == d[S][G] {
				t += e[cur][next]
				cur = next
				res = append(res, next)
				break
			}
		}
	}
	for i := 0; i < len(res); i++ {
		fmt.Printf("%d", res[i])
		if i < len(res)-1 {
			fmt.Printf(" ")
		} else {
			fmt.Println()
		}
	}
}
0