結果
問題 | No.416 旅行会社 |
ユーザー | fmhr |
提出日時 | 2016-08-28 11:59:20 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 514 ms / 4,000 ms |
コード長 | 3,664 bytes |
コンパイル時間 | 10,906 ms |
コンパイル使用メモリ | 219,508 KB |
実行使用メモリ | 34,560 KB |
最終ジャッジ日時 | 2024-05-08 15:15:58 |
合計ジャッジ時間 | 17,049 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 228 ms
17,920 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 7 ms
5,376 KB |
testcase_09 | AC | 27 ms
5,376 KB |
testcase_10 | AC | 236 ms
18,688 KB |
testcase_11 | AC | 230 ms
18,688 KB |
testcase_12 | AC | 236 ms
17,920 KB |
testcase_13 | AC | 221 ms
17,920 KB |
testcase_14 | AC | 401 ms
34,560 KB |
testcase_15 | AC | 411 ms
34,432 KB |
testcase_16 | AC | 376 ms
34,048 KB |
testcase_17 | AC | 394 ms
34,560 KB |
testcase_18 | AC | 514 ms
34,432 KB |
testcase_19 | AC | 381 ms
27,520 KB |
testcase_20 | AC | 329 ms
27,648 KB |
ソースコード
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{ value: s, priority: inf, } heap.Push(&que, item) //cnt := 0 for que.Len() > 0 { p := heap.Pop(&que).(*Item) v := p.value if dist[v] > p.priority { //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{ priority: dist[e.to], value: 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 //} // An Item is something we manage in a priority queue. type Item struct { value int // The value of the item; arbitrary. priority int // The priority of the item in the queue. // The index is needed by update and is maintained by the heap.Interface methods. index int // The index of the item in the heap. } // 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 { // We want Pop to give us the highest, not lowest, priority so we use greater than here. return pq[i].priority > pq[j].priority } 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, value int, priority int) { item.value = value item.priority = priority 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 }