結果

問題 No.807 umg tours
ユーザー ccppjsrbccppjsrb
提出日時 2020-08-10 15:30:35
言語 Go
(1.23.4)
結果
AC  
実行時間 854 ms / 4,000 ms
コード長 2,974 bytes
コンパイル時間 13,342 ms
コンパイル使用メモリ 215,440 KB
実行使用メモリ 75,392 KB
最終ジャッジ日時 2024-11-23 20:11:11
合計ジャッジ時間 22,887 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 3 ms
6,816 KB
testcase_03 AC 3 ms
6,816 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 AC 3 ms
6,816 KB
testcase_07 AC 2 ms
6,820 KB
testcase_08 AC 2 ms
6,816 KB
testcase_09 AC 2 ms
6,816 KB
testcase_10 AC 2 ms
6,820 KB
testcase_11 AC 675 ms
56,396 KB
testcase_12 AC 440 ms
39,428 KB
testcase_13 AC 673 ms
55,444 KB
testcase_14 AC 263 ms
29,680 KB
testcase_15 AC 197 ms
20,224 KB
testcase_16 AC 720 ms
59,648 KB
testcase_17 AC 841 ms
62,128 KB
testcase_18 AC 844 ms
63,516 KB
testcase_19 AC 820 ms
53,124 KB
testcase_20 AC 328 ms
26,932 KB
testcase_21 AC 345 ms
26,752 KB
testcase_22 AC 136 ms
16,000 KB
testcase_23 AC 105 ms
13,696 KB
testcase_24 AC 447 ms
55,532 KB
testcase_25 AC 854 ms
75,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"math"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	m := getNextInt(scanner)
	g := newGraph(n)
	for i := 0; i < m; i++ {
		a := getNextInt(scanner) - 1
		b := getNextInt(scanner) - 1
		c := getNextInt64(scanner)
		g.appendEdge(a, b, i, c)
		g.appendEdge(b, a, i, c)
	}
	dd := &daikes{}
	for i := 0; i < n; i++ {
		g.v[i].c = math.MaxInt64
		g.v[i].used = math.MaxInt64
	}
	heap.Push(dd, newDaike(0, 0, false))
	for dd.Len() > 0 {
		p := heap.Pop(dd).(daike)
		if p.used {
			if g.v[p.i].used <= p.c {
				continue
			}
			g.v[p.i].used = p.c
		} else {
			if g.v[p.i].c <= p.c {
				continue
			}
			g.v[p.i].c = p.c
		}
		for _, e := range g.e[p.i] {
			heap.Push(dd, newDaike(e.to, p.c+e.c, p.used))
			if p.used {
				continue
			}
			heap.Push(dd, newDaike(e.to, p.c, true))
		}
	}
	fmt.Fprintln(writer, 0)
	for i := 1; i < n; i++ {
		fmt.Fprintln(writer, g.v[i].c+g.v[i].used)
	}
}
func newDaike(i int, c int64, used bool) daike {
	return daike{i: i, c: c, used: used}
}

type daike struct {
	i    int
	c    int64
	used bool
}
type daikes []daike

func (h daikes) Len() int { return len(h) }
func (h daikes) Less(i, j int) bool {
	return h[i].c < h[j].c
}
func (h daikes) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *daikes) Push(x interface{}) {
	*h = append(*h, x.(daike))
}
func (h *daikes) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

type vertex struct {
	c, used int64
}
type edge struct {
	to, id int
	c      int64
}
type graph struct {
	v []vertex
	e [][]edge
}

func newGraph(n int) graph {
	return graph{
		v: make([]vertex, n),
		e: make([][]edge, n),
	}
}
func (g *graph) appendEdge(from, to, id int, c int64) {
	g.e[from] = append(g.e[from], edge{
		to: to,
		id: id,
		c:  c,
	})
}
0