hdu 1301 Jungle Roads (最小生成树)

简介:

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

复制代码
/************************************************************************/
/*     
        hdu  Jungle Roads
        最小生成树
        题目大意:最小生成树,题目很长,题意很简单就是最小生成树,prim算法模拟
*/
/************************************************************************/

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>

#define MAX 0xfffffff

const int N = 30;
int map[N][N];
int vis[N];
int n;

void build_map()
{
    int t = n;
    char v,e;
    int num,len;

    for (int i = 0; i < n; i++)
    for (int j = i; j < n; j++)
    map[i][j] = map[j][i] = ((i==j)?0:MAX);

    while(--t)
    {
        getchar();
        v = getchar();
        scanf("%d",&num);
        while(num--)
        {
            getchar();
            e = getchar();
            scanf("%d",&len);
            map[v-'A'][e-'A'] = map[e-'A'][v-'A'] = len;
        }
    }
}

int prim()
{
    int t = n;
    int sum = 0;
    int min,k;
    vis[0] = 1;
    while(--t)
    {
        min = MAX;
        for(int i = 1; i < n; i++)
        {
            if (vis[i] != 1 && map[0][i] < min)
            {
                min = map[0][i];
                k = i;
            }
        }
        vis[k] = 1;
        sum += min;
        for (int i = 1; i < n; i++)
        {
            if (vis[i] != 1 && map[k][i] < map[0][i])
            map[0][i] = map[k][i];
        }
    }
    return sum;
}


int main()
{
    while(scanf("%d",&n) && n != 0)
    {
        build_map();
        memset(vis,0,sizeof(vis));
        printf("%d\n",prim());
    }
    return 0;
}
复制代码

 '










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

相关文章
|
6月前
Jungle Roads(最小生成树)
Jungle Roads(最小生成树)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
AtCoder Beginner Contest 216 G - 01Sequence (并查集 贪心 树状数组 差分约束)
152 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+打印路径)