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

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 && HDU 1301 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 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. 常规轮询请求,客户端用Ajax调webservice的方法

    服务端发布webservice,下图方框中的一定要有 客户端代码 <script type="text/javascript"> $(document).ready(f ...

  2. asp.net mvc 用Redis实现分布式集群共享Session。

    1.这两天研究Redis搞分布式session问题,网上找的资料都是用ServiceStack.Redis来实现的,但是在做性能测试的时候发现最新的v4版本有限制每小时候最多请求6000次,因为官网开 ...

  3. WordPress 主题开发 - (十三) Archive模板 待翻译

    What archive.php does (and all its related templates) is show posts based on a select criteria. A da ...

  4. JSP标记

    JSP标记是JSP页面中很重要的组成部分,JSP标记包括指令标记.动作标记和自定义标记.其中自定义标记主要讲述与Tag文件有关的Tag标记. 一 指令标记page Page指令标记,简称page指令, ...

  5. 【C++】快排

    假设要排序的数据类型为int int main() { qsort(a,len,sizeof(int),cmp); //qsort(数组的起始位置,排序个数,类型大小,比较函数); } int cmp ...

  6. 配置《算法 第四版》的Eclipse开发环境

    1. 安装JAVA JAVA网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 配置环境变量(我把JAVA安装在 ...

  7. 第六节:宿主如何使用AppDomain

    前面已经讨论了宿主以及宿主加载CLR的方式.同时还讨论了宿主如何告诉CLR创建和卸载AppDomain.为了使这些讨论更加具体,下面将描述一些常见的宿主和AppDomain使用情形.特别地,我要解释不 ...

  8. MongoDB 学习笔记(二)—— MongoDB Shell

    MongoDB自带一个JavaScript shell 可以从命令行中与MongoDB交互,功能非常强大.如在上一节最后一张图所看到,可以执行JavaScript程序. 运行Shell 前提是启动Mo ...

  9. StyleCop学习笔记——自定义规则

    本文将简单的一步一步的指导这可能有助于学习如何创建自己的规则 1.创建一个项目. Visual Studio创建一个新的类库项目.NET3.5 2.引用两个DLL,StyleCop.dll和Style ...

  10. Zygote(app_process)相关分析2

    在前一篇文章中已经分析了从init.c到Zygote(app_process)的启动流程. 今天开始分析frameworks/base/cmds/app_process/app_main.cpp. s ...