結果

問題 No.416 旅行会社
ユーザー fmhrfmhr
提出日時 2016-08-28 10:43:59
言語 Go
(1.22.1)
結果
TLE  
実行時間 -
コード長 3,190 bytes
コンパイル時間 12,467 ms
コンパイル使用メモリ 235,440 KB
実行使用メモリ 83,788 KB
最終ジャッジ日時 2024-04-26 10:06:53
合計ジャッジ時間 19,945 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 221 ms
83,788 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 6 ms
6,940 KB
testcase_09 AC 28 ms
7,588 KB
testcase_10 AC 227 ms
20,444 KB
testcase_11 AC 218 ms
20,452 KB
testcase_12 AC 225 ms
20,452 KB
testcase_13 AC 221 ms
20,432 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

const inf int = 1 << 25
func main() {
	sc.Split(bufio.ScanWords)
	// indexと島を合わせるために孤立した島0を加えておく
	n := nextInt() + 1
	m := nextInt()
	q := nextInt()
	g := newGraph(n)
	bridge := make([]nodes, m)
	crashed := make(map[int]int)
	for i := 0; i < m; i++ {
		bridge[i].a = nextInt()
		bridge[i].b = nextInt()
		crashed[bridge[i].a*1000000+bridge[i].b] = inf
	}
	crash := make([]nodes, q)
	for i := 0; i < q; i++ {
		crash[i].a = nextInt()
		crash[i].b = nextInt()
		crashed[crash[i].a*1000000+crash[i].b] = i + 1
	}

	// 壊れない橋を追加する
	for i := 0; i < m; i++ {
		a := bridge[i].a
		b := bridge[i].b
		cost := crashed[a*1000000+b]
		g[a] = append(g[a], Edge{to: b, cost: cost})
		g[b] = append(g[b], Edge{to: a, cost: cost})
	}
	d := g.dijkstar(n, 1)

	for i := 2; i < n; i++ {
		switch d[i] {
		case inf:
			fmt.Println("-1")
		case -1:
			fmt.Println("0")
		default:
			fmt.Println(d[i])
		}
	}
}

type nodes struct {
	a, b int
}

type Edge struct {
	to   int
	cost int
}
type Graph [][]Edge

func newGraph(n int) Graph {
	g := make(Graph, n)
	for i := range g {
		g[i] = make([]Edge, 0)
	}
	return g
}

func (g *Graph) Find(n int) []Edge {
	return (*g)[n]
}

func (g *Graph) dijkstar(n, s int) []int {
	dist := make([]int, n) // 頂点sからの最短距離
	for i := range dist {
		dist[i] = -1
	}
	que := make(PriorityQueue, 0)
	heap.Init(&que)
	dist[s] = inf
	item := &Item{
		node:  s,
		cost:  inf,
	}
	heap.Push(&que, item)
	cnt := 0
	for que.Len() > 0 {
		p := que.Pop().(*Item)
		v := p.node
		if dist[v] < p.cost {
			cnt++
			continue
		}
		for i := 0; i < len((*g)[v]); i++ {
			e := (*g)[v][i]
			if dist[e.to] < min(dist[v], e.cost) {
				dist[e.to] = min(dist[v], e.cost)
				item := &Item{
					cost: dist[e.to], node: e.to,
				}
				heap.Push(&que, item)
			}
		}
		//fmt.Println("que.size: ", que.Len())
	}
	//fmt.Println("cnt : ", cnt)
	return dist
}

type Item struct {
	node  int // 頂点の番号
	cost  int // 最短距離
	index int
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	return pq[i].cost < pq[j].cost
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Item)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, node, dist int) {
	item.node = node
	item.cost = dist
	heap.Fix(pq, item.index)
}

var sc = bufio.NewScanner(os.Stdin)

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	i, _ := strconv.Atoi(nextLine())
	return i
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
0