地址: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. jquery表格可编辑修改表格里面的值,点击td变input无刷新更新表格

    td点击后变为input可以输入,更新数据,无刷新更新 演示 XML/HTML Code <table border="0" cellpadding="0" ...

  2. 设计模式入门之桥接模式Bridge

    Abstraction:抽象部分的父类,定义须要实现的接口.维护对实现部分的引用,从而把实现桥接到Implementor中去 Implementor:实现部分的接口 RefinedAbstractio ...

  3. svn+ssh

    According to official document, svn+ssh is supposed to be somehow faster than apache+dav_svn, howeve ...

  4. 一般处理程序在VS2012中打开问题

    问题:如果你用vs2012建立的一个一般处理程序,运行查看是,出现这样的界面 原因:VS2012默认使用IIS Web服务器,而不是Visual Studio开发服务器,基于安全考虑IIS默认不允许浏 ...

  5. C#操作Office.word(二)

    在上一篇文章"C#操作Office.word(一)"中我们讲述了如何使用VS2010引用COM中Miscrosoft Word 14.0 Object Library实现创建文档, ...

  6. 用JS画斐波那契螺旋线(黄金螺旋线)

    偶然看到斐波那契螺旋线(黄金螺旋线)的定义及画图方法,试着用JS画了一下,很漂亮,很好玩 具体定义及画法大家查一下就有了,很简单. 以下是代码: <!DOCTYPE html> <h ...

  7. (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单

    原文 (C#)Windows Shell 外壳编程系列7 - ContextMenu 注册文件右键菜单 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windo ...

  8. A Brief Introduction to Multiset[STL]

    基础 multiset是<set>库中一个非常有用的类型,它可以看成一个序列,插入一个数,删除一个数都能够在O(logn)的时间内完成,而且他能时刻保证序列中的数是有序的,而且序列中可以存 ...

  9. HDOJ 1043 Eight(A* 搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 思路分析: <1> 搜索算法: A*算法, Heuristic函数:曼哈顿距离 &l ...

  10. golang语法学习(一):变量,常量以及数据类型

    学习一门新的语言肯定是要从他的主要的语法開始,语法构成了整个程序设计的基础,从语法中我们也能够看到这门语言的一些特性.可是话说回来.语法这东西,不同的语言大同小异,所以这也对语法的记忆造成了一定的难度 ...