• HDOJ 2682 Tree


    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.
     

    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
     
       最小生成树,用素数筛法打表,判断素数就可以了

    #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
    ");
                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
    ",sum);
        }
        return 0;
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    从0开始学FreeRTOS-(创建任务)-2
    从0开始学FreeRTOS-1
    linux(ubuntu)系统mysql-5.7 修改字符集
    腾讯云服务器简单环境配置
    linux系统ubuntu18.04安装mysql(5.7)
    ubuntu18.04从零开始配置环境(jdk+tomcat+idea)到使用idea开发web应用和servlet
    Eclipse为工具包关联源码(本例工具包为dom4j-1.6.1)
    关于c#(vs)dategridview控件继承不能修改的问题
    C语言写单链表的创建、释放、追加(即总是在最后的位置增加节点)
    c++邻接表存储图(无向),并用广度优先和深度优先遍历(实验)
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4771096.html
Copyright © 2020-2023  润新知