hdu 2682 Tree 最小生成树~~~~水题一枚,,用到了筛法求素数,我竟然在格式上面PE了两次!!

Tree

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1754    Accepted Submission(s): 509

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

转载请申明:http://blog.csdn.net/lionel_d

题目给的输出样例很具有迷惑性啊!!竟然只有一个换行,o(╯□╰)o

好啊,是我笨

看代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 650
#define INF 1000000000

int graph[MAX][MAX] , value[MAX];
bool NotPrime[2001000] ;
int prim(int n)
{
	int lowCost[MAX];
	bool visited[MAX] ;
	memset(visited,false,sizeof(visited)) ;
	int sum = 0 ;
	for(int i = 0 ; i < n ; ++i)
	{
		lowCost[i] = graph[0][i] ;
	}
	visited[0] = true ;
	for(int i = 0 ; i < n-1 ; ++i)
	{
		int index = -1 , min = INF ;
		for(int j = 0 ; j < n ; ++j)
		{
			if(!visited[j] && lowCost[j]<min)
			{
				index = j ;
				min = lowCost[j] ;
			}
		}
		if(index == -1)
		{
			if(i < n-1)
			{
				return INF ;
			}
			break ;
		}
		sum += min ;
		visited[index] = true ;
		for(int j = 0 ; j < n ; ++j)
		{
			if(!visited[j] && lowCost[j] > graph[index][j])
			{
				lowCost[j] = graph[index][j] ;
			}
		}
	}
	return sum ;
}

int min(int a , int b)
{
	return a>b?b:a ;
}

int main()
{
	int t ;
	NotPrime[0] = NotPrime[1] = true ;
	for(int i = 2 ; i < 2001000/2 ; ++i)
	{
		for(int j = 2 ; j*i < 2001000 ; ++j)
		{
			NotPrime[j*i] = true ;
		}
	}
	scanf("%d",&t);
	while(t--)
	{
		int n ;
		scanf("%d",&n);
		for(int i = 0 ; i < n ; ++i)
		{
			scanf("%d",&value[i]) ;
		}
		for(int i = 0 ; i < n ; ++i)
		{
			for(int j = 0 ; j < i ; ++j)
			{
				if(!NotPrime[value[i]] || !NotPrime[value[j]] || !NotPrime[abs(value[i]+value[j])])
				{
					graph[i][j] = graph[j][i] = min(min(value[i],value[j]),abs(value[i]-value[j]))  ;
				}
				else
				{
					graph[i][j] = graph[j][i] = INF ;
				}
			}
			graph[i][i] = 0 ;
		}
		int sum = prim(n) ;
		if(sum == INF)
		{
			puts("-1");
		}
		else
		{
			printf("%d\n",sum) ;
		}
	}
	return 0 ;
}

与君共勉

时间: 2024-12-26 02:25:20

hdu 2682 Tree 最小生成树~~~~水题一枚,,用到了筛法求素数,我竟然在格式上面PE了两次!!的相关文章

hdu 2053 Switch Game 水题一枚,鉴定完毕

Switch Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10200    Accepted Submission(s): 6175 Problem Description There are many lamps in a line. All of them are off at first. A series of op

简单的dp hdu 数塔(水题)

数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21314    Accepted Submission(s): 12808 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少

HDU 2090 算菜价 --- 水题

/* HDU 2090 算菜价 --- 水题 */ #include <cstdio> int main() { char s[105]; double a, b, sum = 0; while (scanf("%s", s)==1){ scanf("%lf%lf", &a, &b); a *= b; sum += a; } printf("%.1f\n", sum); return 0; }

HDU 2091 空心三角形 --- 水题

/* HDU 2091 空心三角形 --- 水题 */ #include <cstdio> int main() { int kase = 0; char ch; int h, t; //h表示高 while (scanf("%c", &ch) == 1 && ch != '@'){ scanf("%d", &h); if (kase++){ printf("\n"); } getchar(); if

水题一枚

Description Given a code (not optimized), and necessary inputs, you have to find the output of the code for the inputs. The code is as follows: int a, b, c, d, e, f; int fn( int n ) { if( n == 0 ) return a; if( n == 1 ) return b; if( n == 2 ) return

hdu 2212 DFS(水题)

DFS Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4923    Accepted Submission(s): 3029 Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every digit

蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)

算法训练 Torry的困惑(基本型) 时间限制:1.0s   内存限制:512.0MB 问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7--这样的数叫做质数.Torry突然想到一个问题,前10.100.1000.10000--个质数的乘积是多少呢?他把这个问题告诉老师.老师愣住了,一时回答不出来.于是Torry求助于会编程的你,请你算出前n个质数的乘积.不过,考虑到你才接触编程不久,Torry只要你算出这个数模上50000的值. 输入格式 仅包含一个正整数n,其中n<=100

hdu 4548 筛法求素数 打表

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4548 Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为“美素数”,如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数. 给定一个区间,你能计算出这个区间内有多少个美素数吗? Input 第一行输入一个正整数T,表示总共有T组数据

hdu 5007(字符串水题)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5007 Post Robot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 327    Accepted Submission(s): 253 Problem Description DT is a big fan of digital