Advertisement
Ladies_Man

Tele lines (Тел. линии)

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