Tree

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

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
思路:由于题目对能相连的点有限制,必须将这些点处理,能相连的点合并到一个集合中,最后查看是否所有点都在一个集合里,若都在说明是一个连通图,存在最小生成树,否则图不连通,不存在最小花费。
AC代码:
 
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
int CityHappy[],vis[];
int isprime[],dist[];
int map[][],n;
int father[],depth[];
void init_B()
{
int i;
for(i = ;i <= n;i ++)
{
father[i] = i;
depth[i] = ;
}
} int find(int x)
{
if(x == father[x])
return x;
return father[x] = find(father[x]);
} void unit(int x,int y)
{
x = find(x);
y = find(y);
if(x == y)
return ;
if(depth[x] > depth[y])
father[y] = x;
else
{
if(depth[x] < depth[y])
father[x] = y;
else
{
father[x] = y;
depth[y]++;
}
}
} void prime()
{
int i,j;
isprime[] = isprime[] = ;
for(i = ;i <= 1e6;i ++)
{
if(!isprime[i])
{
for(j = i << ;j <= 1e6;j += i)
isprime[j] = ;
}
}
} int judge(int a,int b)
{
if(!isprime[a] || !isprime[b])
return ;
if(!isprime[a+b])
return ;
return ;
} int min(int a,int b)
{
return a < b?a:b;
} void opration()
{
int i,j,a,b;
init_B();
for(i = ;i <= n;i ++)
{
for(j = ;j <= n;j ++)
{
if(i != j)
{
a = CityHappy[i];
b = CityHappy[j];
if(judge(a,b))
{
map[i][j] = min(min(a,b),abs(a-b));
map[j][i] = map[i][j];
unit(i,j);
}
else
map[i][j] = map[j][i] = << ;
}
}
}
} void init()
{
int i;
memset(vis,,sizeof(vis));
for(i = ;i <= n;i ++)
dist[i] = map[][i];
} int main()
{
int t,i,j,k,cnt,min,sum;
scanf("%d",&t);
prime();
while(t--)
{
sum = cnt = ;
scanf("%d",&n);
for(i = ;i <= n;i ++)
scanf("%d",&CityHappy[i]);
opration();
init();
for(i = ;i <= n;i ++)
{
if(i == find(i))
cnt++;
if(cnt == )
break;
}
if(cnt == )
{
printf("-1\n");
continue ;
}
for(i = ;i < n;i ++)
{
min = << ;
for(j = ;j <= n;j ++)
{
if(!vis[j] && min > dist[j])
{
min = dist[j];
k = j;
}
}
vis[k] = ;
if(min != << )
sum += min;
for(j = ;j <= n;j ++)
{
if(!vis[j] && dist[j] > map[k][j])
dist[j] = map[k][j];
}
}
printf("%d\n",sum);
}
return ;
}
 

Tree HDOJ--2682的更多相关文章

  1. hdoj 2682 Tree

    Tree Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. HDOJ 2682 Tree(最小生成树prim算法)

    Tree Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  3. HDOJ 1308.Is It A Tree?

    2015-07-15 问题简述: 给出一组节点关系,判断由这些节点组成的图是否为一颗树. 树只有一个根节点,每个节点只有一条边指向它,没有环. 原题链接:http://poj.org/problem? ...

  4. 【HDOJ 5379】 Mahjong tree

    [HDOJ 5379] Mahjong tree 往一颗树上标号 要求同一父亲节点的节点们标号连续 同一子树的节点们标号连续 问一共同拥有几种标法 画了一画 发现标号有二叉树的感觉 初始标号1~n 根 ...

  5. hdoj 4925 Apple tree 【最小割】

    题目:pid=4925">hdoj 4925 Apple tree 来源:2014 Multi-University Training Contest 6 题意:给出一个矩阵,然后每一 ...

  6. hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏

    three version are provided. disjoint set, linked list version with weighted-union heuristic, rooted ...

  7. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  8. 【HDOJ】4601 Letter Tree

    挺有意思的一道题,思路肯定是将图转化为Trie树,这样可以求得字典序.然后,按照trie的层次求解.一直wa的原因在于将树转化为线性数据结构时要从原树遍历,从trie遍历就会wa.不同结点可能映射为t ...

  9. 【HDOJ】1325 Is It A Tree?

    并查集.需要考虑入度. #include <stdio.h> #include <string.h> #define MAXNUM 10005 int bin[MAXNUM]; ...

  10. hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. Javascript 数组自定义排序,并获取排序后的保存原索引的同序数组(堆排序实现)

    比如数组A: [ 0: 5, 1: 2, 2: 4, 3: 3, 4: 1 ] 排序后的结果为:[1, 2, 3, 4, 5],但是有时候会有需求想要保留排序前的位置到一个同位数组里,如前例则为:[4 ...

  2. ES6的编码风格

    编程风格 [转自http://es6.ruanyifeng.com/#docs/style] 块级作用域 字符串 解构赋值 对象 数组 函数 Map结构 Class 模块 ESLint的使用 本章探讨 ...

  3. boa服务器问题日志

    1. 某一次在登录boa服务器的时候,不知哪里的问题,无法登录「192.168.1.0-192.168.3.255」网段的设备,但是公司IP网段的机器都可以用.最终发现,问题出现在自己的PC添加了浏览 ...

  4. Codevs 3731 寻找道路 2014年 NOIP全国联赛提高组

    3731 寻找道路 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在有向图G中,每条边的长度均为1,现给定起点和终点,请你在图中找 ...

  5. ORACLE数据库闪回日志写满

    网站页面无法显示完整.检查web服务是正常的,所以可能是ORACLE数据库出了问题. 首先检查闪回日志写满 然后检查归档日志文件写满的缘故了.使用以下几个命令可以看出当前归档日志文件的使用情况: se ...

  6. Linux的IO调度

    Linux的IO调度 IO调度发生在Linux内核的IO调度层.这个层次是针对Linux的整体IO层次体系来说的.从read()或者write()系统调用的角度来说,Linux整体IO体系可以分为七层 ...

  7. git 401 错误

    错误信息:error: The requested URL returned error: 401 Unauthorized while accessing https://git.oschina.n ...

  8. placeholder属性兼容js支持

    $(function(){ //判断浏览器是否支持placeholder属性 supportPlaceholder='placeholder'in document.createElement('in ...

  9. php内存管理

    1.为什么需要内存管理 由于计算机的内存由操作系统进行管理,所以普通应用程序是无法直接对内存进行访问的, 应用程序只能向操作系统申请内存,通常的应用也是这么做的,在需要的时候通过类似malloc之类的 ...

  10. uploadify插件的使用

    插件: uploadify.css jquery.uploadify.js bootstrap html代码: <input type="file" name="u ...