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 ,如需转载请自行联系原作者

相关文章
|
8月前
|
Java
hdu-1869-六度分离(dijkstra)
hdu-1869-六度分离(dijkstra)
50 0
|
8月前
|
机器学习/深度学习 安全 Java
hdu-1596-find the safest road(dijkstra)
hdu-1596-find the safest road(dijkstra)
56 0
|
8月前
Jungle Roads(最小生成树)
Jungle Roads(最小生成树)
Constructing Roads(kruskal)
Constructing Roads(kruskal)
73 0
[HDU 4738] Caocao‘s Bridges | Tarjan 求割边
Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn’t give up. Caocao’s army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river,
148 0
HDU-1874,畅通工程续(Floyd最短路)
HDU-1874,畅通工程续(Floyd最短路)