题目链接: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 &amp;&amp; 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&#160;1301&#160;Jungle&#160;Roads&#160;(最小生成树,基础题,模版解释)——同 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 &amp; 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. loadrunner统计字符串中指定字符出现的次数

    Action() { char *str="sdfas1,sdfsdf2,sdfsdfsdfdsf3,sdfsdfsdfsdfds4,fsdfdsf5,sdfdsfsd6,fsdfsd7sd ...

  2. 20135203齐岳信息安全系统设计基础——实验四&amp;实验五实验报告

    见20135217孙小博的博客:http://www.cnblogs.com/sunxiaobo/p/4991861.html

  3. POJ 2632 Crashing Robots (坑爹的模拟题)

    Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6599   Accepted: 2854 D ...

  4. EXTENDED LIGHTS OUT poj1222 高斯消元法

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6443   Accepted: 42 ...

  5. TCP/IP协议全解析 三次握手与四次挥手[转]

    所谓三次握手(Three-Way Handshake)即建立TCP连接,就是指建立一个TCP连接时,需要客户端和服务端总共发送3个包以确认连接的建立.所谓四次挥手(Four-Way Wavehand) ...

  6. C++格式化输出,C++输出格式控制

    在输出数据时,为简便起见,往往不指定输出的格式,由系统根据数据的类型采取默认的格式,但有时希望数据按指定的格式输出,如要求以十六进制或八进制形式 输出一个 整数,对输出的小数只保留两位小数等.有两种方 ...

  7. ADO.NET学习(一)

    一.ADO.NET简介 ADO.NET可以看作是C#语言访问数据库的一种方式.编程语言编写的程序需要数据库的支持,那么怎样才能让他们建立连接呢?当然是ADO.NET 二.ADO.NET 整体流程 1) ...

  8. pt-kill 用法记录

    pt-kill 用法记录 # 参考资料Percona-Toolkit系列之pt-kill杀会话利器http://www.fordba.com/percona-toolkit-pt-kill.html ...

  9. Timeline Storyteller 现已加入自定义图表库

    前言 下载地址: https://store.office.com/en-us/app.aspx?assetid=WA104381136&sourcecorrid=328f5e2b-e973- ...

  10. php中按值传递和按引用传递的一个问题

    php中传递变量默认是按照值传递. 简单举个例子: <?php function testArray($arr){// &$arr $arr = array(1,2,3,); } $ar ...