hdu Constructing Roads (最小生成树)

简介:

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

复制代码
/************************************************************************/
/*     
        hdu  Constructing Roads
        最小生成树
        题目大意:在N个村子中已经存在部分存在连通,建最少长度的路使得所有的村子连通。
        解题思路:已经连通的村子其中间的路径作为0,即修建的时候修建为0耗费,求这些节点的最小生成树。
*/
/************************************************************************/

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 101;
int map[N][N];
int mark[N];
int n,q,i,j;

int Prim()
{
    int sum = 0;
    int t = n;
    int min,k;
    memset(mark,0,sizeof(mark));
    while(--t)
    {
        min = 10000;
        for (i = 2; i <= n; i++)
        {
            if(mark[i]!=1 && map[1][i] < min)
            {
                min = map[1][i];
                k = i;
            }
        }
        mark[k] = 1;
        sum += min;
        for (j = 2; j <= n; j++)
        {
            if(mark[j]!=1 && map[k][j] < map[1][j])
            {
                map[1][j] = map[k][j];
            }
        }
    }
    return sum;
}


int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                scanf("%d",&map[i][j]);
            }
        }
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d%d",&i,&j);
            map[i][j] = 0;
            map[j][i] = 0;
        }

        printf("%d\n",Prim());
    }
    
    return 0;
}
复制代码

 









本文转自NewPanderKing51CTO博客,原文链接: http://www.cnblogs.com/newpanderking/p/3248130.html,如需转载请自行联系原作者

相关文章
|
6月前
Jungle Roads(最小生成树)
Jungle Roads(最小生成树)
Constructing Roads(kruskal)
Constructing Roads(kruskal)
50 0
|
机器学习/深度学习 人工智能 BI
codeforces-1242-B 0-1 MST
B. 0-1 MST time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out.
164 0
codeforces-1242-B 0-1 MST
|
测试技术
HDU-1026,Ignatius and the Princess I(BFS+打印路径)
HDU-1026,Ignatius and the Princess I(BFS+打印路径)