hdoj 2682 Tree
Tree
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1861 Accepted Submission(s):
545
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.
cases.
Each case begin with a integer N,then N integer
Vi(0<=Vi<=1000000).
minimal cost,otherwise output "-1";
#include<stdio.h>
#include<math.h>
#define INF 0x3f3f3f
#define max 650
#include<string.h>
int lowdis[max],visit[max],map[max][max];
int city;
int sushu[1000100];
void prime()
{
int j,i,min,mindis=0,next;
memset(visit,0,sizeof(visit));
for(i=1;i<=city;i++)
{
lowdis[i]=map[1][i];
}
visit[1]=1;
for(i=2;i<=city;i++)
{
min=INF;
for(j=1;j<=city;j++)
{
if(!visit[j]&&min>lowdis[j])
{
next=j;
min=lowdis[j];
}
}
if(min==INF)
{
printf("-1\n");
return ;
}
visit[next]=1;
mindis+=min;
for(j=1;j<=city;j++)
{
if(!visit[j]&&lowdis[j]>map[next][j])
{
lowdis[j]=map[next][j];
}
}
}
printf("%d\n",mindis);
}
int min(int a,int b)
{
if(a>b)
a=b;
return a;
}
void biao()
{
int i,j;
memset(sushu,0,sizeof(sushu));
for(i=2;i<=1000100;i++)
{
if(!sushu[i])
{
for(j=i*2;j<=1000100;j+=i)
sushu[j]=1;
}
}
sushu[1]=1;
}
int main()
{
int n,i,j;
int a[max];
scanf("%d",&n);
biao();
while(n--)
{
scanf("%d",&city);
for(i=1;i<=city;i++)
{
scanf("%d",&a[i]);
for(j=1;j<=city;j++)
{
if(i==j)
map[i][j]=0;
else
map[i][j]=map[j][i]=INF;
}
}
for(i=1;i<=city;i++)
{
for(j=i+1;j<=city;j++)
{
if(!sushu[a[i]]||!sushu[a[j]]||!sushu[a[i]+a[j]])
{
map[i][j]=map[j][i]=min(min(a[i],a[j]),abs(a[i]-a[j]));
}
}
}
prime();
}
return 0;
}
hdoj 2682 Tree的更多相关文章
- HDOJ 2682 Tree(最小生成树prim算法)
Tree Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- HDOJ 3516 Tree Construction
四边形优化DP Tree Construction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDOJ 5044 Tree
树链剖分裸题. . .. 又要扩栈又要输入挂还卡格式....真无语 Tree Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 6553 ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- HDU 2682 Tree
题目: There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and ...
- HDU 2682 Tree(Kruskal算法求解MST)
题目: There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and ...
- HDOJ 3516 Tree Construction 四边形优化dp
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意: 大概就是给你个下凸包的左侧,然后让你用平行于坐标轴的线段构造一棵树,并且这棵树的总曼哈顿 ...
- HDOJ 1308.Is It A Tree?
2015-07-15 问题简述: 给出一组节点关系,判断由这些节点组成的图是否为一颗树. 树只有一个根节点,每个节点只有一条边指向它,没有环. 原题链接:http://poj.org/problem? ...
- 【HDOJ 5379】 Mahjong tree
[HDOJ 5379] Mahjong tree 往一颗树上标号 要求同一父亲节点的节点们标号连续 同一子树的节点们标号连续 问一共同拥有几种标法 画了一画 发现标号有二叉树的感觉 初始标号1~n 根 ...
随机推荐
- IText 生成简单表格(报表)doc文档 单元居中
IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下所示: import com.lowagie ...
- Shell中调用、引用、包含另一个脚本文件的三种方法
脚本 first (测试示例1) first#!/bin/bashecho 'your are in first file' 方法一:使用source #!/bin/bashecho 'your ar ...
- 172. Factorial Trailing Zeroes
题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)
Invitation Cards DescriptionIn the age of television, not many people attend theater performances. A ...
- python lambda 用法
可以视lambda为一个简易的函数,它不需要return,形式简单 #冒号左边是变量 #冒号右边是返回值 例: >>> def f (x): return x**2 ... > ...
- IE/Firefox每次刷新时自动检查网页更新,无需手动清空缓存的设置方法
浏览器都有自己的 缓存机制,一般CSS和图片都会被缓存在本地,这样我们修改的CSS就看不到效果 了,每次都去清空缓存,再刷新看效果,这样操作太麻烦了.在IE下我们可以直接 去修改internet选项/ ...
- python中的commands模块
commands模块用于调用shell命令 有3中方法: commands.getstatus() 返回执行状态 commands.getoutput() 返回执行结果 commands.ge ...
- poj 3278 Catch That Cow (bfs)
题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...
- EF Code First学习笔记 初识Code First
Code First是Entity Framework提供的一种新的编程模型.通过Code First我们可以在还没有建立数据库的情况下就开始编码,然后通过代码来生成数据库. 下面通过一个简单的示例来 ...
- 自杀程序&递归删除目录
自杀程序,删掉自身,包括执行程序所在的文件夹. 仅在windows下进行了测试,使用语言C# string fileName = Path.GetTempPath() + "killmyse ...