結果

問題 No.160 最短経路のうち辞書順最小
ユーザー tnoda_
提出日時 2015-04-08 20:54:05
言語 Go1.4
(1.4.2)
結果
AC  
実行時間 64 ms / 5,000 ms
コード長 1,262 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 32,512 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-25 00:11:23
合計ジャッジ時間 2,957 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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