Tree
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4160 Accepted Submission(s): 1255
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
写了好长时间,能通过,但是残忍的超时了,发现,超时的原因可能是花在了判断素数上面
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<queue>
#define NUM 666
struct Node{
int number;
int parent;
int value;
} city[NUM];
using namespace std;
int isPrime( int num )
{
int tmp =sqrt(num);
for(int i= 2;i<=tmp; i++)
if(num %i== 0)
return 0 ;
return 1 ;
}
int findRoot(int index){
if(city[index].parent==0)
return index;
city[index].parent = findRoot(city[index].parent);
return city[index].parent;
}
int triMin(int a, int b){
int min=a;
if(min > b)
min = b;
if(min > abs(a-b))
min = abs(a-b);
return min;
}
int main()
{
int n,T,i,j,minValue,temp,index_i,index_j,totalVal;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%d", &n);
for(i=1; i<=n; i++){
scanf("%d",&city[i].value);
city[i].number = i;
city[i].parent = 0;
}
totalVal=0;
//选择能组成素数的,不在一棵树上面的,最近的
while(1){
minValue=-1;
for(i=2; i<=n; i++){
for(j=1; j<i; j++){
if(findRoot(i)!=findRoot(j)){
if(isPrime(city[i].value) || isPrime(city[j].value) || isPrime(city[i].value + city[j].value)){
temp = triMin(city[i].value, city[j].value);
if(minValue==-1 || minValue>temp){
minValue=temp;
index_i=i;
index_j=j;
}
}
}
}
}
if(minValue==-1)
break;
temp = findRoot(index_i);
city[temp].parent = findRoot(index_j);
totalVal+=minValue;
}
temp=0;
for(i=1; i<=n; i++)
if(city[i].parent==0)
temp++;
if(temp>1){
printf("-1\n");
}else{
printf("%d\n", totalVal);
}
}
//fclose(stdin);
return 0;
}
尝试打表:
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<queue>
#define NUM 666
#define DM 1000000
struct Node{
int number;
int parent;
int value;
} city[NUM];
using namespace std;
int prime[DM];
void dabiao()
{
int i,j;
memset(prime,0,sizeof(prime));//素数为0
for(i=2;i<DM;i++)
{
if(!prime[i])
{
for(j=2*i;j<DM;j+=i)
{
prime[j]=1;
}
}
}
prime[1]=1;
}
int findRoot(int index){
if(city[index].parent==0)
return index;
city[index].parent = findRoot(city[index].parent);
return city[index].parent;
}
int triMin(int a, int b){
int min=a;
if(min > b)
min = b;
if(min > abs(a-b))
min = abs(a-b);
return min;
}
int main()
{
int n,T,i,j,minValue,temp,index_i,index_j,totalVal;
freopen("in.txt","r",stdin);
scanf("%d",&T);
dabiao();
while(T--){
scanf("%d", &n);
for(i=1; i<=n; i++){
scanf("%d",&city[i].value);
city[i].number = i;
city[i].parent = 0;
}
totalVal=0;
//选择能组成素数的,不在一棵树上面的,最近的
while(1){
minValue=-1;
for(i=2; i<=n; i++){
for(j=1; j<i; j++){
if(findRoot(i)!=findRoot(j)){
//if(isPrime(city[i].value) || isPrime(city[j].value) || isPrime(city[i].value + city[j].value)){
if(prime[city[i].value]==0 || prime[city[j].value]==0 || prime[city[i].value + city[j].value==0]){
temp = triMin(city[i].value, city[j].value);
if(minValue==-1 || minValue>temp){
minValue=temp;
index_i=i;
index_j=j;
}
}
}
}
}
if(minValue==-1)
break;
temp = findRoot(index_i);
city[temp].parent = findRoot(index_j);
totalVal+=minValue;
}
temp=0;
for(i=1; i<=n; i++)
if(city[i].parent==0)
temp++;
if(temp>1){
printf("-1\n");
}else{
printf("%d\n", totalVal);
}
}
//fclose(stdin);
return 0;
}
Time Limit Exceeded
原因:两层for循环只遴选了一条最短路径,导致,特别低耗
正确的做法是选出所有的边,然后,一个sort快排,再组合成题目的条件
大牛的:https://blog.csdn.net/chenzhenyu123456/article/details/44594901