hdu 2682 Tree 最小生成树 (并查集)

Tree

Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 26   Accepted Submission(s) : 10

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and B whose value of happiness are VA and VB,if VA is a prime number,or VB is a prime number or (VA+VB) is a prime number,then they can be connected.What's more,the cost to connecte two cities is Min(Min(VA , VB),|VA-VB|).
Now we want to connecte all the cities together,and make the cost minimal.

Input

The first will contain a integer t,followed by t cases.
Each case begin with a integer N,then N integer Vi(0<=Vi<=1000000).

Output

If the all cities can be connected together,output the minimal cost,otherwise output "-1";

Sample Input

2
5
1
2
3
4
5

4
4
4
4
4

Sample Output

4
-1

Author

Teddy
最小生成树:类似于畅通工程一类的,不过中间多加了素数的处理判断


#include
#include
#include
#include
using namespace std;
struct inin
{
	int a;
	int b;
	int dis;
}boy[410000];

int p[660];
int prime[1000010];
int p2[660];

int cmp(inin a,inin b)
{
	return a.dis




你可能感兴趣的:(hdu,oj,并查集)