Tree

Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 2
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
 题解:
就是给n个数,如果这个数跟另一个数的和或者这两个数有一个是素数就可以连接;权值为Min(Min(VA , VB),|VA-VB|).
注意要打素数表;
prime代码:
 #include<stdio.h>
#include <string.h>
#include<math.h>
#define Min(x,y) (x<y?x:y)
const int INF=0x3f3f3f3f;
const int MAXN=;
bool prim[];
int N,answer;
int judge(int a,int b){
if(!prim[a]||!prim[b]||!prim[a+b]){
return Min(Min(a,b),fabs(a-b));
}
else return -;
}
void pt(){
memset(prim,false,sizeof(prim));
prim[]=prim[]=true;
for(int i=;i<=;i++){
if(!prim[i])for(int j=i*i;j<;j+=i){
prim[j]=true;
}
}
}
int map[MAXN][MAXN],vis[MAXN],low[MAXN];
int v[MAXN];
void prime(){
int k;
int temp,flot=;
answer=;
memset(vis,,sizeof(vis));
vis[]=;
for(int i=;i<N;i++)low[i]=map[][i];
for(int i=;i<N;i++){
temp=INF;
for(int j=;j<N;j++)
if(!vis[j]&&temp>low[j])
temp=low[k=j];
if(temp==INF){
if(flot==N)printf("%d\n",answer);
else puts("-1");
break;
}
answer+=temp;
vis[k]=;
flot++;
for(int j=;j<N;j++)
if(!vis[j]&&low[j]>map[k][j])
low[j]=map[k][j];
}
}
int main(){
int T,t;
scanf("%d",&T);
while(T--){
pt();
memset(map,INF,sizeof(map));
scanf("%d",&N);
for(int i=;i<N;i++)scanf("%d",&v[i]);
for(int i=;i<N;i++)
for(int j=i+;j<N;j++){
t=judge(v[i],v[j]);
if(t>=){
if(t<map[i][j])map[i][j]=map[j][i]=t;
}
}
prime();
}
return ;
}

Tree(prime)的更多相关文章

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

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

  2. easyUI之Tree(树)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  3. 2021.07.19 BZOJ2654 tree(生成树)

    2021.07.19 BZOJ2654 tree(生成树) tree - 黑暗爆炸 2654 - Virtual Judge (vjudge.net) 重点: 1.生成树的本质 2.二分 题意: 有一 ...

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

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

  5. Device Tree(三):代码分析

    一.前言 Device Tree总共有三篇,分别是: 1.为何要引入Device Tree,这个机制是用来解决什么问题的?(请参考引入Device Tree的原因) 2.Device Tree的基础概 ...

  6. Linux内核Radix Tree(二)

    1.   并发技术 由于需要页高速缓存是全局的,各进程不停的访问,必须要考虑其并发性能,单纯的对一棵树使用锁导致的大量争用是不能满足速度需要的,Linux中是在遍历树的时候采用一种RCU技术,来实现同 ...

  7. Linux内核Radix Tree(一)

    一.概述 Linux radix树最广泛的用途是用于内存管理,结构address_space通过radix树跟踪绑定到地址映射上的核心页,该radix树允许内存管理代码快速查找标识为dirty或wri ...

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

    原文网址:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html 一.前言 Device Tree总共有三篇,分别是: 1.为何要引入De ...

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

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

随机推荐

  1. 精美的 ( Android, iPhone, iPad ) 手机界面设计素材和线框图设计工具

    在制作界面原型的时候,如果有现成的界面基础元素可以使用的话,设计师就可以非常快速的完成原型的制作,能够节省大量的时间和精力.在这篇文章, 我向大家分享45套非常有用的 UI 和 Wireframe 套 ...

  2. JavaWeb——文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  3. OGG常见问题处理

    1403: ORA-01403: No data found 在运行PL/SQL块.存储过程.函数.触发器等,假设须要进行操作的记录没有查询到.则会返回1403的错误 Goldengate中的1403 ...

  4. mysql 分库分表的方法

    分表后怎么做全文搜索 1.merge方式分表(不好) 2. 使用 sql union 3 使用Sphinx全文检索引擎 一,先说一下为什么要分表 当一张的数据达到几百万时,你查询一次所花的时间会变多, ...

  5. .NET MySQL的参数化查询

    MySqlConnection conn = new MySqlConnection(SqlConnnectString); MySqlCommand cmd = new MySqlCommand(& ...

  6. PHP学习笔记六【方法-递归】

    <?php //递归 global $n;//定义全局变量 function abc($n) { if($n>2) { abc(--$n); } echo '$n='.$n.'<br ...

  7. Log4j.properties配置详细解读

    Log4j.properties配置 Log4j有三个主要的组件:Loggers(记录器),Appenders  (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以 ...

  8. php非递归无限级分类.

    项目需要.递归无限级分类效率实在太低.理了半天思路写的. 分类越多效率越高. /** * 单次循环返回无限极分类嵌套 * @param array $data 操作的数组 * @param strin ...

  9. 用纯CSS3绘制萌系漫画人物动态头像

    大家已经见惯了用CSS3画的图标.LOGO.头像,这次台湾同学Rei给我们带来了用纯CSS3绘制的日本动漫<轻音少女>女主角秋山澪的动态头像.看到动图我震惊了!!!CSS3的强大再次霸气测 ...

  10. 转:说说JSON和JSONP

    前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socke ...