hdoj 2682 Tree(最小生成树)

Tree

http://acm.hdu.edu.cn/showproblem.php?pid=2682

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1855    Accepted Submission(s): 544

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

Source

2009浙江大学计算机研考复试(机试部分)——全真模拟

 

//最小生成树  标准版

#include<cstdio>

#include<cmath>

#include<cstring>

#include<cstdlib>

#include<algorithm>

using namespace std;

const int M=1000001;

int pre[M];

int n[M];

int prime[M];

struct node

{

int start;

int end;

int value;

}num[M];

int find(int x)

{

return pre[x]==x?x:pre[x]=find(pre[x]);

}

void merge(int x,int y)

{

int fx=find(x);

int fy=find(y);

if(fx!=fy)

pre[fx]=fy;

}

int cmp(node a,node b)

{

return a.value<b.value;

}

int main()

{

memset(prime,0,sizeof(prime));

__int64 i,j;

prime[0]=prime[1]=1;

for(i=2;i<1001;i++)

if(!prime[i])

for(j=i*i;j<M;j+=i)

prime[j]=1;

int t;

scanf("%d",&t);

while(t--)

{

int city,k;

scanf("%d",&city);

for(i=1;i<=city;i++)

{

scanf("%d",&n[i]);

pre[i]=i;

}

k=0;

memset(num,0,sizeof(num));

for(i=1;i<=city;i++)

for(j=i+1;j<=city;j++)

if(!prime[n[i]]||!prime[n[j]]||!prime[n[i]+n[j]])

{

num[k].start=i;

num[k].end=j;

num[k].value=min(min(n[i],n[j]),abs(n[i]-n[j]));

k++;

}

sort(num,num+k,cmp);

__int64 ans=0;

for(i=0;i<k;i++)

{

int fx=find(num[i].start);

int fy=find(num[i].end);

if(fx!=fy)

{

merge(fx,fy);

ans+= num[i].value;

}

}

int count=0;

for(i=1;i<=city;i++)

{

if(pre[i]==i)

count++;

if(count>1)

break;    

    }

if(count>1)

printf("-1\n");

else

printf("%I64d\n",ans);

}

return  0;

}

时间: 2024-10-13 17:57:59

hdoj 2682 Tree(最小生成树)的相关文章

HDOJ 2682 Tree(最小生成树prim算法)

Tree Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1954    Accepted Submission(s): 573 Problem Description There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities

hdu 2682 Tree 最小生成树~~~~水题一枚,,用到了筛法求素数,我竟然在格式上面PE了两次!!

Tree Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1754    Accepted Submission(s): 509 Problem Description There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities

HDU 4786 Fibonacci Tree 最小生成树变形

思路: 这题比赛的时候想了好久,最后队友机智的想到了. 不过那时不是我敲的,现在敲的1A. 想好就容易了. 直接把1或者0当做边的权值,然后按边从小到大排序,然后算最小生成用到了几条白边,然后再按边从大到小排序,然后再算白边用了几条.然后最小和最大需要用到的白边都算出来了.如果在这最小最大区间中存在那个啥数列的话就是Yes,否则就是No. 为什么在这区间里面就是对的呢?刚开始我也想了好久,然后发现,因为白边权值是1,然后黑边是0,然后假设用到白边最小的是6,最大的是10,那么,我们可以用黑边去替

{POJ}{3925}{Minimal Ratio Tree}{最小生成树}

题意:给定完全无向图,求其中m个子节点,要求Sum(edge)/Sum(node)最小. 思路:由于N很小,枚举所有可能的子节点可能情况,然后求MST,memset()在POJ里面需要memory头文件. #include <iostream> #include <vector> #include <map> #include <cmath> #include <memory> #include <algorithm> #includ

HDU 4408 Minimum Spanning Tree 最小生成树计数

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX

hdu 4786 Fibonacci Tree ( 最小生成树 )

Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2487    Accepted Submission(s): 796 Problem Description Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him t

HDOJ 3516 Tree Construction

四边形优化DP Tree Construction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 868    Accepted Submission(s): 470 Problem Description Consider a two-dimensional space with a set of points (xi, yi) t

[HDOJ4786]Fibonacci Tree 最小生成树

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 先跑一遍最小生成树,注意判断是否已全部联通(用一个记号来统计最后生成树中有多少条边).再记下最小生成树的权值和A. 再反向排序,求一遍最大生成树.记下权值和B.问题转换成求[A,B]内是否有斐波那契数存在. 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <

hdu 4786 Fibonacci Tree (最小生成树扩展)

///白边优先和黑边优先做两次最小生成树 ///若有斐波那契树在这中间为yes # include <stdio.h> # include <algorithm> # include <iostream> # include <string.h> # include <math.h> using namespace std; struct node { int x; int y; int v; }; struct node a[100010];