Tree

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

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
敲代码时有一个小地方没看到  结果一直RE纠结了俩小时,所以说要细心............
prime算法:
#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的更多相关文章

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

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

  2. HDOJ 3516 Tree Construction

    四边形优化DP Tree Construction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  3. HDOJ 5044 Tree

    树链剖分裸题. . .. 又要扩栈又要输入挂还卡格式....真无语 Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 6553 ...

  4. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

  5. HDU 2682 Tree

    题目: There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and ...

  6. HDU 2682 Tree(Kruskal算法求解MST)

    题目: There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and ...

  7. HDOJ 3516 Tree Construction 四边形优化dp

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3516 题意: 大概就是给你个下凸包的左侧,然后让你用平行于坐标轴的线段构造一棵树,并且这棵树的总曼哈顿 ...

  8. HDOJ 1308.Is It A Tree?

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

  9. 【HDOJ 5379】 Mahjong tree

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

随机推荐

  1. Android:使用ViewPager实现左右滑动切换图片(图上有点点)

    在以下实例的基础上加上点点 Android:使用ViewPager实现左右滑动切换图片 (简单版) 效果预览: 因为要把点点放图片上,所以修改布局为相对布局: <?xml version=&qu ...

  2. std::advance 给迭代器增加指定偏移量

    template <class InputIterator, class Distance> void advance (InputIterator& it, Distance n ...

  3. PLS-00103: 出现符号 ...

    Oracle存储过程: create or replace procedure update_people(in_name ), in_status in nvarchar2) as begin up ...

  4. objective-C 自定义对象归档的实现

    自定义对象要实现归档必须实现NSCoding协议 NSCoding协议有两个方法,encodeWithCoder方法对对象的属性数据做编码处理,initWithCoder解码归档数据来初始化对象. # ...

  5. hdu4631Sad Love Story(多校3)(最接近点对)

    http://acm.hdu.edu.cn/showproblem.php?pid=4631 比赛的时候搜到了最接近点对的求法 Nlog(N) 又估摸着依次插入求的话会TLE 想了想觉得可以先把最近的 ...

  6. poj 3041 Asteroids (二分图的最大匹配 第一题)

    题目:http://poj.org/problem?id=3041 题意:在某个n*n的空间内,分布有一些小行星,某人在里面打炮,放一枪后某一行或某一列的行星就都没了,让求最少的打炮数. 然后把每行x ...

  7. Innodb parent table open时导致crash

    case描述: innodb中,父表和子表通过foreign constraint进行关联, 因为在更新数据时需要check 外键constraint,如果父表被大量的子表reference, 那么在 ...

  8. sharedevelop iis express

    sharedevelop 的IIS express的配置文件在 %userprofile%\documents\IISExpress\config\applicationhost.config 自动会 ...

  9. 深入理解Java虚拟机 - 虚拟机内存划分

    在内存管理方面,Java相对于C和C++的区别在于Java具有内存动态分配以及垃圾收集技术,但平时我们很少去关注JVM的内存结构以及GC,在出现内存泄露或溢出方面的问题,排查工作将变得异常艰难.   ...

  10. LBS云端数据删除和上传

    这里采用C#模拟表单提交,实现LBS云端删除和csv格式文件的上传. 删除: /// <summary> /// 从LBS云端删除数据 /// </summary> /// & ...