Poj(1251),Prim字符的最小生成树

题目链接:http://poj.org/problem?id=1251

字符用%s好了,方便一点。

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

#define INF 0x3f3f3f3f

int maps[305][305];
bool vis[305];
int dis[305];
int n;

int Prim()
{
    memset(vis,false,sizeof(vis));
    for(int i=1; i<=n; i++)
        dis[i] = INF;
    int ans=0;
    dis[1] = 0;
    for(int i=1; i<=n; i++)
    {
        int tmp = INF,k=0;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]<tmp)
            {
                tmp = dis[j];
                k=j;
            }
        }

        vis[k] = true;
        ans+=tmp;
        for(int i=1; i<=n; i++)
        {
            if(!vis[i]&&dis[i]>maps[k][i])
                dis[i] = maps[k][i];
        }
    }
    return ans;
}

int main()
{
    while(scanf("%d",&n),n)
    {
        for(int i=0; i<=n; i++)
        {
            for(int j=0; j<=n; j++)
                if(i==j)
                    maps[i][j]=0;
                else maps[i][j] = INF;
        }
        for(int i=0; i<n-1; i++)
        {
            int t;
            char a[2],b[2];
            scanf("%s%d",a,&t);
            for(int j=0; j<t; j++)
            {
                int dist;
                scanf("%s%d",b,&dist);
                maps[a[0]-‘A‘+1][b[0]-‘A‘+1] = maps[b[0]-‘A‘+1][a[0]-‘A‘+1] = dist;
            }
        }
        printf("%d\n",Prim());
    }
    return 0;
}
时间: 2024-10-24 14:05:35

Poj(1251),Prim字符的最小生成树的相关文章

POJ 1251 Jungle Roads (最小生成树)

POJ 1251 Jungle Roads Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road

POJ 1251 Jungle Roads(最小生成树)

题意  有n个村子  输入n  然后n-1行先输入村子的序号和与该村子相连的村子数t  后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离  求链接所有村子的最短路径 还是裸的最小生成树咯 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=30,M=1000; int par[N],n,m,ans; struct edge{int u

POJ 1251 Jungle Roads (prim)

D - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1251 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extr

ZOJ 1406 POJ 1251 Jungle Roads 丛林中的道路,最小生成树,Kruskal算法

题目链接:ZOJ 1406 POJ 1251 Jungle Roads 丛林中的道路 Jungle Roads Time Limit: 2 Seconds      Memory Limit: 65536 KB The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some ye

poj 2485(prim最小生成树)

Highways Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways

poj 1251(最小生成树)

Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensi

[2016-04-09][POJ][1251][Jungle Roads]

时间:2016-04-09 00:02:24 星期六 题目编号:[2016-04-09][POJ][1251][Jungle Roads] 题目大意:给定n个城镇的若干条路及其每月维修的代价,问,在所有城市联通的情况下,最少需要多少维修费 分析: 保证边权最小,并且图联通-–>最小生成树 #include <algorithm> #include <cstring> #include <cstdio> using namespace std; int fa[30]

POJ 1251 Jungle Roads(kruskal)

Submit Status Practice POJ 1251 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the l

{POJ}{3925}{Minimal Ratio Tree}{最小生成树}

题意:给定完全无向图,求其中m个子节点,要求Sum(edge)/Sum(node)最小. 思路:由于N很小,枚举所有可能的子节点可能情况,然后求MST,memset()在POJ里面需要memory头文件. #include <iostream> #include <vector> #include <map> #include <cmath> #include <memory> #include <algorithm> #includ