地址: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. Android TextView 实现文字大小不同和文字颜色不同

    效果图如下: 关键代码如下: StringBuffer sb = new StringBuffer(); if(day > 0) { sb.append("<a href=\&q ...

  2. SQL Server索引进阶:第二级,深入非聚集索引

    原文地址: Stairway to SQL Server Indexes: Level 2, Deeper into Nonclustered Indexes 本文是SQL Server索引进阶系列( ...

  3. [Swust OJ 403]--集合删数

    题目链接:http://acm.swust.edu.cn/problem/403/ Time limit(ms): 5000 Memory limit(kb): 65535   Description ...

  4. 关于一个wpf中的定时器

    http://www.cnblogs.com/royenhome/archive/2010/01/24/1655168.html

  5. 射频识别技术漫谈(24)——ISO15693的防冲突与传输协议

    遵守ISO15693协议的电子标签都有一个8字节共64bit的全球唯一序列号(UID),这个UID一方面可以使全球范围内的标签互相区别,更重要的是可以在多标签同时读写时用于防冲突.8字节UID按权重从 ...

  6. Spring Boot使用Redis进行消息的发布订阅

    今天来学习如何利用Spring Data对Redis的支持来实现消息的发布订阅机制.发布订阅是一种典型的异步通信模型,可以让消息的发布者和订阅者充分解耦.在我们的例子中,我们将使用StringRedi ...

  7. AttributeError: 'module' object has no attribute 'Thread'

    $ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last):  File "th ...

  8. 如何开始一个模块化可扩展的Web App(转)

    原文链接:http://avnpc.com/pages/start-a-modular-extensible-webapp 日志未经声明,均为AlloVince原创.版权采用『 知识共享署名-非商业性 ...

  9. Winet API 支持HTTPP/SOCKS代理

    源程序 1.Winet API 支持使用IE代理.或者不使用代理.或者使用自定义代理三种方式. 2.使用自定义代理的话,支持HTTP代理,SOCKS代理,但是SOCKS代理不知支持用户名密码,HTTP ...

  10. 定制ToolChain for ARM

    **************************************************************************编写:王卫无,北京讯业互联科技有限公司版本号:V1. ...