HDOJ 2682 Tree
来源:互联网 发布:韩国网络爆红65g 编辑:程序博客网 时间:2024/05/16 19:52
Tree
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2037 Accepted Submission(s): 590
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
#include<stdio.h>#include<string.h>#include<stdlib.h>int CityHappy[605],vis[605];int isprime[1000005],dist[605];int map[601][601],n;int father[605],depth[605];void init_B(){ int i; for(i = 1;i <= n;i ++) { father[i] = i; depth[i] = 0; }}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[0] = isprime[1] = 1; for(i = 2;i <= 1e6;i ++) { if(!isprime[i]) { for(j = i << 1;j <= 1e6;j += i) isprime[j] = 1; } }}int judge(int a,int b){ if(!isprime[a] || !isprime[b]) return 1; if(!isprime[a+b]) return 1; return 0;}int min(int a,int b){ return a < b?a:b;}void opration(){ int i,j,a,b; init_B(); for(i = 1;i <= n;i ++) { for(j = 1;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] = 1 << 30; } } }}void init(){ int i; memset(vis,0,sizeof(vis)); for(i = 1;i <= n;i ++) dist[i] = map[1][i];}int main(){ int t,i,j,k,cnt,min,sum; scanf("%d",&t); prime(); while(t--) { sum = cnt = 0; scanf("%d",&n); for(i = 1;i <= n;i ++) scanf("%d",&CityHappy[i]); opration(); init(); for(i = 1;i <= n;i ++) { if(i == find(i)) cnt++; if(cnt == 2) break; } if(cnt == 2) { printf("-1\n"); continue ; } for(i = 0;i < n;i ++) { min = 1 << 30; for(j = 1;j <= n;j ++) { if(!vis[j] && min > dist[j]) { min = dist[j]; k = j; } } vis[k] = 1; if(min != 1 << 30) sum += min; for(j = 1;j <= n;j ++) { if(!vis[j] && dist[j] > map[k][j]) dist[j] = map[k][j]; } } printf("%d\n",sum); } return 0;}
0 0
- hdoj 2682 Tree
- HDOJ--2682--Tree
- HDOJ 2682 Tree
- hdoj--2682--Tree()
- HDOJ 2682 Tree 【prime】【kruskal】
- hdoj Tree
- hdoj 2682 Tree(最小生成树)
- hdoj 2682 Tree 【最小生成树】
- 【treap tree】 HDOJ 4006
- HDOJ 4925 Apple Tree
- HDOJ 3516 Tree Construction
- HDOJ 4786 Fibonacci Tree
- HDOJ 5044 Tree
- HDOJ 5044 Tree
- 【HDOJ 5379】 Mahjong tree
- HDOJ 5498 Tree
- hdoj 4786 Fibonacci Tree
- Hdoj 4786 Fibonacci Tree
- 学习《算法导论》第六章 堆排序 总结二
- Android Binder 机制
- Html5 Blob
- leetcode 234 Palindrome Linked List(难易度:Easy)
- HDU 2035 人见人爱A^B(水~)
- HDOJ 2682 Tree
- C++各数据类型取值范围
- 讯飞离线语音合成(语记|语音+)
- Android中插件开发篇之----动态加载Activity(免安装运行程序)
- healthkit简介
- 如何设置hive与hbase同步
- UFFS文件系统简介
- 查看Android应用包名package和入口activity名称
- Android网络编程之使用get方式向服务端提交数据和乱码问题的解决