题目:

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(Kruskal算法求解MST)的更多相关文章

  1. HDU 5253 连接的管道(Kruskal算法求解MST)

    题目: 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行灌溉了.当老 J ...

  2. ZOJ 1586 QS Network(Kruskal算法求解MST)

    题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...

  3. POJ 1251 Jungle Roads(Kruskal算法求解MST)

    题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...

  4. HDU 1863 畅通工程(Prim算法求解MST)

    题目: 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本.现 ...

  5. HDU 2682 Tree

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

  6. POJ 1258 Agri-Net(Prim算法求解MST)

    题目链接: http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...

  7. ZOJ 1203 Swordfish(Prim算法求解MST)

    题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...

  8. hdu 2988(最小生成树 kruskal算法)

    Dark roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. 克鲁斯卡尔(Kruskal)算法求最小生成树

    /* *Kruskal算法求MST */ #include <iostream> #include <cstdio> #include <cstring> #inc ...

随机推荐

  1. Ajax 访问 或 获取 IIS 虚拟目录

    使用场景 最近用 .net core mvc 写了一个工具类的项目,作为我们项目的后台管理网站使用.第一次被老大拿去部署的时候被告知不可用,同样的代码在我电脑和我的iis上都可以使用的啊. 后来才知道 ...

  2. [ASP.NET].NET逻辑分层架构总结

    一.基础知识准备: 1.层的原则: (1)每一层以接口方式供上层调用. (2)上层只能调用下层. (3)依赖分为松散交互和严格交互两种. 2.业务逻辑分类: (1)应用逻辑. (2)领域逻辑. 3.采 ...

  3. Android studio项目预览的时候提示错误ActionBarOverlayLayout

    android studio打开项目(别人的demo),提示页面没法预览.截图如下 根据查询,是主题没法正常显示,需要修改样式.样式文件的路径为res\values\styles.xml,截图如下. ...

  4. .Net Core + NGINX跳转登录时端口丢失

    使用.Net Core + NGINX部署到服务器的时候,如果端口不是使用默认的80端口,在跳转到登录页面时,URL中的端口丢失. NGINX的配置如下: server { listen ; loca ...

  5. Windows Phone中解决多模块多程序集之间相互循环引用的问题一种思路

    Windows Phone中解决多模块多程序集之间相互循环引用的问题一种思路 那就是利用mvvmlight中的messager组件(可单独提取出),制作双向的一个消息发送.通过公共的类的定义,来传递数 ...

  6. RabbitMQ之消息持久化(转)

    原文地址 https://blog.csdn.net/u013256816/article/details/60875666/ 消息的可靠性是RabbitMQ的一大特色,那么RabbitMQ是如何保证 ...

  7. 安装 rabbitmq ,通过生成器获取redis列表数据 与 Celery 分布式异步队列

    一.安装rabbitmq  @全体成员 超简易安装rabbitmq文档 1.安装配置epel源rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/ ...

  8. 【ocp新题】OCP 12c 062认证考试出现大量新题-8

    8. Which are two ways for a database service to be recognized by a listener in Oracle Database 12c? ...

  9. Python面向对象(类的成员之方法)

    day24 类的成员之方法 - 普通方法,保存在类中,由对象来调用,self > 对象 - 静态方法,保存在类中,由类直接调用 - 类方法,保存在类中,由类直接调用,cls > 当前类 c ...

  10. 正则表达式学习之grep,sed和awk

    正则表达式是用于描述字符排列和匹配模式的一种语法,它主要用于字符串的模式分割.匹配.查找以及替换操作. 描述一个正则表达式需要字符类.数量限定符.位置限定符.规定一些特殊语法表示字符类,数量限定符和位 ...