(最小生成树) Jungle Roads -- POJ -- 1251

链接:

http://poj.org/problem?id=1251

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 110;
const int INF = 0xfffffff;

int n, J[N][N], dist[N], vis[N];

int Prim()
{
    int i, j, ans=0;
    dist[1]=0;
    memset(vis, 0, sizeof(vis));
    vis[1]=1;

    for(i=1; i<=n; i++)
        dist[i]=J[1][i];

    for(i=1; i<n; i++)
    {
        int index=1, MIN=INF;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]<MIN)
            {
                index=j;
                MIN=dist[j];
            }
        }
        vis[index]=1;
        ans += MIN;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]>J[index][j])
                dist[j]=J[index][j];
        }
    }
    return ans;
}

int main ()
{
    while(scanf("%d", &n), n)
    {
        int i, j, b, t, m;
        char ch;

        for(i=1; i<=n; i++)
        for(j=1; j<=i; j++)
          J[i][j]=J[j][i]=INF;

        for(i=1; i<n; i++)
        {
            cin>>ch>>m;
            for(j=0; j<m; j++)
            {
                cin>>ch>>t;
                b=ch-‘A‘+1;
                J[i][b]=J[b][i]=t;
            }
        }
        int ans=Prim();

        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-08 11:10:54

(最小生成树) Jungle Roads -- POJ -- 1251的相关文章

A - Jungle Roads - poj 1251(简单)

想必看这道题的时候直接看数据还有那个图就能明白什么意思吧,说的已经很清楚了,每个点都有一些相连的点和权值,求出来如果连接所有点,最小的权值是多少,赤裸裸的最小生成树... ************************************************************************************ #include<iostream>#include<cstring>#include<cstdio>#include<que

Jungle Roads POJ - 1251 模板题

#include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N=0x3f3f3f3f; int p[50]; struct edge{ int a,b,w; }e[100]; bool cmp(edge a,edge b) { return a.w<b.w; } int find(int x) { if(p[x]!=x) p[x]=find(p[x]

A - Jungle Roads——POJ

A - Jungle Roads Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status 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 vi

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 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

[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 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