A - Jungle Roads - poj 1251(简单)

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

************************************************************************************

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;

#define maxn 30

struct node
{
    int v, len;
    node(int v, int len):v(v),len(len){}
    friend bool operator < (node a, node b){
        return a.len > b.len;
    }
};
vector<node> G[maxn];

int prim(int s)
{
    int i, ans=0, use[maxn]={0}, M;
    priority_queue<node> Q;
    use[s] = 1;

for(i=0, M=G[s].size(); i<M; i++)
        Q.push(G[s][i]);

while(Q.size())
    {
        node q = Q.top();Q.pop();

if(use[q.v] == 0)
        {
            for(i=0, M=G[q.v].size(); i<M; i++)
                Q.push(G[q.v][i]);
            use[q.v] = 1, ans += q.len;
        }
    }

for(i=0; i<maxn; i++)
        G[i].clear();

return ans;
}

int main()
{
    int N;

while(scanf("%d", &N) != EOF && N)
    {
        int i, u, v, len, M;
        char s[10];

for(i=1; i<N; i++)
        {
            scanf("%s%d", s, &M);
            u = s[0] - ‘A‘;
            while(M--)
            {
                scanf("%s%d", s, &len);
                v = s[0] - ‘A‘;
                G[u].push_back(node(v, len));
                G[v].push_back(node(u, len));
            }
        }

int ans = prim(u);

printf("%d\n", ans);
    }

return 0;
}

时间: 2024-10-14 12:39:45

A - Jungle Roads - poj 1251(简单)的相关文章

(最小生成树) 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() { i

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

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

[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 (最小生成树)

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