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. luoguP1555 尴尬的数字(暴力+map)

    题意 题解 枚举每一个可能的二进制数.扔到一个map里 再枚举每一个可能的三进制数看map有没有就行了 反正就是很水 #include<iostream> #include<cstr ...

  2. [洛谷P3982]龙盘雪峰信息解析器

    题目大意:给你一串代码,要求进行解码.解码规则详见题目. 解题思路:这是一道字符串处理的题目. 首先,有这么几种情况输出Error: 1.代码中出现除了0和1外的字符. 2.代码长度不是8的倍数. 3 ...

  3. WebLogic 服务器配置

    环境版本    Windows 8.1       WebLogic 10.3.0     JDK:1.6 WebLogic 创建域在Windows环境下有两种方式: 1.直接在开始菜单创建domai ...

  4. 题解 洛谷 P3376 【【模板】网络最大流】

    本人很弱,只会Dinic.EK与Ford-Fulkerson...(正在学习ISAP...) 这里讲Dinic... Dinic:与Ford-Fulkerson和的思路相似(话说好像最大流求解都差不多 ...

  5. [Angular + TsLint] Disable directive selector tslint error

    @Directive({ // tslint:disable-next-line:directive-selector selector: '[scrollable]' })

  6. [MST] Use Volatile State and Lifecycle Methods to Manage Private State

    MST has a pretty unique feature: It allows you to capture private state on models, and manage this s ...

  7. hdoj--5562--Clarke and food(模拟)

    Clarke and food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. [P3097] [USACO13DEC] [BZOJ4094] 最优挤奶Optimal Milking 解题报告(线段树+DP)

    题目链接:https://www.luogu.org/problemnew/show/P3097#sub 题目描述 Farmer John has recently purchased a new b ...

  9. vuejs scope

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. OpenGL编程(三)让矩形动起来

    上次实现了在窗口中添加一个了一个矩形.这次的任务是在上次代码的基础上,让那个矩形动起来. 1.思路 要看到动态的效果,首先添加一个定时器,规定的时间刷新一次窗口:不断修改矩形的位置,使其在时间轴上达到 ...