#include <iostream>
const int N_MAX = 100;
const int M_MAX = 100;
const int NM_MAX = N_MAX * M_MAX;
const int H_MAX = 10000;
const int INFTY = 65535;
using namespace std;
struct t_coords
{
char x;
char y;
};
struct t_heap
{
t_coords data[NM_MAX];
unsigned int size;
};
struct t_square
{
unsigned int h;
unsigned int l_path;
};
// p_queue_elem moze tutaj blad??
struct t_queue_elem
{
t_coords coords;
t_queue_elem* next;
};
struct t_queue
{
t_queue_elem *first;
t_queue_elem *last ;
};
t_square board[N_MAX+1][M_MAX+1];
unsigned int n,m;
t_queue queue;
t_heap heap;
void queue_init(t_queue q)
{
q.first = NULL;
q.last = NULL;
}
void queue_put(char x, char y, t_queue q)
{
t_queue_elem *p = new t_queue_elem;
p->coords.x = x;
p->coords.y = y;
p->next = NULL;
if (q.first == NULL)
{
q.first = p;
}
else
{
q.last->next = p;
}
q.last = p;
}
void queue_get(char x, char y, t_queue q)
{
t_queue_elem *p = new t_queue_elem();
x = q.first->coords.x;
y = q.first->coords.y;
p = q.first;
q.first = q.first->next;
if(q.first == NULL)
{
q.last = NULL;
}
delete p;
}
bool queue_empty(t_queue q)
{
if(q.first == NULL)
{
return true;
}
else
{
return false;
}
}
unsigned int height(t_coords c)
{
return board[c.x][c.y].h;
}
void heap_init(t_heap h)
{
h.size = 0;
}
void heap_swap(t_heap h, unsigned int i, unsigned int j)
{
t_coords c;
c = h.data[i];
h.data[i] = h.data[j];
h.data[j] = c;
}
void heap_go_down(t_heap h)
{
unsigned int i, smallest;
i = 1;
while(2*i <= h.size)
{
if(2*i <= h.size && height(h.data[2*i]) < height(h.data[i]))
{
smallest = 2*i;
}
else
{
smallest = i;
}
if(2*i + 1 <= h.size && height(h.data[2*i+1]) < height(h.data[smallest]))
{
smallest = 2 * i + 1;
}
if(smallest != i)
{
heap_swap(h,smallest,i);
}
else
{
break;
}
}
}
void heap_add(t_heap h, char x, char y)
{
unsigned int i;
h.size++;
i = h.size;
while( i > 1 && board[x][y].h < height( h.data[i/2]))
{
h.data[i] = h.data[i/2];
i = i/2;
}
h.data[i].x = x;
h.data[i].y = y;
}
void heap_remove( t_heap h, char x, char y)
{
x = h.data[1].x;
y = h.data[1].y;
h.data[1] = h.data[h.size];
h.size--;
heap_go_down(h);
}
bool heap_empty(t_heap h)
{
if(h.size == 0)
{
return true;
}
else
{
return false;
}
}
// indeksy odwrocic m z n ??
// zle wczytanie danych ??
void read_data()
{
unsigned int w,h;
cin >> n;
cin >> m;
heap_init(heap);
for(int i = 0; i <= n + 1; i++)
{
board[i][0].h = INFTY;
board[i][0].l_path = INFTY;
board[i][m + 1].h = INFTY;
board[i][m + 1].l_path = INFTY;
}
for(int j = 0; j <= m + 1; j++)
{
board[0][j].h = INFTY;
board[0][j].l_path = INFTY;
board[n + 1][j].h = INFTY;
board[n + 1][j].l_path = INFTY;
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
board[i][j].l_path = INFTY;
cin >> board[i][j].h;
heap_add( heap, i, j);
}
}
}
void adjust_lpath(char x, char y, unsigned int value)
{
if(board[x][y].h <= value && board[x][y].l_path == INFTY)
{
board[x][y].l_path = value;
queue_put(x,y,queue);
}
}
void find_paths()
{
char x,y;
x=1;
y=1;
t_queue_elem p;
queue_init(queue);
do
{
heap_remove(heap, x, y);
if(x == 1 || x == n ||y == 1 || y == m || board[x-1][y].l_path < INFTY
|| board[x+1][y].l_path < INFTY
|| board[x][y-1].l_path < INFTY
|| board[x][y+1].l_path < INFTY)
{
adjust_lpath( x, y, board[x][y].h);
}
while(!queue_empty(queue))
{
queue_get(x, y, queue);
adjust_lpath( x - 1, y, board[x][y].l_path);
adjust_lpath( x + 1, y, board[x][y].l_path);
adjust_lpath( x, y - 1, board[x][y].l_path);
adjust_lpath( x, y + 1, board[x][y].l_path);
}
}while(!heap_empty(heap));
}
long int sum_up()
{
char i,j;
long int sum;
sum = 0;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= m; j++)
{
sum += (board[i][j].l_path - board[i][j].h);
}
}
return sum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