Tree

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2877    Accepted Submission(s): 883

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

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int g[][];
int vis[],n;
int dis[],x,y,z;
int val[],t;
int ans[];
void get_prime()
{
memset(ans,,sizeof(ans));
ans[]=;
for(int i=;i<;i++)
{
if(ans[i]) continue;
for(int j=;j*i<;j++)
{
ans[j*i]=;
}
}
}
void init()
{
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
g[i][j]=g[j][i]=INF;
}
g[i][i]=;
}
}
int prime()
{
for(int i=;i<=n;i++)
{
dis[i]=g[][i];
vis[i]=;
}
vis[]=;
int minn,v=,sum=;
for(int i=;i<n;i++)
{
minn=INF;
for(int j=;j<=n;j++)
{
if(!vis[j] && minn>dis[j])
{
minn=dis[j];
v=j;
}
}
if(minn==INF) return -;
vis[v]=;
sum+=minn;
for(int j=;j<=n;j++)
{
if(!vis[j]) dis[j]=min(dis[j],g[v][j]);
}
}
return sum;
}
int main()
{
get_prime();
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
init();
for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
for(int j=;j<=i;j++)
{
if(!ans[val[i]] || !ans[val[j]] || !ans[val[i]+val[j]])
g[i][j]=g[j][i]=min(g[i][j],min(min(val[i],val[j]),abs(val[i]-val[j])));
}
}
printf("%d\n",prime());
}
return ;
}
Sample Output
4
-1

必须保证va 或者 vb 或者 va+vb是素数

HDU 2689 Tree的更多相关文章

  1. hdu 2689

    hdu 2689 超级大水题....两种代码都过了,开始以为n^2会tle,后来竟然过了...汗 注意下cin写在while里面,就可以了 #include <iostream> usin ...

  2. hdu 5909 Tree Cutting [树形DP fwt]

    hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...

  3. HDU 5044 Tree(树链剖分)

    HDU 5044 Tree field=problem&key=2014+ACM%2FICPC+Asia+Regional+Shanghai+Online&source=1&s ...

  4. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  5. HDU 2689 Sort it (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe ...

  6. HDU 4757 Tree(可持久化Trie+Tarjan离线LCA)

    Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Su ...

  7. HDU 4757 Tree 可持久化字典树

    Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4757 Des ...

  8. hdu 2689 Sort it

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 题目分析:求至少交换多少次可排好序,可转换为逆序对问题. 用冒泡排序较为简单,复杂度较大~~ 也 ...

  9. HDU 2689 Sort it【树状数组】

    Sort it Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

随机推荐

  1. 大数问题(相加) A + B

    I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum o ...

  2. Conservative GC (Part two :MostlyCopyingGC )

    目录 MostlyCopyingGC 概要 堆结构 分配 new_obj()函数 add_pages()函数 GC执行过程 mostly_copying()函数 promote_page()函数 pa ...

  3. centos7 安装rsyslog

    http://blog.csdn.net/u011630575/article/details/50896781 http://blog.chinaunix.net/uid-21142030-id-5 ...

  4. typedef 与 set_new_handler的几种写法

    可以用Command模式.函数对象来代替函数指针,获得以下的好处: 1. 可以封装数据 2. 可以通过虚拟成员获得函数的多态性 3. 可以处理类层次结果,将Command与Prototype模式相结合 ...

  5. Draw the RGB data from kinect C++ via opengl

    In order to improve my English writing skills,I am going to  write the blogs in English form now! -- ...

  6. Android笔记三十四.Service综合实例二

    综合实例2:client訪问远程Service服务 实现:通过一个button来获取远程Service的状态,并显示在两个文本框中. 思路:如果A应用须要与B应用进行通信,调用B应用中的getName ...

  7. rgba

    正反两面展示效果 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...

  8. snowflake算法

    snowflake算法思考 缘起 为什么会突然谈到分布式唯一id呢?原因是最近在准备使用RocketMQ,看看官网介绍: 一句话,消息可能会重复,所以消费端需要做幂等.为什么消息会重复后续Rocket ...

  9. Linux下配置Squid基础教程

    Linux下配置Squid基础教程 本视频高清下载地址:http://down.51cto.com/data/437529 本文出自 "李晨光原创技术博客" 博客,请务必保留此出处 ...

  10. java command not found

    INSTALLATION PROCEDURE ON RASPBERRY PI The remaining steps should be performed directly on the conso ...