hdu2682

来源:互联网 发布:电子发票 用软件开 编辑:程序博客网 时间:2024/05/18 02:50

Tree

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


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
251234544444
 

Sample Output
4

-1

AC代码:

#include<stdio.h> #include<cmath>#include<math.h>#include<string.h>#include<stdlib.h>int vi[611];  int prime[2000111];  struct A  {          int a,b;          int len;  }eage[180000];  int tot;  //并查集  int set[611];  void build(int n)  {          int i;          for(i=0;i<n;i++)                 set[i]=i;  }  int find(int k)  {          if(set[k]==k)                  return k;          set[k]=find(set[k]);                  return set[k];  }  void Union(int f1,int f2)  {          set[f1]=f2;  }  int MIN(int a,int b)  {          return a>b?b:a;  }  int cmp(const void *a,const void *b)  {          struct A *c,*d;          c=(struct A *)a;          d=(struct A *)b;          return c->len-d->len;  }  int main()  {          int T,n,i,l,temp,f1,f2,cost;        memset(prime,-1,sizeof(prime));          prime[0]=prime[1]=0;          for(i=2;i<=2000000;i++)          {                  if(prime[i]==-1)                  {                          for(temp=2*i;temp<=2000000;temp+=i)                                  prime[temp]=0;                  }          }          scanf("%d",&T);          while(T--)          {                  scanf("%d",&n);                  for(i=0;i<n;i++)                         scanf("%d",&vi[i]);                  tot=0;                  for(i=0;i<n;i++)                  for(l=i+1;l<n;l++)                  {                          if(prime[vi[i]] || prime[vi[l]] || prime[vi[i]+vi[l]])                          {                                  eage[tot].a=i;                                  eage[tot].b=l;                                  eage[tot].len=MIN(vi[i],MIN(vi[l],abs(vi[i]-vi[l])));                                  tot++;                          }                  }                  qsort(eage,tot,sizeof(eage[0]),cmp);                  cost=0;                  build(n);                  for(i=0;i<tot;i++)                  {                          f1=find(eage[i].a);                          f2=find(eage[i].b);                          if(f1==f2)                                  continue;                          cost+=eage[i].len;                          Union(f1,f2);                  }                  temp=0;                  for(i=0;i<n;i++)                 if(set[i]==i)                          temp++;                  if(temp>1)                          printf("-1\n");                  else                             printf("%d\n",cost);          }          return 0;  }  

原创粉丝点击