地址: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. 自己动手写处理器之第一阶段(3)——MIPS32指令集架构简单介绍

    将陆续上传本人写的新书<自己动手写处理器>(尚未出版).今天是第四篇.我尽量每周四篇 1.4 MIPS32指令集架构简单介绍 本书设计的处理器遵循MIPS32 Release 1架构,所以 ...

  2. std::map中函数用法集合

    1 STL的map表里有一个erase方法用来从一个map中删除掉指令的节点  2 eg:  3 map<string,string> mapTest;  4 typedef map< ...

  3. C#WebService 客户端通过Http调用请求(转)

    1.webservice帮助类 public class WebServiceHelper    {               public static string CallServiceByG ...

  4. preg_replace的用法

    <?php $str1 = "03/28/2015"; // 要替换成 2015-03-28 echo preg_replace("/([0-1][1-9])\/( ...

  5. 6.PHP 教程_PHP数据类型

    PHP数据类型 String(字符串),Integer(整型),Float(浮点型),Boolean(布尔型),Array(数组),Object(对象), NULL(空值). PHP字符串 一个字符串 ...

  6. codeforces 628F. Bear and Fair Set 网络流

    题目链接 F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. 常用SQL的优化

    导入数据 对于MyISAM存储引擎的表,可以通过以下方式快速地导入大量数据 alter table tbl_name disable keys;              //关闭表非唯一索引的更新 ...

  8. ASP.NET MVC进阶之路:深入理解依赖注入(DI)和控制反转(IOC)

    0X1 什么是依赖注入 依赖注入(Dependency Injection),是这样一个过程:某客户类只依赖于服务类的一个接口,而不依赖于具体服务类,所以客户类只定义一个注入点.在程序运行过程中,客户 ...

  9. RFID标签天线的三种制作方法

    在RFID标签中,天线层是主要的功能层,其目标是传输最大的能量进出标签芯片.RFID天线是按照射频识别所要求的功能而设计的电子线路,将导电银浆或导电碳浆网印在PVC.PC或PET上,再与面层.保护层和 ...

  10. PHP查看在线服务器与本地服务器支持函数差别

    在本地开发好的程序.代码,当传到服务器后,经常会出现罢工的事情.这时候就要考虑开发环境和在线的环境是否完全一致了.这里所说的一致性,包括了容器的配置.php的配置等等.下面所要解决的只是其中一项,你的 ...