• hdu 1002 prime 模板


    Constructing Roads

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 20927    Accepted Submission(s): 8023


    Problem Description
    There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

    We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
     
    Input
    The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

    Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
     
    Output
    You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
     
     
     多的一点就是 这里告诉你了哪些是已经建立好的 模拟一下prime的过程 每次都是把最近的点放进去 然后更新 那么我们把已经建立好点之间的距离设为0 那么这两个点便会优先入
    #include<cstdio>
    #include<iostream>
    #include<string.h>
    #include<cmath>
    #define maxn 105
    #define inf 9999999
    int vis[maxn],n;//用来表示点是否在集合里 
    int mincost[maxn];//用来记录从集合出发到每个点的最小距离 
    int mapp[maxn][maxn];
    using namespace std;
    void init()
    {
        for(int i=1;i<=n;i++) vis[i]=0;
        fill(mincost,mincost+n+1,inf);
    }
    void prim()
    {
        mincost[1]=0;
        int res=0;
        while(1)
        {
            int v=-1;
            for(int i=1;i<=n;i++)    if(!vis[i]&&(v==-1||mincost[i]<mincost[v])) v=i;//找出离集合最近的点 
            if(v==-1) break;
            vis[v]=1;
            res+=mincost[v];
            for(int i=1;i<=n;i++) mincost[i]=min(mincost[i],mapp[v][i]);//放入以后 更新
        }
        printf("%d
    ",res); 
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            init();
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    int x;
                    scanf("%d",&x);
                    mapp[i][j]=mapp[j][i]=x;
                }
            }
            int q;
            scanf("%d",&q);
            while(q--)
            {
                int a,b;
                scanf("%d %d",&a,&b);
                mapp[a][b]=0;
                mapp[b][a]=0;
            //    vis[a]=1;
            //    vis[b]=1;
            //    for(int i=1;i<=n;i++) mincost[i]=minn(mincost[i],mapp[a][i]);//更新集合到每个点的最小距离 v为新点 
            //    for(int i=1;i<=n;i++) mincost[i]=minn(mincost[i],mapp[b][i]);
            }
            prim();
        }
        return 0;
    
     
     
     
  • 相关阅读:
    Git fetch和git pull的区别
    gitlab数据迁移
    阿里云CentOS7挂载SSD云盘的方法
    phpQuery的用法
    用shell查找某目录下的最大文件
    gearman 简介
    学习笔记(3)——实验室集群WMS服务配置
    学习笔记(2)——实验室集群LVS配置
    Fedora16的双显卡切换问题
    学习笔记(1)——实验室集群配置
  • 原文地址:https://www.cnblogs.com/z1141000271/p/5800136.html
Copyright © 2020-2023  润新知