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.

``` #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 ;
}```

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 ...

双向边,基础题,最小生成树   题目 同题目     #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 ...

http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...

9. poj 1251 Jungle Roads (最小生成树)

随机推荐

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++输出格式控制

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