【最小生成树专题】HDU 2682 Tree (谜啊)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qian2213762498/article/details/82492730

http://acm.hdu.edu.cn/showproblem.php?pid=2682

Tree

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3581    Accepted Submission(s): 1118

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

Source

2009浙江大学计算机研考复试(机试部分)——全真模拟

Recommend

lcy

思路

题目大意:有N个城市,每个城市有一个幸福值,如果两个城市A、B的幸福值分别为VA、VB,如果VA是素数,

或者VB是素数,又或者VA+VB是素数,则城市A和B就能连接一条路,建路的所用花费为Min(Min(VA , VB),|VA-VB|)。

问:现在想要建几条路,使得能够连接所有的城市,所需要建设的最少路程和是多少?

思路:就是求最小生成树,先用素数筛选法将素数打表,然后根据题意建边。最后就是用Kruskal求最小生成树就行了。

这个大佬AC了,思路一致:点击链接

没AC Code (样例都过了啊,就是WA!求大佬赐教!)

//Min(Min(VA , VB),|VA-VB|)。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> 
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<b.val; //按照边权从小到大排序,求最小生成树 
} 
 
int father[nmax];
int findFather(int u){
	if(u==father[u]) return u;
	else{
		int f=findFather(father[u]);
		father[u]=f;
		return f;
	}
}
 
void init(int n){
	for(int i=1;i<=n;i++){
		father[i]=i;
	}
} 
void Kruskal(){//返回最小生成树的边权和
    init(n); 
	int cnt=0;//有效合并次数 
	int ans=0;//最小边权和 
	for(int i=0;i<m;i++){//遍历m条边
		int fu=findFather(edge[i].u);
		int fv=findFather(edge[i].v);
		if(fu!=fv){
			father[fu]=fv;
			ans+=edge[i].val;
			cnt++;
		}
		if(cnt==n-1)//合并了N-1条边,已经找到了最小生成树
			break; 
	} 
	if(cnt==n-1)//找到最小生成树
		printf("%d\n",ans);
	else		//图不连通 
		printf("-1\n");
}
 
int main(int argc, char** argv) {
	int T;
	while(scanf("%d",&T)!=EOF){
		while(T--){
			scanf("%d",&n);
			memset(father,0,sizeof(father));
			memset(prime,0,sizeof(prime));
			memset(mark,0,sizeof(mark));
			memset(s,0,sizeof(s));
			memset(edge,0,sizeof(edge));
			init(n);
			prime[0]=1;
			prime[1]=1;
			prime[2]=0;
			for(int i=2;i<=1000000;i++){
				if(!prime[i]){
					for(int j=i*2;j<=1000000;j+=i){
						prime[j]=1;
					}			
				}
			}
			for(int i=1;i<=n;i++){
				scanf("%d",&s[i]);
			}
			int id=0;
			for(int i=1;i<=n;i++){
				for(int j=i+1;j<=n;j++){
					int tmp=abs(s[i]-s[j]);
					//if(!mark[s[i]]||!mark[s[j]]||!(mark[s[i]]+mark[s[j]])){
					if(prime[s[i]]==0||prime[s[j]]==0||(prime[s[i]]+prime[s[j]])==0){
						edge[id].u=i;
						edge[id].v=j;
						edge[id].val=min(tmp,min(s[j],s[i]));
						id++;
					}
				} 
			}
			m=id;
			sort(edge,edge+m,cmp); 
			if(m>=n-1){
				Kruskal();
			}
			else{
				printf("-1\n");
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qian2213762498/article/details/82492730