题目:

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.

InputThe first will contain a integer t,followed by t cases. 
Each case begin with a integer N,then N integer Vi(0<=Vi<=1000000).OutputIf 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
题意描述:
题目还是有点意思的,但是还是改变不了使水题的本质。
输入结点的个数以及每个结点的权值
计算并输出有特殊要求的最小生成树
解题思路:
属于最小生成树问题,按照数据的格式,使用Kruskal算法。
代码实现:
 #include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct edge
{
int u,v,w;
};
struct edge e[*];//大数组申请全局变量
int f[];
bool cmp(struct edge x,struct edge y)
{
return x.w<y.w;
}
int getf( int v);
int merge(int c,int u);
int isp(int n); int main()
{
int t,i,n,c[],j,k,count,sum;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&c[i]);
k=;
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
if(isp(c[i]) || isp(c[j]) || isp(c[i]+c[j]))
{
e[k].u=i;
e[k].v=j;
e[k++].w = min(min(c[i],c[j]),abs(c[i]-c[j]));
}
}
}
sort(e+,e+k,cmp);
for(i=;i<=n;i++)
f[i]=i;
count = ;
sum=;
for(i=;i<k;i++)
{
if( merge(e[i].u,e[i].v) )
{
count++;
sum += e[i].w;
}
if(count == n-)
break;
} if(count == n-)
printf("%d\n",sum);
else
printf("-1\n");
}
return ;
} int getf( int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
int merge(int v,int u)
{
int t1,t2;
t1=getf(v);
t2=getf(u);
if(t1 != t2)
{
f[t2]=t1;
return ;
}
return ;
}
int isp(int n)
{
if(n==)//1不是素数
return ;
int k,i;
k=(int)sqrt(n);
for(i=;i<=k;i++)
{
if(n % i==)
return ;
}
return ;
}

易错分析:

1、程序直接强制结束,可能是数组开的太大,放在全局变量的位置就可以了。

2、判断素数的过程中,1不是素数(代码功力)

HDU 2682 Tree的更多相关文章

  1. HDU 2682 Tree(Kruskal算法求解MST)

    题目: There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and ...

  2. hdu 5909 Tree Cutting [树形DP fwt]

    hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...

  3. HDU 5044 Tree(树链剖分)

    HDU 5044 Tree field=problem&key=2014+ACM%2FICPC+Asia+Regional+Shanghai+Online&source=1&s ...

  4. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  5. HDU 4757 Tree(可持久化Trie+Tarjan离线LCA)

    Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Su ...

  6. HDU 4757 Tree 可持久化字典树

    Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...

  7. HDU 6228 - Tree - [DFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6228 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  8. HDU 5909 Tree Cutting 动态规划 快速沃尔什变换

    Tree Cutting 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5909 Description Byteasar has a tree T ...

  9. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

随机推荐

  1. 学习时用的软件最新 开发环境为Visual Studio 2010,数据库为SQLServer2005,使用.net 4.0开发。 超市管理系统

    一.源码特点 1.采用典型的三层架构进行开发.模板分离,支持生成静态 伪静态..购物车.登陆验证.div+css.js等技术二.功能介绍 1.本源码是一个超市在线购物商城源码,该网上商城是给超市便利店 ...

  2. Visual studio code离线安装插件

    Visual studio code离线安装插件 公司研发区不能连接公网,使用Visual studio code(vsc)写Golang代码需要安装Go插件,下面介绍下,vsc离线安装插件的步骤.以 ...

  3. php array_walk

    PHP array_walk() 函数 对数组中的每个元素应用用户自定义函数: <?php function myfunction($value,$key) { echo "The k ...

  4. 怎么制作html5网站页面让它适应电脑和手机的尺寸

    https://zhidao.baidu.com/question/918130826792192539.html 用以下代码开头:<!DOCTYPE HTML><html>& ...

  5. class, classloder, dex 详解

    class与dex文件 什么是class文件 class文件是一种能够被JVM识别,加载并且执行的文件格式. class文件的作用 class文件的作用是记录一个类文件的所有信息. 例如记住了当前类的 ...

  6. 理解纯CSS画三角形

    pure css draw a triangle code { display: inline-block; width: 300px; background-color: #E0E0E0 } .te ...

  7. Pycharm使用总结

    1.代码整体向右移动 按住Win+TAB可以快速向右缩进一个tab 的距离,按住Shift + TAB反方向前进一个TAB距离 2.Model加入get,set 方法 在编辑框中右击,选择genera ...

  8. webapi框架搭建-创建项目(三)-webapi owin

    上一篇:创建项目(二) 在上一篇里,我们已经创建好了webapi应用,并已经部署到iis里,本篇讲如何用owin自宿主或是iis宿主来部署webapi应用. owin介绍 传统的asp.net网站只能 ...

  9. Pyqt4的事件与信号

    事件: 事件(Event)是GUI程序中很重要的一部分.它由用户或系统产生.当我们调用程序的exec_方法时,程序就会进入主循环中.主循环捕获事件并将它们发送给相应的对象进行处理. 信号与槽:     ...

  10. python之list

    1.python列表 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推 序列都可以进行的操作包括索引,切片,加,乘, ...