Hdu 1301 Jungle Roads (最小生成树)
地址: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 (最小生成树)的更多相关文章
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- 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 ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】
题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解 Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...
- 最小生成树 || HDU 1301 Jungle Roads
裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...
- POJ 1251 & HDU 1301 Jungle Roads
题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...
- hdu 1301 Jungle Roads
http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
随机推荐
- ListView中响应item的点击事件并且刷新界面
---恢复内容开始--- 最近在在实现listview功能中遇到了这个问题: 点击事件写在了adapter的item中,不知道如何在listview的点击事件中更新数据的显示: 总结:1.要使用not ...
- Extjs 3.4 生成button,并調用相同的window
/////定義一個方法,用來調用win_mucangjieshou的窗口 var panel_contant= function(id_name){ var aa=Ext.getCmp(id_name ...
- AsyncSocket的长连接使用
使用背景:需要跟服务器长期保持连接进行即时通讯:还有在跟智能硬件建立实时链接进行同步智能硬件的状态等,最近我就做项目就碰到需要实时更新智能硬件的状态(比如智能硬件的电量,以及其它工作状态),跟智能硬件 ...
- JavaSE_ 反射 目录(27)
JavaSE学习总结第27天_反射 & 设计模式 & JDK5.7.8新特性27.01 反射_类的加载概述和加载时机27.02 反射_类加载器的概述和分类27.03 反射_反射概述27 ...
- POJ 2406 Power Strings(字符串的最小循环节)
题目链接:http://poj.org/problem?id=2406 题意:确定字符串最多是多少个相同的字串重复连接而成的 思路:关键是找到字符串的最小循环节 code: #include < ...
- iframe 自适应高度、宽度
示例: <iframe id="zyms" frameborder="0" scrolling="yes" style="w ...
- PHP的一些函数
//进制转换类 base_convert //字符转十六进制 binhex
- [LeetCode]题解(python):080-Remove Duplicates from Sorted Array II
题目来源: https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意分析: 跟定一个排好序的数组.修改这个数组使 ...
- XMLTProcessor根据XSLT样式规则将节点转换为document对象
最近使用Firefox进行网页的调试,发现有些javascript XSLT处理xml的语句仅仅支持IE浏览器.而网络中的一些介绍Javascript XSLT 处理XML的文章基本上都是依据Ajax ...
- 手机SIM卡介绍 三类不同标准的SIM卡
SIM卡的全称是Subscriber Identity Module,翻译过来也叫客户识别模块,也叫做智能卡.用户身份识别卡.这块小小的芯片可以存储用户的号码.信息,以及一定数量的联系人数据,配合我们 ...