zoukankan      html  css  js  c++  java
  • Tree

    Tree

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


    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
     

    Author
    Teddy
     

    Source
     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2680 2677 2683 2678 2676 





                                               
    #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]));
    				}
    			}
    		}
    		/*for(i=1;i<=n;i++)
    		for(j=1;j<=n;j++)
    		printf("%d ",map[i][j]);*/
    		printf("%d
    ",prim());
    	}
    	return 0;
    }
    


  • 相关阅读:
    Android 模拟系统事件(三)
    全民Scheme(2):来自星星的你
    Java经典23种设计模式之行为型模式(三)
    libmysqld,嵌入式MySQLserver库
    闲云控制台(一)控制台命令解析框架
    怎样改动android系统字体大小
    [多校2015.02.1006 高斯消元] hdu 5305 Friends
    换工作经历和心得
    安卓实训第七天---多线程下载实现(进度条)
    校园双选会,你都懂么
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273843.html
Copyright © 2011-2022 走看看