Tree(prime)
Tree
Time Limit : 6000/2000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 2
Now we want to connecte all the cities together,and make the cost minimal.
#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)的更多相关文章
- Device Tree(三):代码分析【转】
转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...
- easyUI之Tree(树)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 2021.07.19 BZOJ2654 tree(生成树)
2021.07.19 BZOJ2654 tree(生成树) tree - 黑暗爆炸 2654 - Virtual Judge (vjudge.net) 重点: 1.生成树的本质 2.二分 题意: 有一 ...
- Device Tree(二):基本概念
转自:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制是用 ...
- Device Tree(三):代码分析
一.前言 Device Tree总共有三篇,分别是: 1.为何要引入Device Tree,这个机制是用来解决什么问题的?(请参考引入Device Tree的原因) 2.Device Tree的基础概 ...
- Linux内核Radix Tree(二)
1. 并发技术 由于需要页高速缓存是全局的,各进程不停的访问,必须要考虑其并发性能,单纯的对一棵树使用锁导致的大量争用是不能满足速度需要的,Linux中是在遍历树的时候采用一种RCU技术,来实现同 ...
- Linux内核Radix Tree(一)
一.概述 Linux radix树最广泛的用途是用于内存管理,结构address_space通过radix树跟踪绑定到地址映射上的核心页,该radix树允许内存管理代码快速查找标识为dirty或wri ...
- 【转】Device Tree(三):代码分析
原文网址:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html 一.前言 Device Tree总共有三篇,分别是: 1.为何要引入De ...
- 【转】Device Tree(二):基本概念
原文网址:http://www.wowotech.net/linux_kenrel/dt_basic_concept.html 一.前言 一些背景知识(例如:为何要引入Device Tree,这个机制 ...
随机推荐
- UVA 11111-Generalized Matrioshkas(栈)
题意:有很多层盒子,盒子里面再套盒子,一个盒子可能套多个独立的子盒子,但子盒子的总体积必须小于该盒子,否则不合法,输入给一行数,负数代表左边,正数代表右边,大小表示其体积,如-2,-1,1,2则表示体 ...
- Perl Symbolic Reference
看一些模块的代码,很多时候通过*glob的方式来改变变量或者函数,这种方法称为Symbolic reference. 首先看一下*glob的结构,这个在之前的博文已经讲过,不做细述: SV = PVG ...
- C#执行zip文件压缩的几种方法及我遇到的坑总结
工作项目中需要用到zip压缩解压缩文件,一开始看上了Ionic.Zip.dll这个类库,操作方便,写法简单 对应有个ziphelper类 using Ionic.Zip; public static ...
- MapReduce程序依赖的jar包
难得想写个mapreduce程序.发现已经不记得须要加入那些jar包了,网上找了一会也没发现准确的答案.幸好对hadoop体系结构略知一二.迅速试出了写mapreduce程序须要的五个jar包. 不多 ...
- Qt开始学习的一些问题
1.需要将qmake.moc和qvfb的路径加入到系统的环境变量: qmake.moc:export PATH=$PATH:/usr/local/Trolltech/QtEmbedded-4.6.1- ...
- 【Java基础】构造方法调用构造方法
从一个程序开始: class dog { private String name; private String color; private int age; dog(String name) // ...
- CSS的base文件常用代码
article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body,but ...
- Best Time to Buy and Sell Stock (java)
有一组数组代表股票的价格 一次买一次卖如何得到最大利润? public int maxProfit(int[] prices) { if(prices.length==0)return 0; int ...
- Reverse Words in a String (JAVA)
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- ios9基础知识(技能篇)
NSFileManager.NSURL.NSFileHandle.NSData.NSXMLParser.NSUserDefaults.NSKeyedArchiver.NSKeyedUnarchiver ...