TreeTime Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2037 Accepted Submission(s): 590
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<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\n");
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\n",sum);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。