typedef int (*cmp_func)(int index1, int index2);
typedef void (*swap_func)(int index1, int index2);
template<class T>
void adjust_sub_tree(T A[], int n, int root, cmp_func *cmp, swap_func *swap)
{
int left, right;
int i=root;/*root is the index, and n is the number*/
int max;
template <class T>
void make_heap(T A[], int n, cmp_func cmp, swap_func swap)
{
/*n starts from 0, so i's (i>0) parent index should be (i-1)/2*/
/* j's (j>=0) children should be 2*j+1, 2*j+2 */
int p;
p=(n-1-1)/2; /*the last parent*/
while(p>=0)
{
adjust_sub_tree(A, n, p--, &cmp, &swap);
}
return;
}
template <class T>
T* pop_heap(T A[], int n, cmp_func cmp, swap_func swap)
{
if(n==1)
{
return &A[0];
}
/*at first, we swap A[0] and A[n-1]*/
(*swap)(0, n-1);
/*now, adjust the heap to be valid*/
adjust_sub_tree(A, n-1, 0, &cmp, &swap);
#define ROW 400
#define COL 300
#define MAX_OBSTACLE 6
typedef struct node
{
int bit_found; /*0->not valued, 1->have valid value, 2->has been found*/
int obstacle;
struct node * pre;
int shortest_dis;/*indicates the shortest distance to the original one*/
}NODE;/*this define is not very good, should seperate two kinds info*/
int not_found_num=ROW*COL; /*indicates the number not have been found*/
NODE map[ROW][COL];
NODE *heap[ROW*COL]={NULL};/*this is the heap, which stores all of the un-founded nodes*/
int heap_len=0;/*the valid number in the heap*/
int cmp(int index1, int index2)
{
return -(heap[index1]->shortest_dis-heap[index2]->shortest_dis); /*the template is a big-heap*/
}
/*add these node into heap if it has not been set valid value yet*/
if(new_row>0)
{
/*up is valid*/
UPDATE(new_row-1, new_col)
INSERT_INTO_HEAP(new_row-1, new_col)
}
if(new_row<ROW-1)
{
UPDATE(new_row+1, new_col)
INSERT_INTO_HEAP(new_row+1, new_col)
}
if(new_col>0)
{
UPDATE(new_row, new_col-1)
INSERT_INTO_HEAP(new_row, new_col-1)
}
if(new_col<COL-1)
{
UPDATE(new_row, new_col+1)
INSERT_INTO_HEAP(new_row, new_col+1)
}
/*now, we should modify the heap to the right format*/
make_heap(heap, heap_len, cmp, swap);
}
int find_shortest_path(int start_row, int start_col)
{
int new_row, new_col;/*saving the coordinate of the new node*/
//init the S set, done in the main initilization area
//if there are some nodes not been found, then go on
while(find_new_node(&new_row, &new_col) == 0)
{
//cout<<"reaching node: "<<new_row<<", "<<new_col<<endl;
if(new_row == 0)
{
/*reach the first row*/
return new_col;
}
update_distance(new_row, new_col);
}
和zoj monthly, Nov 2008在一定程序上有类似
E题目大意:100*100的格子,每个格子里有整数的分数。一个人从左上角走到右下角,每次可以往左、右或下走到相邻的格子,如果格子里是非正数,则每经过一次格子会加上那个格子里的分数,如果是格子里是正数,则第一次经过它是加分的,以后再经过这个格子都会减掉其中的分数,求到终点最大的得分方法。