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. HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)

    原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...

  2. House Robber II 解答

    Question After robbing those houses on that street, the thief has found himself a new place for his ...

  3. zoj 3471 Most Powerful(状态压缩dp)

    Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These ato ...

  4. 在php添加mongo过程中出现的mongo.so: > undefined symbol: php_json_encode in Unknown on line 0. After installation mongo driver for php 的错误

    3 down vote my system is centos 6.3. I got the problem solved. vim /etc/php.ini then add extension=j ...

  5. Back to Underworld(搜索)

    Back to Underworld Time Limit:4000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Su ...

  6. Ice_cream's world I

    Ice_cream's world I Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  7. linux内存操作----kernel 3.5.X copy_from_user()和copy_to_user()

    前面的一篇文章中简单的描写叙述了一下内存映射的内容,http://blog.csdn.net/codectq/article/details/25658813,这篇文章作为用户把内存规划好之后,在用户 ...

  8. js库开发--参数传递及方法修改

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  9. Mongoose的使用

    最近想做一个练手的App小项目.考虑到数据接口的问题,因为关系型数据库用的比较多,也有一定经验了,所以打算使用比较火的MongoDB作为数据库,下面就介绍一下Mongoose的使用方法吧. 概念:Mo ...

  10. PHP学习笔记八【数组】

    <?php //定义数组 $hens[0]=3; $hens[1]=5; $hens[2]=1; $hens[3]=3.4; $hens[4]=2; $hens[5]=50; //遍历整个数组 ...