hdoj Tree

Tree

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

Total Submission(s): 1846    Accepted Submission(s): 540

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
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define MAX 2000020
#define inf 0x3f3f3f3f
using namespace std;
int isprime[MAX],map[610][610],low[610],v[610],vis[610];
int min(int a,int b){
    return a<b?a:b;
}
void count(){
    int i,j;isprime[1]=1;
    for(i=2;i*i<MAX;++i){
        if(isprime[i])continue;
        for(j=i*i;j<MAX;j+=i)
            isprime[j]=1;
    }
}
int prime(int n){
    int temp,result=0,i,j,pos=1;
    memset(vis,0,sizeof(vis));
    for(i=1;i<=n;++i)
        low[i]=map[pos][i];
    vis[pos]=1;
    for(j=1;j<n;++j){
        temp=inf;
        for(i=1;i<=n;++i){
            if(!vis[i]&&temp>low[i]){
                temp=low[i];pos=i;
            }
        }
        if(temp==inf)return -1;
        result+=temp;
        vis[pos]=1;
        for(i=1;i<=n;++i){
            if(!vis[i]&&low[i]>map[pos][i])
                low[i]=map[pos][i];
        }
    }
    return result;
}
int main()
{
    count();
    int t,i,n,m,j;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(i=1;i<=n;++i){
            scanf("%d",&v[i]);
        }
        memset(map,0x3f,sizeof(map));
        for(i=1;i<=n;++i){
            for(j=1;j<=n;++j){
                if(isprime[v[i]]==0||isprime[v[j]]==0||isprime[v[i]+v[j]]==0){
                    map[i][j]=min(min(v[i],v[j]),abs(v[i]-v[j]));
                }
            }
        }
        printf("%d\n",prime(n));
    }
    return 0;
}

时间: 2024-10-10 13:06:12

hdoj Tree的相关文章

【HDOJ 5379】 Mahjong tree

[HDOJ 5379] Mahjong tree 往一颗树上标号 要求同一父亲节点的节点们标号连续 同一子树的节点们标号连续 问一共有几种标法 画了一画 发现标号有二叉树的感觉 初始标号1~n 根结点1可以标1或n 否则其他情况无法让下面的子树满足各自连续并且该根的儿子节点都要连续 根结点下的节点平分其他标号 画一画可以发现 每个根下最多有两颗子树 否则无法满足条件 并且两颗子树占据剩余标号的左右两边 中间夹的必须是叶子 这样才能满足该根下的儿子节点标号连续 若根下只有一颗子树 同样可以选择占剩

hdoj 4786 Fibonacci Tree 【生成树+想法】

题目:hdoj 4786 Fibonacci Tree 题意:给出 n 个点 m 条边的图,边只有两种颜色,白色和黑色,让你判断能不能让一个生成树中白边的个数为斐波那契数. 分析:这是个想法题目,前提是知道生成树的定义:生成树必须是所有点都在树中 那么既然要是斐波那契数,我只要把白色边的最大个数和最小个数求出来,如果这个范围内有斐波那契数的话,那么就满足条件. 当然这样求的前提条件是期间的所有的生成树都是满足条件的.即都是满足能够生成树的. ok,AC代码: #include<iostream>

hdoj 1325 Is It A Tree? 【并查集】

做了一上午,终于ac了 wa了一次主要是忘了还有环!!! 主要是运用并查集知识,又复习了一次!! 思路:输入之后找能不能成环,成环就不是,其次还要判断是不是有两个父节点,如果有两个父节点也不是,之后就找相关的祖先就好了: 还要注意:如果只有一个节点,也是树,如果有两个或多个根节点也不是树:如果没有根节点也不是 链接http://acm.hdu.edu.cn/showproblem.php?pid=1325 代码 #include<stdio.h> int fat[1000]; int fath

【HDOJ 5834】Magic boy Bi Luo with his excited tree(树型DP)

[HDOJ 5834]Magic boy Bi Luo with his excited tree(树型DP) Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Bi Luo is a magic boy, he also has a migic tree,

HDOJ 题目4408 Minimum Spanning Tree(Kruskal+Matrix_Tree)

Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1408    Accepted Submission(s): 450 Problem Description XXX is very interested in algorithm. After learning the Prim algori

【HDOJ】1325 Is It A Tree?

并查集.需要考虑入度. 1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAXNUM 10005 5 6 int bin[MAXNUM]; 7 int degree[MAXNUM]; 8 int nums[MAXNUM]; 9 10 int find(int x) { 11 int r = x; 12 13 while (bin[r] != r) 14 r = bin[r]; 15 16 return r; 17 } 1

HDOJ 题目3966 Aragorn&#39;s Story(Link Cut Tree成段加减点权,查询点权)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5505    Accepted Submission(s): 1441 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

hdoj 4925 Apple tree 【最小割】

题目:hdoj 4925 Apple tree 来源:2014 Multi-University Training Contest 6 题意:给出一个矩阵,然后每一个格子中的数是2^(相邻格子的个数),然后要求不能取相邻的数,让取得数最大. 分析:这个题目有两种解法,一共是通解.网络流,还有一种是找规律,因为题目中数据是有规律的,所以能够找规律.非常多人是这样做的. 以下给出网络流的解法,事实上就是一个方格取数问题. 就是hdoj 1569 点击打开链接 的版本号,仅仅只是数据范围增大了.只是数

Is It A Tree?------HDOJ杭电1325(两种方法,可以用也可以不用并查集!!!!!!详解)

Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the