POJ 1251 Jungle Roads

题意:嗯……没看题……看了眼图……求个最小生成树。

解法:kruskal。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long

using namespace std;

struct node
{
    int u, v, val;
    bool operator < (const node &tmp) const
    {
        return val < tmp.val;
    }
}edge[10005];
int father[105];
int Find(int a)
{
    if(a != father[a]) father[a] = Find(father[a]);
    return father[a];
}
bool Union(int a, int b)
{
    int c = Find(a), d = Find(b);
    if(c == d) return false;
    father[c] = d;
    return true;
}
int main()
{
    int n;
    while(~scanf("%d", &n) && n)
    {
        int cnt = 0;
        for(int i = 1; i <= n; i++)
            father[i] = i;
        for(int i = 1; i < n; i++)
        {
            char ch[2];
            int k;
            scanf("%s%d", ch, &k);
            int u = ch[0] - ‘A‘ + 1;
            while(k--)
            {
                int x;
                scanf("%s%d", ch, &x);
                edge[cnt].u = u;
                edge[cnt].v = ch[0] - ‘A‘ + 1;
                edge[cnt++].val = x;
            }
        }
        sort(edge, edge + cnt);
        int ans = 0;
        for(int i = 0; i < cnt; i++)
        {
            if(Union(edge[i].u, edge[i].v)) ans += edge[i].val;
        }
        printf("%d\n", ans);
    }
    return 0;
}

  就不告诉你们是2421的代码改的……

时间: 2024-12-09 22:37:27

POJ 1251 Jungle Roads的相关文章

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

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

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

题意  有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

NYOJ 434 &amp;&amp; POJ 1251 Jungle Roads(最小生成树)

链接:click here 题意: 题目大意在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使每个岛屿都间接或直接与其他岛屿相同时所用的的最短时间(只有修完一个桥后才可修下一个桥).简言之就是求最小生成树. 对于数据,数据输入的第一行n代表岛屿的个数,当为0是结束程序,接着n-1行开始时为这岛屿的编号,用大写字母表示,接着是一个整数m,表示与该岛屿连接的字典序大于该岛屿编号的个数,然后该行输入m对数据,每对数据的第一个字母表示与该岛屿连通的岛屿的编号,第二个数字表示要重修

HDU 1301 &amp;POJ 1215 Jungle Roads【最小生成树,Prime算法+Kruskal算法】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6737    Accepted Submission(s): 4893 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst o

【POJ】1251 Jungle Roads

题目链接:http://poj.org/problem?id=1251 题意:n个村庄字母标号,每个字母后跟m个字母,表示该字母到mi的距离.求构建所有村庄道路的最短距离. 题解:最小生成树裸题.注意输入. 代码: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 const int maxn = 30; 6 const int inf = 0x3f3