hdoj--2682--Tree()
来源:互联网 发布:淘宝卖视频教程赚钱吗 编辑:程序博客网 时间:2024/05/16 12:31
Tree
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2107 Accepted Submission(s): 610
Total Submission(s): 2107 Accepted Submission(s): 610
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.
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).
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
251234544444
Sample Output
4-1
Author
Teddy
Source
2009浙江大学计算机研考复试(机试部分)——全真模拟
Recommend
lcy | We have carefully selected several similar problems for you: 2677 2683 2678 2676 2681
#include<stdio.h>#include<string.h>#include<stdlib.h>#define INF 0xfffffff#define min(a,b)(a>b?b:a)int map[1010][1010],p[1000010*2],mark[1010],num[1010];int n;void fun(){ int i,j; p[1]=1; for(i=2;i<1000010*2;i++) { if(!p[i]) { for(j=i+i;j<1000010*2;j+=i) { p[j]=1; } } }}int prim(){ int sum=0,p=n,i,j; int flog; memset(mark,0,sizeof(mark)); while(--p) { int min=INF; for(i=2;i<=n;i++) { if(!mark[i]&&map[1][i]<min) { min=map[1][i]; flog=i; } } if(min==INF) break; sum+=min; mark[flog]=1; for(j=2;j<=n;j++) { if(!mark[j]&&map[1][j]>map[flog][j]) map[1][j]=map[flog][j]; } } if(p) return -1; else return sum;}int main(){ int t; fun(); scanf("%d",&t); while(t--) { int i,j; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&num[i]); for(i=1;i<=n;i++) for(j=1;j<=n;j++) map[i][j]=INF; for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(!p[num[i]]||!p[num[j]]||!p[num[i]+num[j]]) { map[j][i]=map[i][j]=min(min(num[i],num[j]),abs(num[i]-num[j])); } } } printf("%d\n",prim()); } return 0;}
0 0
- hdoj--2682--Tree()
- hdoj 2682 Tree
- HDOJ--2682--Tree
- HDOJ 2682 Tree
- HDOJ 2682 Tree 【prime】【kruskal】
- HDOJ 题目2682 Tree(最小生成树)
- HDOJ 2682 Tree(最小生成树--prime)
- hdoj 2682 Tree(最小生成树+打表)
- hdoj Tree
- hdoj 2682 Tree(最小生成树)
- hdoj 2682 Tree 【最小生成树】
- hdoj tree 5606 (并查集)
- 【treap tree】 HDOJ 4006
- HDOJ 4925 Apple Tree
- HDOJ 3516 Tree Construction
- HDOJ 4786 Fibonacci Tree
- HDOJ 5044 Tree
- HDOJ 5044 Tree
- java截取字符串,截串,substring和split,分割字母和数字,正则缝隙
- 如何保证在IE和火狐等主流浏览器让图片在块级元素中左右,上下居中显示
- 读书笔记--Activity的启动
- Android仿ios7TabBar
- java web 防止表单重复提交解决方案
- hdoj--2682--Tree()
- 关于private成员、protected成员、public成员调用问题
- 欢迎使用CSDN-markdown编辑器
- java.util.Date和java.sql.Date的区别和相互转化
- 每天学一点linux(7)
- VB.NET小总
- hdu4686 Arc of Dream
- 基于CLGeocoder - 地理编码
- 浅谈Android视图动画中的坐标系问题