标签:
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1861 Accepted Submission(s):
545
#include<stdio.h> #include<math.h> #define INF 0x3f3f3f #define max 650 #include<string.h> int lowdis[max],visit[max],map[max][max]; int city; int sushu[1000100]; void prime() { int j,i,min,mindis=0,next; memset(visit,0,sizeof(visit)); for(i=1;i<=city;i++) { lowdis[i]=map[1][i]; } visit[1]=1; for(i=2;i<=city;i++) { min=INF; for(j=1;j<=city;j++) { if(!visit[j]&&min>lowdis[j]) { next=j; min=lowdis[j]; } } if(min==INF) { printf("-1\n"); return ; } visit[next]=1; mindis+=min; for(j=1;j<=city;j++) { if(!visit[j]&&lowdis[j]>map[next][j]) { lowdis[j]=map[next][j]; } } } printf("%d\n",mindis); } int min(int a,int b) { if(a>b) a=b; return a; } void biao() { int i,j; memset(sushu,0,sizeof(sushu)); for(i=2;i<=1000100;i++) { if(!sushu[i]) { for(j=i*2;j<=1000100;j+=i) sushu[j]=1; } } sushu[1]=1; } int main() { int n,i,j; int a[max]; scanf("%d",&n); biao(); while(n--) { scanf("%d",&city); for(i=1;i<=city;i++) { scanf("%d",&a[i]); for(j=1;j<=city;j++) { if(i==j) map[i][j]=0; else map[i][j]=map[j][i]=INF; } } for(i=1;i<=city;i++) { for(j=i+1;j<=city;j++) { if(!sushu[a[i]]||!sushu[a[j]]||!sushu[a[i]+a[j]]) { map[i][j]=map[j][i]=min(min(a[i],a[j]),abs(a[i]-a[j])); } } } prime(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4507577.html