hdu 1301 Jungle Roads 最小生成树

时间:2021-10-31 20:50:15

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301

hdu 1301 Jungle Roads 最小生成树

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

The input consists of one to 100 data sets,
followed by a final line containing only 0. Each data set starts with a line
containing only a number n, which is the number of villages, 1 < n < 27,
and the villages are labeled with the first n letters of the alphabet,
capitalized. Each data set is completed with n-1 lines that start with village
labels in alphabetical order. There is no line for the last village. Each line
for a village starts with the village label followed by a number, k, of roads
from this village to villages with labels later in the alphabet. If k is greater
than 0, the line continues with data for each of the k roads. The data for each
road is the village label for the other end of the road followed by the monthly
maintenance cost in aacms for the road. Maintenance costs will be positive
integers less than 100. All data fields in the row are separated by single
blanks. The road network will always allow travel between all the villages. The
network will never have more than 75 roads. No village will have more than 15
roads going to other villages (before or after in the alphabet). In the sample
input below, the first data set goes with the map above.

The output is
one integer per line for each data set: the minimum cost in aacms per month to
maintain a road system that connect all the villages. Caution: A brute force
solution that examines every possible set of roads will not finish within the
one minute time limit.

题意描述:n个城市之间有一些道路连接和维修道路所需的花费,要求n个城市保持直接或间接道路通畅,求出最少花费。

算法分析:赤果果的最小生成树。(最近的切身体会:算法基础一定要扎实,不然高级一点的算法学了之后反倒基础全忘完了)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std; int g[][],vis[];
int n; int Prim()
{
int t=n-;
int sum=;
while (t--)
{
int k,Min=inf;
for (int i= ;i<n ;i++)
{
if (!vis[i] && Min>g[][i])
{
k=i;
Min=g[][i];
}
}
vis[k]=;
sum += Min;
for (int i= ;i<n ;i++)
{
if (!vis[i] && g[k][i]<g[][i])
g[][i]=g[k][i];
}
}
return sum;
} int main()
{
while (scanf("%d",&n)!=EOF && n)
{
char str[],s[];
int num,cnt;
for (int i= ;i< ;i++)
{
for (int j= ;j< ;j++)
g[i][j]=inf;
}
memset(vis,,sizeof(vis));
for (int i= ;i<n ;i++)
{
scanf("%s%d",str,&num);
int x=str[]-'A';
while (num--)
{
scanf("%s%d",s,&cnt);
int y=s[]-'A';
g[x][y]=g[y][x]=cnt;
}
}
printf("%d\n",Prim());
}
return ;
}

hdu 1301 Jungle Roads 最小生成树的更多相关文章

  1. POJ 1251 &amp&semi;&amp&semi; HDU 1301&Tab; Jungle Roads (最小生成树)

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

  2. Hdu 1301 Jungle Roads (最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也 ...

  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&&num;160&semi;1301&&num;160&semi;Jungle&&num;160&semi;Roads&&num;160&semi;(最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

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

  5. POJ 1251 &plus; HDU 1301 Jungle Roads 【最小生成树】

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

  6. 最小生成树 &vert;&vert; HDU 1301 Jungle Roads

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

  7. POJ 1251 &amp&semi; 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 &lpar;最小生成树&rpar;

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

随机推荐

  1. 在虚拟机中安装Ubuntu Server 15&period;04

    学Linux,上红联! 红联Linux门户|Linux通用技术|Linux发行版技术|Linux企业应用|Linux实验室|红联Linux论坛 Linux系统教程 Linux入门 Linux管理 Li ...

  2. 使用连接来代替in和not in(使用外连接技巧)

    比如:表A里面的一个字段叫做MOBILE 里面存的记录如下 : 123456781 表B里面的一个字段也叫做MOBILE里面存的记录如下 12341910   (1)我们要查询一下A和B里面都有的,以 ...

  3. Java 理论与实践&colon; 非阻塞算法简介--转载

    在不只一个线程访问一个互斥的变量时,所有线程都必须使用同步,否则就可能会发生一些非常糟糕的事情.Java 语言中主要的同步手段就是synchronized 关键字(也称为内在锁),它强制实行互斥,确保 ...

  4. 7 个改变世界的 Java 项目

    英文链接:http://radar.oreilly.com/2011/07/7-java-projects.html 文章转载自:开源中国社区 http://www.oschina.net/news/ ...

  5. &lbrack;转&rsqb;解决MySQL出现大量unauthenticated user的问题

    最近发现两台MySQL server在中午的时候忽然(很突然的那种)发飙,不断的挂掉.重启mysql也尽是失败,看mysql的errorlog,只能看到类似如下的信息: Forcing close o ...

  6. &OpenCurlyDoubleQuote;百度杯”CTF比赛 2017 二月场&lowbar;onthink

    题目在i春秋ctf训练营中能找到,这题直接拿大佬的wp来充数 百度找到onethinnk的一个漏洞. 参考:http://www.hackdig.com/06/hack-36510.htm 就是注册个 ...

  7. 使用UE4&sol;Unity创建VR项目

    一.主要的步骤是说一下使用UE4,在此之前先说一下使用unity创建的VR项目 1.unity创建oculus rift dk2项目 在unity中创建一个简单的场景,让摄像机能看见场景中的物体,不对 ...

  8. 项目集成自动分词系统ansj,实现自定义词库

    一,分词系统地址:https://github.com/NLPchina/ansj_seg 二,为什么选择ansj? 1.项目需求: 我们平台要做手机售后的舆情分析,即对购买手机的用户的评论进行分析. ...

  9. semantic-ui 图标

    semantic-ui提供了很多的图标,基本常用的在官网上面都能找到.要想记住这么多图标是不可能的,但是也是有简便方法记忆. 首先,图标其实和按钮的区别基本没有,要说有的话,也就是基础样式的大小不同吧 ...

  10. Educational Codeforces Round 53 &lpar;Rated for Div&period; 2&rpar; A Diverse Substring

    传送门 https://www.cnblogs.com/violet-acmer/p/10163375.html 题意: 给出串是多态的定义“长度为 n 的串 s ,当串 s 中所有字母出现的次数严格 ...