Tree

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

Total Submission(s): 2107    Accepted Submission(s): 610

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2677 2683 2678 2676 2681

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define INF 0xfffffff
#define min(a,b)(a>b?b:a)
int map[1010][1010],p[1000010*2],mark[1010],num[1010];
int n;
void fun()
{
int i,j;
p[1]=1;
for(i=2;i<1000010*2;i++)
{
if(!p[i])
{
for(j=i+i;j<1000010*2;j+=i)
{
p[j]=1;
}
}
}
}
int prim()
{
int sum=0,p=n,i,j;
int flog;
memset(mark,0,sizeof(mark));
while(--p)
{
int min=INF;
for(i=2;i<=n;i++)
{
if(!mark[i]&&map[1][i]<min)
{
min=map[1][i];
flog=i;
}
}
if(min==INF)
break;
sum+=min;
mark[flog]=1;
for(j=2;j<=n;j++)
{
if(!mark[j]&&map[1][j]>map[flog][j])
map[1][j]=map[flog][j];
}
}
if(p) return -1;
else
return sum;
}
int main()
{
int t;
fun();
scanf("%d",&t);
while(t--)
{
int i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=INF;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(!p[num[i]]||!p[num[j]]||!p[num[i]+num[j]])
{
map[j][i]=map[i][j]=min(min(num[i],num[j]),abs(num[i]-num[j]));
}
}
}
printf("%d\n",prim());
}
return 0;
}

hdoj--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. hdoj 2682 Tree

    Tree Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

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

    Tree Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. Device Tree(二):基本概念

    转自:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制是用 ...

  5. 2015暑假多校联合---Mahjong tree(树上DP 、深搜)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...

  6. Device Tree(三):代码分析【转】

    转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...

  7. 【转】Device Tree(二):基本概念

    原文网址:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制 ...

  8. 图的最短路径问题————树上奶牛(tree.cpp)

    和往常一样,继续从题目引入 树上奶牛 (tree.cpp) [题目描述] 农夫John的奶牛不是住在地上而是住在树上的QWQ. 奶牛之间需要串门,不过在串门之前他们会向John询问距离的大小.可是Jo ...

  9. HDURevenge of Segment Tree(第二长的递增子序列)

    HDURevenge of Segment Tree(第二长的递增子序列) 题目链接 题目大意:这题是求第二长的递增子序列. 解题思路:用n^2的算法来求LIS,可是这里还要记录一下最长的那个序列是否 ...

  10. JQuery Easy Ui (Tree树)详解(转)

    第一讲:JQuery Easy Ui到底是什么呢? 首先咱们知道JQuery是对Java Script的封装,是一个js库,主要提供的功能是选择器,属性修改和事件绑定等等.. JQuery ui是在j ...

随机推荐

  1. HDU Integer's Power(容斥原理)

    题意 求[l,r]的最大指数和(1<=l,r<=10^18) 最大指数和(如64=8^2=4^3=2^6,所以64的最大指数和是6) 题解 很明显我们可以先求出[1,n]的最大指数和,然后 ...

  2. python 面向对象 继承

    什么是继承 继承表达的是一种”是“的关系,比如人是动物 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类 继承是基于抽象的结 ...

  3. [Typescript] Installing Promise Type Definitions Using the lib Built-In Types

    To fix Promise is not recolized in TypeScript, we can choose to use a lib: npm i @types/es6-promise ...

  4. UVa 10954 Add All 贪心

    贪心   每一次取最小的两个数,注意相加的数也要算' #include<cstring> #include<iostream> #include<cstdio> # ...

  5. phpcms v9会员推荐位

    模版显示推荐会员代码 <div class="zhanxun2">             <div class="title">   ...

  6. Python图像处理库PIL的ImageStat模块介绍

    ImageStat模块用于计算整个图像或者图像的一个区域的统计数据. 一.ImageStat模块的函数 1.  Stat 定义1:ImageStat.Stat(image)⇒ Stat instanc ...

  7. Windows安装PHP MongoDB扩展

    本文将讲述一下在Wamp环境下安装MongoDB扩展的过程,大家可以略作参考 Linux 版本的可以参考之前发布的Linux安装PHP MongoDB扩展 安装环境 系统环境:Windows 7 64 ...

  8. CUDA中的归约

    CUDA编程实战书中的乘方和解决办法: 对一个数组执行某种计算,然后产生一个更小的结果数组. 由一个线程在共享内存上进行迭代并计算出总和值.而如果用并行,所花时间就与数组长度的对数成正比. 代码的思想 ...

  9. YYDispatchQueuePool阅读笔记

    阅读了开源大神的YYDispatchQueuePool,在此记下一些从中学到的东西. 首先YYDispatchQueuePool.m文件中有如下类型对应: static inline dispatch ...

  10. <Sicily>Greatest Common Divisors

    一.题目描述 A common divisor for two positive numbers is a number which both numbers are divisible by. It ...