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. python 性能优化

    1.优化循环 循环之外能做的事不要放在循环内,比如下面的优化可以快一倍 2.使用join合并迭代器中的字符串 join对于累加的方式,有大约5倍的提升 3.使用if is 使用if is True比i ...

  2. java打印菱形的简单方法

    代码: public class Diamond { /** * 测试main方法 */ public static void main(String[] args) { printDiamond(1 ...

  3. 格而知之3:Core Data的基本使用

    最近准备做一个随手笔记类的app给自己用,考虑到从未使用过Core Data,就决定用Core Data来做数据存储.在网上参考了一些Core Data的资料后,用一天的时间写了这个demo,主要测试 ...

  4. 设计模式入门之职责链模式Chain Of Responsibility

    //职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. //实例:申请费用的功能,不同金额的费 ...

  5. 7. Shell 脚本编写

    一.Shell 脚本编写 1.提示用户输入一个字符串,如果是 hello,打出 yes,并每秒输出 "hello,world",否则就输出 no,实现如下: #!/bin/bash ...

  6. c++中各种数据类型所占字节

    求各种数据类型所占用的字节数可调用sizeof函数,求各种数据类型的最大值可以调用limits标准库中的numeric_limits<T>::max(),numeric_limits< ...

  7. Eclipse MyEclipse 复制项目 复制现有项目 复制功能相似项目

    如果现在已经存在一个Java Web项目 ProjectA,现在想做另外一个项目,里面绝大部分功能和结构都可以复用,如果想通过复制的方法来,那么可以这么做: 1.到资源管理器中,将ProjectA文件 ...

  8. jsp基础之 jstl

    JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. JSTL支持通用的.结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签. 除了这些,它还提供 ...

  9. Linux操作系统报:read-only file system

    在对集群测试过程中发现系统中某一节点中的磁盘变成read-only file system,从而导致测试任务出错,从网上查找资料,找到以下解决方案: 这个报错的意思是硬盘属性变成只读,不可写入: VO ...

  10. asp.net 获取当前url地址

    设当前页完整地址是:http://www.jb51.net/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名 "www.jb5 ...