地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301

很明显,这是一道“赤裸裸”的最小生成树的问题;

我这里采用了Kruskal算法,当然用Prim算法也一样可以解题。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std; typedef struct node{
int from, to;
int length; //长度,指代问题中两地间的费用
}node; const int maxn = 90;
node edge[maxn];
int pre[30];
int N,turn; int cmp(const void *a, const void *b) // 比较函数
{
return (*(node*)a).length-(*(node*)b).length;
} inline int root( int i ) //用于判断是否两节点在同一树上
{
while( pre[i] )
i=pre[i];
return i;
} inline void input() //输入
{
char c1,c2;
int i,order;
int dis;
turn=0;
while(cin>>c1>>order)
{
for(i=0;i<order;i++)
{
cin>>c2>>dis;
edge[turn].from=c1-'A';
edge[turn].to=c2-'A';
edge[turn].length=dis;
turn++;
}
if( c1=='A'+N-2 ) //输入完成,退出
break;
}
return ;
} inline int span()
{
memset(pre,0,sizeof(pre));
qsort(edge,turn,sizeof(node),cmp); //根据长度排序
int cost=0;
int count=0;
int pos=-1;
int m,n;
while(count<N-1)
{
do{
pos++;
m=root( edge[pos].from );
n=root( edge[pos].to );
}while(n==m); //用于判断是否两节点在同一树上
cost = cost + edge[pos].length;
pre[m]=n; //把该节点加入这棵树
count++;
}
return cost;
} int main()
{
while(cin>>N && N)
{
input();
int flag=span();
cout<<flag<<endl;
}
return 0;
}

Hdu 1301 Jungle Roads (最小生成树)的更多相关文章

  1. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

  2. POJ 1251 && HDU 1301 Jungle Roads (最小生成树)

    Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...

  3. hdu 1301 Jungle Roads krusckal,最小生成树,并查集

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...

  4. HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

    双向边,基础题,最小生成树   题目 同题目     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...

  5. POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】

    题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解  Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...

  6. 最小生成树 || HDU 1301 Jungle Roads

    裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...

  7. POJ 1251 & HDU 1301 Jungle Roads

    题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...

  8. hdu 1301 Jungle Roads

    http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...

  9. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

随机推荐

  1. ios蓝牙开发(三)app作为外设被连接的实现

    再上一节说了app作为central连接peripheral的情况,这一节介绍如何使用app发布一个peripheral,给其他的central连接 还是这张图,central模式用的都是左边的类,而 ...

  2. HDU1171:Big Event in HDU(多重背包分析)

    通过分析,要使A>=B并且差值最小.所以只要使sum/2的容量下,B最大就Ok了 #include<iostream> #include<cstdio> #include ...

  3. HTML静态网页(框架)

    1.frameset frameset最外层,使用时需要去除body改用frameset. <frameset  rows="100,*" frameborder=" ...

  4. Java 网络编程(二) 两类传输协议:TCP UDP

    链接地址:http://www.cnblogs.com/mengdd/archive/2013/03/09/2951841.html 两类传输协议:TCP,UDP TCP TCP是Transfer C ...

  5. Yii2.0中文开发向导——rules常用规则

    public function rules(){ return [ //必须填写 ['email, username, password,agree,verifyPassword,verifyCode ...

  6. 关于yield创建协程的理解

    先上利于理解的代码: #coding:utf-8 def consumer(): c_r = '' while 1: m = yield c_r if not m: return print(&quo ...

  7. R语言学习之主成分分析法的R实践

    主成分分析R软件实现程序(一): >d=read.table("clipboard",header=T) #从剪贴板读取数据 >sd=scale(d)  #对数据进行标 ...

  8. (Problem 42)Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  9. 宣布在日本地区正式发布 Windows Azure

     昨天,我与 Microsoft 日本的集团副总裁 Yasuyuki Higuchi 一同站在台上,宣布在两个新地区正式发布 Windows Azure:日本东部和日本西部.能够亲自见证 Micr ...

  10. USACO 2001 OPEN

    第1题 绿组. 奶牛接力赛[relay] 题目描述 农夫约翰已经为一次赛跑选出了K(2≤K≤40)头牛组成了一支接力队.赛跑在农夫约翰所拥有的农场上进行,农场的编号为1到Ⅳf4≤Ⅳ< 800), ...