http://acm.hdu.edu.cn/showproblem.php?pid=2682
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const int N=610;
const int M=2000000;
const int MaxDist=2000002;
int mat[N][N],dist[N],n;
bool visit[N];
int s[M],carry[M];
void plan()//把2000000内的素数找出来
{
int i,j;
fill(s,s+M,0);
s[1]=1;
for(i=2;i<=sqrt(double(M));i++)
{
if(!s[i])
{
for(j=i*i;j<=M;j+=i)
s[j]=1;
}
}
}
bool prime(int a)//判断是不是素数
{
return s[a]==0;
}
int Min(int a,int b)
{
return a<b?a:b;
}
void prim()
{
int i,j,sum=0,carry1,carry2=0,cnt=0;
for(i=0;i<n;i++)
{
dist[i]=mat[i][0];
visit[i]=false;
}
visit[0]=true;
for(i=0;i<n;i++)
{
carry1=MaxDist;
for(j=0;j<n;j++)
{
if(carry1>dist[j]&&visit[j]==false)
{
carry1=dist[j];
carry2=j;
}
}
if(carry1==MaxDist)break;
visit[carry2]=true;
sum+=carry1;
for(j=0;j<n;j++)
{
if(dist[j]>mat[carry2][j]&&visit[j]==false)
{
dist[j]=mat[carry2][j];
}
}
}
for(i=0;i<n;i++)
{
if(visit[i]==false)break;
}
if(i<n)sum=-1;
cout<<sum<<endl;
}
int main()
{
plan();
int i,j,T;
cin>>T;
while(T--)
{
cin>>n;
fill(&mat[0][0],&mat[n][n]+1,MaxDist);
for(i=0;i<n;i++)cin>>carry[i];
for(i=0;i<n;i++)//按照题意把距离放入数组
{
for(j=0;j<n;j++)
{
if(prime(carry[i])||prime(carry[j])||prime(carry[i]+carry[j]))
{
mat[i][j]=mat[j][i]=Min(Min(carry[i],carry[j]),abs(carry[i]-carry[j]));
}
}
}
prim();
}
return 0;
}