http://acm.hdu.edu.cn/showproblem.php?pid=2682
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3581 Accepted Submission(s): 1118
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.
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).
If the all cities can be connected together,output the minimal cost,otherwise output "-1";
2
5
1
2
3
4
5
4
4
4
4
4
4
-1
Teddy
2009浙江大学计算机研考复试(机试部分)——全真模拟
lcy
题目大意:有N个城市,每个城市有一个幸福值,如果两个城市A、B的幸福值分别为VA、VB,如果VA是素数,
或者VB是素数,又或者VA+VB是素数,则城市A和B就能连接一条路,建路的所用花费为Min(Min(VA , VB),|VA-VB|)。
问:现在想要建几条路,使得能够连接所有的城市,所需要建设的最少路程和是多少?
思路:就是求最小生成树,先用素数筛选法将素数打表,然后根据题意建边。最后就是用Kruskal求最小生成树就行了。
这个大佬AC了,思路一致:点击链接
//Min(Min(VA , VB),|VA-VB|)。
#include
#include
#include
#include
using namespace std;
const int nmax=610;
const int mmax=360000+10;
const int pmax=1000000+10;
int prime[pmax];//素数数组
int mark[pmax];//全体数的标记数组
int s[nmax];
int n,m;//点数、边数
struct Edge{
int u,v;
int val;//起点、终点、边权
}edge[mmax];
bool cmp(Edge a,Edge b){
return a.val=n-1){
Kruskal();
}
else{
printf("-1\n");
}
}
}
return 0;
}