Tree
Time Limit : 6000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
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).
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
Source
2009浙江大学计算机研考复试(机试部分)——全真模拟
思路:Prim,这题数据有问题
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> using namespace std; int prime[2000040]; int number[1020]; int map[1020][1020]; int pre[1020]; const int MAXN = 100000000; bool p[1020]; int sum; int dist[1020]; int the_last_flag; int t,n; void Prim(int n,int dist[1020],int map[1020][1020],int pre[1020]) { int minn; for(int i = 2;i <= n;i ++) { p[i] = false; dist[i] = map[1][i]; pre[i] = 1; } dist[1] = 0; p[1] = true; for(int i = 1;i <= n - 1;i ++) { minn = MAXN; the_last_flag = 0; for(int j = 1;j <= n;j ++) { if(!p[j] && minn > dist[j]) { minn = dist[j]; the_last_flag = j; } } if(the_last_flag == 0) return ; p[the_last_flag] = true; for(int j = 1;j <= n;j ++) { if(!p[j] && map[the_last_flag][j] != MAXN && dist[j] > map[the_last_flag][j]) { dist[j] = map[the_last_flag][j]; pre[j] = the_last_flag; } } } } int main() { memset(prime,0,sizeof(prime)); prime[1] = 1; prime[0] = 1; for(int i = 2;i <= 2000020;i ++) for(int j = 2;i * j <= 2000020;j ++) prime[i * j] = 1; scanf("%d",&t); while(t --) { scanf("%d",&n); for(int i = 1;i <= n;i ++) scanf("%d",&number[i]); for(int i = 1;i <= n;i ++) for(int j = 1;j <= n;j ++) { if(i == j) map[i][j] = 0; else { if(prime[number[i]] == 0 || prime[number[j]] == 0 || prime[number[i] + number[j]] == 0) { //printf("%d %d ",number[i],number[j]); int x = min(number[i],number[j]); int y = abs(number[i] - number[j]); map[i][j] = min(x,y); //printf("%d %d %d ",i,j,map[i][j]); } else { map[i][j] = MAXN;map[j][i] = MAXN; } } } /*for(int i = 1;i <= n;i ++) { for(int j = 1;j <= n;j ++) printf("%d ",map[i][j]); printf(" "); }*/ the_last_flag = 0; sum = 0; Prim(n,dist,map,pre); if(the_last_flag == 0) printf("-1 "); else { for(int i = 1;i <= n;i ++) sum += dist[i]; printf("%d ",sum); } } return 0; }