思路:由于题目对能相连的点有限制,必须将这些点处理,能相连的点合并到一个集合中,最后查看是否所有点都在一个集合里,若都在说明是一个连通图,存在最小生成树,否则图不连通,不存在最小花费。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int CityHappy[605],vis[605];
int isprime[1000005],dist[605];
int map[601][601],n;
int father[605],depth[605];
void init_B()
{
int i;
for(i = 1;i <= n;i ++)
{
father[i] = i;
depth[i] = 0;
}
}
int find(int x)
{
if(x == father[x])
return x;
return father[x] = find(father[x]);
}
void unit(int x,int y)
{
x = find(x);
y = find(y);
if(x == y)
return ;
if(depth[x] > depth[y])
father[y] = x;
else
{
if(depth[x] < depth[y])
father[x] = y;
else
{
father[x] = y;
depth[y]++;
}
}
}
void prime()
{
int i,j;
isprime[0] = isprime[1] = 1;
for(i = 2;i <= 1e6;i ++)
{
if(!isprime[i])
{
for(j = i << 1;j <= 1e6;j += i)
isprime[j] = 1;
}
}
}
int judge(int a,int b)
{
if(!isprime[a] || !isprime[b])
return 1;
if(!isprime[a+b])
return 1;
return 0;
}
int min(int a,int b)
{
return a < b?a:b;
}
void opration()
{
int i,j,a,b;
init_B();
for(i = 1;i <= n;i ++)
{
for(j = 1;j <= n;j ++)
{
if(i != j)
{
a = CityHappy[i];
b = CityHappy[j];
if(judge(a,b))
{
map[i][j] = min(min(a,b),abs(a-b));
map[j][i] = map[i][j];
unit(i,j);
}
else
map[i][j] = map[j][i] = 1 << 30;
}
}
}
}
void init()
{
int i;
memset(vis,0,sizeof(vis));
for(i = 1;i <= n;i ++)
dist[i] = map[1][i];
}
int main()
{
int t,i,j,k,cnt,min,sum;
scanf("%d",&t);
prime();
while(t--)
{
sum = cnt = 0;
scanf("%d",&n);
for(i = 1;i <= n;i ++)
scanf("%d",&CityHappy[i]);
opration();
init();
for(i = 1;i <= n;i ++)
{
if(i == find(i))
cnt++;
if(cnt == 2)
break;
}
if(cnt == 2)
{
printf("-1
");
continue ;
}
for(i = 0;i < n;i ++)
{
min = 1 << 30;
for(j = 1;j <= n;j ++)
{
if(!vis[j] && min > dist[j])
{
min = dist[j];
k = j;
}
}
vis[k] = 1;
if(min != 1 << 30)
sum += min;
for(j = 1;j <= n;j ++)
{
if(!vis[j] && dist[j] > map[k][j])
dist[j] = map[k][j];
}
}
printf("%d
",sum);
}
return 0;
}