結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
fmhr
|
| 提出日時 | 2016-08-28 11:59:20 |
| 言語 | Go (1.23.4) |
| 結果 |
AC
|
| 実行時間 | 421 ms / 4,000 ms |
| コード長 | 3,664 bytes |
| コンパイル時間 | 12,297 ms |
| コンパイル使用メモリ | 222,304 KB |
| 実行使用メモリ | 35,072 KB |
| 最終ジャッジ日時 | 2024-12-14 20:10:19 |
| 合計ジャッジ時間 | 18,300 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
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
}
fmhr