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. canvas 连线曲线图

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name=& ...

  2. Linux 和 Windows 双系统时间同步问题 修改注册表

    路径:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation 1:新建  >> DWORD(32 b ...

  3. Mysql学习总结(26)——MySQL子查询

    mysql中虽然有连接查询实现多表连接查询,但是连接查询的性能很差,因此便出现了子查询. 1.理论上,子查询可以出现在查询语句的任何位置,但实际应用中多出现在from后和where后.出现在from后 ...

  4. 2016 10 26考试 NOIP模拟赛 杂题

    Time 7:50 AM -> 11:15 AM 感觉今天考完后,我的内心是崩溃的 试题 考试包 T1: 首先看起来是个贪心,然而,然而,看到那个100%数据为n <= 2000整个人就虚 ...

  5. 国庆 day 1 下午

    一道图论好题(graph) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图, ...

  6. XUtils3框架的基本用法(一)

    本文为作者原创,转载请指明出处: http://blog.csdn.net/a1002450926/article/details/50341173 今天给大家带来XUtils3的基本介绍.本文章的案 ...

  7. Jquery控件jrumble

    <!DOCTYPE HTML> <html>  <head>   <title>demo.html</title>   <meta h ...

  8. Find or Query Data with the mongo Shell

    https://docs.mongodb.com/getting-started/shell/query/ Overview You can use the find() method to issu ...

  9. 【DNN引用包】

    <%@ Register TagPrefix="dnn" TagName="address" Src="~/controls/address.a ...

  10. XML字符串解析实体类方法

    /// <summary> /// XML字符串解析实体类方法 /// </summary> public class StringXML { public StringXML ...