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是素数

最新文章

  1. form表单 ----在路上(15)
  2. jq 构造函数,然后再表单提交过程中对数据进行修改
  3. [Node.js] OAuth 2 和 passport框架
  4. Multiple reportviewers on one page With reportviwer 11.0
  5. 【object-c基础】Object-c基础之三:面对对象开发@interface,@implementation
  6. C++小知识之Vector排序
  7. SignalR1
  8. SpringMVC+Spring+hibernate整合及分页
  9. jQuery中的常用内容总结(二)
  10. Spring Boot会员管理系统——处理文件上传
  11. [Swift]LeetCode1022. 从根到叶的二进制数之和 | Sum of Root To Leaf Binary Numbers
  12. The Best Books on Game Dev
  13. git 使用命令删除远程分支和本地分支
  14. 【CF1141E】Superhero Battle
  15. Python3.6下的Requests登录及利用Cookies登录
  16. 利用windows的计划任务和eKing.CmdReadFileAndSendEmailOper(控制台小程序)实现远程登录服务器的邮件告警提醒
  17. jsp数据库开发
  18. Bootstrap中datetimepicker日期控件1899年问题解决
  19. XMLHttpRequest HTTP请求的返回码为0 http status = 0
  20. Spring Cloud 概述

热门文章

  1. Linux 设置交换分区
  2. HDU-2050 折线分割平面 找规律&amp;递推
  3. django 在非空的字段里插入现象表述
  4. 【Henu ACM Round#20 E】Star
  5. 二 HTable 源码导读
  6. 网页加速之Chromium 预载入 Prerendering
  7. Struts2 全局结果集
  8. RequestMapping、Responsebody、RequestBody
  9. 深入Vue的响应式原理
  10. oracle 高水位线问题