hdoj 2682 Tree 【最小生成树】
来源:互联网 发布:阿里云 pptp 无法连接 编辑:程序博客网 时间:2024/06/05 16:01
Tree
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2099 Accepted Submission(s): 605
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
打表
代码:
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<math.h>#define M 650#define INF 0x3f3f3fusing namespace std;int a[M];int judge[1000010];int vis[M];int low[M];int map[M][M];int n;void dabiao()//´ò±í£» {int i,j;memset(judge,0,sizeof(judge));for(i=2;i<1000010;i++){if(!judge[i]){for(j=i*2;j<1000010;j+=i){judge[j]=1;}}}judge[1]=1;}void init(){int i,j;for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(i==j)map[i][j]=0;elsemap[i][j]=INF;}}}void prim(){int min,i,j,next,sum=0;memset(vis,0,sizeof(vis));for(i=1;i<=n;i++){low[i]=map[1][i];}vis[1]=1;for(i=1;i<n;i++){min=INF;next=1;for(j=1;j<=n;j++){if(!vis[j]&&min>low[j]){min=low[j];next=j;}}if(min==INF){printf("-1\n");return ;}sum+=min;vis[next]=1;for(j=1;j<=n;j++){if(!vis[j]&&low[j]>map[next][j]){low[j]=map[next][j];}}}printf("%d\n",sum);}int main(){int T;int i,j;dabiao();scanf("%d",&T);while(T--){scanf("%d",&n);init();for(i=1;i<=n;i++){scanf("%d",&a[i]);}for(i=1;i<=n;i++){ for(j=i+1;j<=n;j++) {if(!judge[a[i]]||!judge[a[j]]||!judge[a[i]+a[j]]){map[i][j]=map[j][i]=min(min(a[i],a[j]),abs(a[i]-a[j]));}}}prim();}return 0;}
0 0
- hdoj 2682 Tree(最小生成树)
- hdoj 2682 Tree 【最小生成树】
- HDOJ 题目2682 Tree(最小生成树)
- hdoj.2682 Tree【最小生成树】 2015/03/27
- HDOJ 2682 Tree(最小生成树--prime)
- HDOJ 2682 Tree(最小生成树prim算法)
- hdoj 2682 Tree(最小生成树+打表)
- hdoj 4786 Fibonacci Tree 【最小生成树&&最大生成树】
- hdoj 4786 Fibonacci Tree 【最小生成树&&最大生成树】
- [HDOJ 4896] Minimal Spanning Tree [最小生成树]
- HDOJ 4786 Fibonacci Tree(最小生成树--kruskul)
- Tree 【最小生成树】
- hdoj 4786 Fibonacci Tree 【最小生成树 + 最大生成树】
- hdu 2682 Tree 最小生成树
- HDU 2682--Tree【最小生成树】
- HDOJ 2988 -------最小生成树
- hdoj 4408 Minimum Spanning Tree 求最小生成树的数目
- HDU2682 Tree 最小生成树
- 游标嵌套
- Programming assignment 3: Optimization and generalization 解答
- DSP Flash API步骤
- 14.2.2.2 Consistent Nonlocking Reads 一致性非锁定读
- Funny Comments
- hdoj 2682 Tree 【最小生成树】
- centos开机svn不会自动启动的解决办法
- Gradle 编译打印日志
- Handler.obtainMessage()
- Java远程连接操作linux服务器,scp获取文件
- Nginx的负载均衡方案详解
- STM32BSP文件编写
- Java虚拟机工作原理详解
- 简单工厂设计模式