Advertisement
Ladies_Man

Shortest map route (Дейкстра)

Jun 9th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.94 KB | None | 0 0
  1. package main
  2. import "github.com/skorobogatov/input"
  3. import "fmt"
  4. import "os"
  5. import "math"
  6. type vertex struct {
  7.         value, dist float64
  8.         index, x, y int
  9.     edge        []*vertex
  10.         parent      *vertex
  11. }
  12. type edge struct {
  13.     u, v, ind int
  14.     vertex    *vertex
  15.     weight    int
  16. }
  17. type queue struct {
  18.     heap       []*vertex
  19.     count, cap int
  20. }
  21.  
  22. func InitPriorityQueue(size int) *queue {
  23.     var q queue
  24.     q.heap, q.count, q.cap = make([]*vertex, size), 0, size
  25.     return &q
  26. }
  27.  
  28. func (q *queue) InitAttributes(G [][]vertex, n int) {
  29.         for i := 0; i < n; i++ {
  30.         for j := 0; j < n; j++ {
  31.             v := &G[i][j]
  32.             if v == &G[0][0] { v.dist = v.value } else { v.dist = math.Inf(1) }
  33.             v.parent = nil
  34.             q.QueueInsert(&G[i][j])
  35.         }
  36.     }
  37. }
  38.  
  39. func InitGraph(size int) [][]vertex {
  40.     graph_map := make([][]vertex, size)
  41.     for i := 0; i < size; i++ { graph_map[i] = make([]vertex, size) }
  42.     return graph_map
  43. }
  44.  
  45. func (q *queue) QueueInsert(ptr *vertex) {
  46.     i := q.count
  47.     if i == q.cap { fmt.Printf("overflow\n") }
  48.     q.count++
  49.     q.heap[i] = ptr
  50.     for i > 0 && q.heap[i].dist < q.heap[(i-1)/2].dist {
  51.         q.heap[(i-1)/2], q.heap[i] = q.heap[i], q.heap[(i-1)/2]
  52.         q.heap[i].index = i
  53.         i = (i - 1) / 2
  54.     }
  55.     q.heap[i].index = i
  56. }
  57.  
  58. func (q *queue) ExtractMin() *vertex {
  59.     var Heapify func(i int, n int, P []*vertex)
  60.     Heapify = func(i int, n int, P []*vertex) {
  61.         for {
  62.             l := 2*i + 1
  63.             r := l + 1
  64.             j := i
  65.             if l < n && P[i].dist > P[l].dist { i = l }
  66.             if r < n && P[i].dist > P[r].dist { i = r }
  67.             if i == j { break }
  68.             P[i], P[j] = P[j], P[i]
  69.             P[i].index, P[j].index = i, j
  70.         }
  71.     }
  72.     if 0 == q.count { fmt.Printf("Queue is empty\n") }
  73.     ptr := q.heap[0]
  74.     q.count--
  75.     if 0 != q.count {
  76.         q.heap[0] = q.heap[q.count]
  77.         q.heap[0].index = 0
  78.         Heapify(0, q.count, q.heap)
  79.     }
  80.     return ptr
  81. }
  82.  
  83. func (q *queue) DecreaseKey(i int, k float64) {
  84.     q.heap[i].dist = k
  85.     for i > 0 && q.heap[(i-1)/2].dist > k {
  86.         q.heap[(i-1)/2], q.heap[i] = q.heap[i], q.heap[(i-1)/2]
  87.         q.heap[i].index = i
  88.         i = (i - 1) / 2
  89.     }
  90.     q.heap[i].index = i
  91. }
  92.  
  93. func nless2(num int) bool {
  94.     var a, b, c, d int
  95.     if 1 == num {
  96.         input.Scanf("%d\n", &a)
  97.         fmt.Printf("%d", a)
  98.         return false
  99.     }
  100.     if 2 == num {
  101.         input.Scanf("%d %d\n%d %d\n", &a, &b, &c, &d)
  102.         if c > b { fmt.Printf("%d", a+b+d) } else { fmt.Printf("%d", a+c+d) }
  103.         return false
  104.     }
  105.     return true
  106. }
  107.  
  108. func Relax(u *vertex, v *vertex, weight float64) (changed bool) {
  109.     changed = false
  110.     if u.dist+weight < v.dist { changed = true }
  111.     if changed { v.dist = u.dist + weight }
  112.     return changed
  113. }
  114.  
  115. func main() {
  116.     var n int
  117.     var x float64
  118.     input.Scanf("%d\n", &n)
  119.     if n > 1200 { goto next }
  120.     if m := nless2(n); !(m) { os.Exit(0) }
  121. next:
  122.     graph_map := InitGraph(n)
  123.     edge_map := make([]edge, n*n)
  124.     for i := 0; i < n; i++ {
  125.         for j := 0; j < n; j++ {
  126.             input.Scanf("%f", &x)
  127.             graph_map[i][j].value = x
  128.             graph_map[i][j].x, graph_map[i][j].y = i, j
  129.                         graph_map[i][j].parent = nil
  130.             edge_map[i].weight = (int)(x)
  131.             edge_map[i].ind = i
  132.         }
  133.     }
  134.     queue := InitPriorityQueue(n * n)
  135.     queue.InitAttributes(graph_map, n)
  136.     for 0 != queue.count {
  137.         v := queue.ExtractMin()
  138.         v.index = -1
  139.         x, y := v.x, v.y
  140.         if x+1 < n && -1 != graph_map[x+1][y].index && Relax(v, &graph_map[x+1][y], graph_map[x+1][y].value) { queue.DecreaseKey(graph_map[x+1][y].index, graph_map[x+1][y].dist) }
  141.         if y+1 < n && -1 != graph_map[x][y+1].index && Relax(v, &graph_map[x][y+1], graph_map[x][y+1].value) { queue.DecreaseKey(graph_map[x][y+1].index, graph_map[x][y+1].dist) }
  142.         if x > 0 && -1 != graph_map[x-1][y].index && Relax(v, &graph_map[x-1][y], graph_map[x-1][y].value) { queue.DecreaseKey(graph_map[x-1][y].index, graph_map[x-1][y].dist) }
  143.         if y > 0 && -1 != graph_map[x][y-1].index && Relax(v, &graph_map[x][y-1], graph_map[x][y-1].value) { queue.DecreaseKey(graph_map[x][y-1].index, graph_map[x][y-1].dist) }
  144.  
  145.     }
  146.     bottom_right := (int)(graph_map[n-1][n-1].dist)
  147.     fmt.Printf("%d", bottom_right)
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement