• hdu 3371 Connect the Cities(最小生成树)


    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371

    984ms风险飘过~~~

    /************************************************************************/
    /*     
            hdu  Connect the Cities
            最小生成树
            题目大意:最小生成树,题目很长,题意很简单就是最小生成树。关键是构建图
    */
    /************************************************************************/
    
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    
    #define MAX 0xfffffff
    
    const int N = 501;
    int map[N][N];
    int vis[N];
    int mark[N];
    int num,n;
    
    void build_map()
    {
        int m,k;
        int q,p,c;
        int t;
    
        scanf("%d%d%d",&n,&m,&k);
        for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j++)
        map[i][j] = map[j][i] = ((i==j)?0:MAX);
    
        for (int i = 0; i < m; i++)
        {
            scanf("%d%d%d",&p,&q,&c);
            if (map[p][q] > c)
            {
                map[p][q] = map[q][p] = c;
            }
            
        }
        while(k--)
        {
            scanf("%d",&t);
            for (int i = 0; i < t; i++)
            {
                scanf("%d",&mark[i]);
                for (int j = 0; j < i; j++)
                {
                    map[mark[j]][mark[i]] = map[mark[i]][mark[j]] = 0;
                }
            }
        }
    }
    
    int prim()
    {
        int t = n;
        int sum = 0;
        int min,k;
        vis[0] = 1;
        while(--t)
        {
            min = MAX;
            for(int i = 2; i <= n; i++)
            {
                if (vis[i] != 1 && map[1][i] < min)
                {
                    min = map[1][i];
                    k = i;
                }
            }
            if(min == MAX)break;
            vis[k] = 1;
            sum += min;
            for (int i = 2; i <= n; i++)
            {
                if (vis[i] != 1 && map[k][i] < map[1][i])
                map[1][i] = map[k][i];
            }
        }
        return t==0?sum:-1;
    }
    
    
    int main()
    {
        scanf("%d",&num);
        while(num--)
        {
            build_map();
            memset(vis,0,sizeof(vis));
            printf("%d
    ",prim());
        }
        return 0;
    }
  • 相关阅读:
    基于java对doc文档的分词,导入数据库
    软件架构 软件质量属性(淘宝)
    c/c++ Java python
    阅读架构漫谈心得体会
    关于ubuntu16.4在vmware中打开只显示桌面,桌面闪烁的问题
    暑假总结07
    暑假总结06
    RandomStringUtils
    spring-data-elasticsearch
    Socket和ObjectOutputStream问题
  • 原文地址:https://www.cnblogs.com/newpanderking/p/3253094.html
Copyright © 2020-2023  润新知