Advertisement
Guest User

YA SOSU ZA KOLBASY

a guest
Nov 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <inttypes.h>
  4. #include <string.h>
  5.  
  6. enum { bigInt = 10000000000 };
  7.  
  8. long long Abs(int c)
  9. {
  10.     return (2 * (c > 0) - 1) * c * 1LL;
  11. }
  12. struct cell
  13. {
  14.     int to;
  15.     int cost;
  16. };
  17. struct heapA
  18. {
  19.     long long size;
  20.     struct cell *a;
  21. }H;
  22. void shiftDown(struct heapA *h, int i)
  23. {
  24.     struct cell *b = NULL;
  25.     b = h -> a;
  26.     while(2 * i + 1 < (h -> size))
  27.     {
  28.         int l = 2 * i + 1;
  29.         int r = 2 * i + 2;
  30.         int j = l;
  31.         if(r < h -> size && (*(b + r)).cost < (*(b + l)).cost)
  32.         {
  33.             j = r;
  34.         }
  35.         if((*(b + i)).cost <= (*(b + j)).cost)
  36.         {
  37.             break;
  38.         }
  39.         struct cell c;
  40.         c = *(b + i);
  41.         *(b + i) = *(b + j);
  42.         *(b + j) = c;
  43.         i = j;
  44.     }
  45. }
  46. void shiftUp(struct heapA *h, int i)
  47. {
  48.     struct cell *b = h->a;
  49.     while(((b + i)->cost) < ((b + (i - 1)/2) -> cost))
  50.     {
  51.         struct cell c;
  52.         c = *(b + i);
  53.         *(b + i) = *(b + (i-1)/2);
  54.         *(b + (i-1)/2) = c;
  55.         i = (i - 1) / 2;
  56.     }
  57. }
  58. struct cell extrMin(struct heapA *h)
  59. {
  60.     struct cell m = *(h -> a);
  61.     *(h -> a) = *(h -> a + h -> size - 1);
  62.     (h -> size) -= 1;
  63.     shiftDown(h, 0);
  64.     return m;
  65. }
  66. void insert(struct heapA *h, struct cell value)
  67. {
  68.     (h -> size) += 1;
  69.     *((h -> a) + (h -> size) - 1) = value;
  70.     shiftUp(h, (h->size) - 1);
  71. }
  72. const int dx[4] = {1, 0, -1 , 0};
  73. const int dy[4] = {0, 1, 0, -1};
  74. int main(void)
  75. {
  76.     int n, m, x1, x2, y1, y2;
  77.     scanf("%d%d%d%d%d%d", &n, &m, &y1, &x1, &y2, &x2);
  78.     int *a = malloc(sizeof(int) * n * m);
  79.     long long *d = malloc(n * m * sizeof(long long));
  80.     H.a = malloc((n + 1) * (m + 1) * sizeof(struct cell));
  81.     H.size = 0;
  82.     for(int i = 0; i < n; i++)
  83.     {
  84.         for(int j = 0; j < m; ++j)
  85.         {
  86.             int x;
  87.             scanf("%d", &x);
  88.             *(a + i * m + j) = x;
  89.         }
  90.     }
  91.  
  92.     for(int i = 0; i < n; ++i)
  93.     {
  94.         for(int j = 0; j < m; ++j)
  95.         {
  96.             *(d + i * m + j) = bigInt;
  97.         }
  98.     }
  99.     *(d + x1 + y1 * m) = 0;
  100.     struct cell tmp = {x1 + y1 * m, 0};
  101.     insert(&H, tmp);
  102.     while(H.size)
  103.     {
  104.         struct cell t = extrMin(&H);
  105.         int v = t.to;
  106.         long long cst = t.cost;
  107.         if(cst > *(d + v)) continue;
  108.         int y = v / m;
  109.         int x = v % m;
  110.         for(int i = 0; i < 4; ++i)
  111.         {
  112.             if(x + dx[i] < m && x + dx[i] >= 0 && y + dy[i] >= 0 && y + dy[i]< n)
  113.             {
  114.                 int to = x + dx[i] + (y + dy[i]) * m;
  115.                 long long path = Abs(*(a  + v) - *(a + to));
  116.                 if(*(d + v) + path < *(d + to))
  117.                 {
  118.                     *(d + to) = *(d + v) + path;
  119.                     tmp.cost = *(d + to);
  120.                     tmp.to = to;
  121.                     insert(&H, tmp);
  122.                 }
  123.             }
  124.         }
  125.     }
  126.     printf("%lld", *(d + x2 + y2 * m));
  127.     free(a);
  128.     free(d);
  129.     free(H.a);
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement