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

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <vector>
  6. #include <queue>
  7. #include <cstdlib>
  8. #include <iomanip>
  9. #include <cmath>
  10. #include <ctime>
  11. #include <map>
  12. #include <set>
  13. using namespace std;
  14. #define lowbit(x) (x&(-x))
  15. #define max(x,y) (x>y?x:y)
  16. #define min(x,y) (x<y?x:y)
  17. #define MAX 100000000000000000
  18. #define MOD 1000000007
  19. #define pi acos(-1.0)
  20. #define ei exp(1)
  21. #define PI 3.141592653589793238462
  22. #define ios() ios::sync_with_stdio(false)
  23. #define INF 0x3f3f3f3f
  24. #define mem(a) (memset(a,0,sizeof(a)))
  25. typedef long long ll;
  26. int g[][];
  27. int vis[],n;
  28. int dis[],x,y,z;
  29. int val[],t;
  30. int ans[];
  31. void get_prime()
  32. {
  33. memset(ans,,sizeof(ans));
  34. ans[]=;
  35. for(int i=;i<;i++)
  36. {
  37. if(ans[i]) continue;
  38. for(int j=;j*i<;j++)
  39. {
  40. ans[j*i]=;
  41. }
  42. }
  43. }
  44. void init()
  45. {
  46. for(int i=;i<=n;i++)
  47. {
  48. for(int j=;j<i;j++)
  49. {
  50. g[i][j]=g[j][i]=INF;
  51. }
  52. g[i][i]=;
  53. }
  54. }
  55. int prime()
  56. {
  57. for(int i=;i<=n;i++)
  58. {
  59. dis[i]=g[][i];
  60. vis[i]=;
  61. }
  62. vis[]=;
  63. int minn,v=,sum=;
  64. for(int i=;i<n;i++)
  65. {
  66. minn=INF;
  67. for(int j=;j<=n;j++)
  68. {
  69. if(!vis[j] && minn>dis[j])
  70. {
  71. minn=dis[j];
  72. v=j;
  73. }
  74. }
  75. if(minn==INF) return -;
  76. vis[v]=;
  77. sum+=minn;
  78. for(int j=;j<=n;j++)
  79. {
  80. if(!vis[j]) dis[j]=min(dis[j],g[v][j]);
  81. }
  82. }
  83. return sum;
  84. }
  85. int main()
  86. {
  87. get_prime();
  88. scanf("%d",&t);
  89. while(t--)
  90. {
  91. scanf("%d",&n);
  92. init();
  93. for(int i=;i<=n;i++)
  94. {
  95. scanf("%d",&val[i]);
  96. for(int j=;j<=i;j++)
  97. {
  98. if(!ans[val[i]] || !ans[val[j]] || !ans[val[i]+val[j]])
  99. g[i][j]=g[j][i]=min(g[i][j],min(min(val[i],val[j]),abs(val[i]-val[j])));
  100. }
  101. }
  102. printf("%d\n",prime());
  103. }
  104. return ;
  105. }
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. Blink Coordinate Spaces

    Blink Coordinate Spaces Blink Coordinate Spaces Types of Zoom There are two types of zoom in Chromiu ...

  2. [JSOI2008]火星人 hash+splay

    题目描述: 现在,火星人定义了一个函数 LCQ(x, y)LCQ(x,y),表示:该字符串中第 xx 个字符开始的字串,与该字符串中第 yy 个字符开始的字串,两个字串的公共前缀的长度.比方说,LCQ ...

  3. Ubuntu(kali)开启mysql远程连接

    Linux 默认关闭mysql的远程连接,编辑 /etc/mysql/my.cnf 文件, 把里面的 bind-address = 127.0.0.1 改成 bind-address = 0.0.0. ...

  4. 初学javascript,写一个简单的阶乘算法当作练习

    代码如下: <script> var a = prompt("请输入值"); function mul(a){ if(a==1){ return 1; } return ...

  5. 百度地图API的使用示例

    刚刚工作的时候写过百度地图API文档,那时候没有记录到技术博客里面,今天在群里看见有个姑娘在问这个问题,重温了一遍,这个API还算好用.百度地图API,集成简单好用,全面,兼容问题,文档全面: 官方文 ...

  6. Unity Launcher类,轻松打开网页,照片,app 等

    using UnityEngine; using UnityEngine.WSA; public class test : MonoBehaviour { void Start () { //打开百度 ...

  7. 关于Windows7下创建Cocos2D-X项目的小问题

    "新版的Cocos2D-X"已经不支持用上述脚本来创建工程了,而是改为用create-project.py来创建...命令格式: python create-project.py ...

  8. uva 473(dp)

    题意:按创作时间给出n首歌每首歌的时间ti,然后按创作时间装到m个光盘内,给出光盘最大分钟数t,问m个光盘最多总共放多少首歌. 题解:对于每首歌都能够选或者不选,假设选择了这首歌,是否把这首歌当做第j ...

  9. UVa 11849 - CD

    题目:给你两个有序序列(每一个序列中元素不同),求两序列中都出现的元素个数. 分析:简单题. 合并排序合并过程. 设置两个指针.指向两序列当前元素.那个元素小指针向后移动.相同大则计数加一,同一时候后 ...

  10. 面试基础_03实现strcpy、strcat、strcmp、strlen

    实现代码例如以下: /************************************************************************* > File Name: ...