POJ 1826 The Best Farm

The Best Farm
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 1519   Accepted: 455

Description

Background
In a fight against invaders, farmer William get the farmers all around the country together to help the king beat the invaders. So the king decided to award farmer William a great farm after the winning of the war.
Problem
The king divides his country as a A*B grid and labels each 1*1 square with a pair of integers. Refer to the following picture as example:
POJ 1826 The Best Farm_第1张图片
Nevertheless, not all the squares are available. Some have been awarded to others, and some have been destroyed in the war. So the king only list the available 1*1 squares, and let farm William choose some from them. Meanwhile, William can’t choose all of the squares. He can only choose some of them, so that the squares can form a connected area, to build up his farm. A connected area is defined as following:
1. A connected area is made up of some 1*1 squares;
2. From any of these 1*1 squares, one can walk to any other 1*1 square belonged to this area without entering some square that is not in this area;
3. When standing in a square, one can walk into the adjacent squares in four directions: north, south, east, and west.
In addition, every square available has a value. William should choose to build up a farm (connected area) that is the most valuable. In other words, William should choose some of the square forming a connected area, in which the sum of the value of squares is the largest.
Your task in this problem is to find out the largest value.

Input

The input consists of several test cases. In the first line of each test case, there are one positive integers N (1<=N<=200000), indicating the number of available squares. The following N lines contain the information of the N squares, one per line. In one line, there are three integers x y v separated by one blank. (x, y) is the location of this square, and v is its value. All the x and y lie in the range of signed 16-bit integer. The value v is a nonnegative integer that is less than 10000. The test case starting with one zero is the final test case and has no output.

Output

For each test case, print your answer, the largest value with which William can build up his farm, in one line without any redundant spaces.

Sample Input

1
0 0 1
6
0 1 1
0 0 1
1 0 1
2 2 2
2 1 2
2 -1 1
0

Sample Output

1
4

Source

Atlas of rruucc@POJ

 

/* 主要思想就是哈希,因为坐标值x,y非常大,如果不哈希是无法存储的剩下的就是 找权值最大的连通分量了,一开始没有看清题,以为每个farm的value值可以是负 数(如果是这样就需要树状DP了),写着写着突然发现题目中说value值是非负值, 那么这就容易多了,直接DFS找所有连通分量然后取总value最大的就是题目要求 的了。看来好看清题啊,不过乘这个机会又把树状DP复习了一下 */ #include <iostream> //定义哈希List的最大容量 #define HASH_VAL 200000 using namespace std; //哈希容器中元素的定义 struct elem { //x, y是当前地的坐标, pos是当前地在farms数组中对应的下标 int x, y, pos; //指向下一个具有同样哈希值的elem,避免访问冲突 elem *next; elem() { x = y = pos = 0; next = NULL; } }; //哈希容器 struct hash { //next指示第一个具有这个哈希值的farm elem *next; }hashs[HASH_VAL + 2]; //存储每一块farm struct farm { int value; //标识是否被访问过 bool v; elem *ptr; }farms[HASH_VAL + 2]; int fNum, maxVal; //哈希函数 int getHashVal(int x, int y) { __int64 temp = x * x + y * y; return temp % HASH_VAL; } //-1 is not exist, else the pos of the farms in farms //if not exist and insertIfNotExist is true then insert this farm to the hashs int getPos(int x, int y, bool insertIfNotExist, int pos, int value) { int hashVal = getHashVal(x, y); if(!hashs[hashVal].next) { if(!insertIfNotExist) return -1; else { elem *newPtr = new elem(); newPtr->x = x; newPtr->y = y; newPtr->pos = pos; farms[pos].ptr = newPtr; farms[pos].v = false; farms[pos].value = value; hashs[hashVal].next = newPtr; return pos; } } if(hashs[hashVal].next->x == x && hashs[hashVal].next->y == y) return hashs[hashVal].next->pos; elem *curPtr = hashs[hashVal].next; while(curPtr->next) { if(curPtr->next->x == x && curPtr->next->y == y) return curPtr->next->pos; curPtr = curPtr->next; } if(!insertIfNotExist) return -1; else { elem *newPtr = new elem(); newPtr->x = x; newPtr->y = y; newPtr->pos = pos; farms[pos].ptr = newPtr; farms[pos].v = false; farms[pos].value = value; curPtr->next = newPtr; return pos; } } int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; //dfs遍历找连通分量 int dfs(int nodePos) { farms[nodePos].v = true; int curVal = farms[nodePos].value; int curX = farms[nodePos].ptr->x, curY = farms[nodePos].ptr->y; int newX, newY; for(int p = 0; p < 4; p++) { newX = curX + dir[p][0]; newY = curY + dir[p][1]; int pos = getPos(newX, newY, false, 0, 0); if(pos == -1 || farms[pos].v) continue; curVal += dfs(pos); } return curVal; } int main() { int i, x, y, value; while(scanf("%d", &fNum) && fNum != 0) { maxVal = INT_MIN; memset(hashs, 0, sizeof(hashs)); for(i = 0; i < fNum; i++) { scanf("%d%d%d", &x, &y, &value); if(farms[i].ptr != NULL) { delete farms[i].ptr; farms[i].ptr = NULL; } getPos(x, y, true, i, value); } int curVal; for(i = 0; i < fNum; i++) { if(farms[i].v) continue; curVal = dfs(i); if(curVal > maxVal) maxVal = curVal; } printf("%d/n", maxVal); } return 0; } 

你可能感兴趣的:(struct,null,Integer,input,Build,each)