POJ - 1251A - 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.
Input
Output
Sample Input
9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
Sample Output
216
30
解题思路:
大致题意:在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使每个岛屿都间接或直接与其他岛屿相连时所用的的最短时间(只有修完一个桥后才可修下一个桥)。
简言之就是求最小生成树。
对于数据,数据输入的第一行n代表岛屿的个数,当为0是结束程序,接着n-1行开始时为这岛屿的编号,用大写字母表示,接着是一个整数m
代码如下:
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std; int n , m ,k;
char a , b ;
struct edge{
int u ;
int v ;
int w ;
}e[];
int pre[];
bool cmp(edge a ,edge b)
{
return a.w < b.w;
}
int find(int x)
{
return (x==pre[x])?x:pre[x] = find(pre[x]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==)
break;
int edge_num = ;
for(int i = ; i <= ;i++)
{
pre[i] = i;
} for(int i = ; i < n ;i++)
{
cin>>a>>m;
for(int j = ; j < m ; j++)
{
cin>>b>>k;
e[edge_num].u = a ;
e[edge_num].v = b ;
e[edge_num].w = k;
edge_num++;
}
}
int ans = ;
sort(e,e+edge_num,cmp);
for(int i = ; i < edge_num ;i++)
{
int u = e[i].u;
int v = e[i].v;
int w = e[i].w;
int fx = find(u);
int fy = find(v);
if(fx!=fy)
{
pre[fx] = fy;
ans += w;
}else
{
continue;
}
}
printf("%d\n",ans);
}
return ;
}
POJ - 1251A - Jungle Roads 利用最小生成树的更多相关文章
- POJ 1251 Jungle Roads(最小生成树)
题意 有n个村子 输入n 然后n-1行先输入村子的序号和与该村子相连的村子数t 后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离 求链接全部村子的最短路径 还是裸的最小生成树咯 ...
- POJ 1251 Jungle Roads (最小生成树)
题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...
- POJ - 1251 Jungle Roads (最小生成树&并查集
#include<iostream> #include<algorithm> using namespace std; ,tot=; const int N = 1e5; ]; ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- POJ 1251 Jungle Roads - C语言 - Kruskal算法
Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- POJ1251 Jungle Roads 【最小生成树Prim】
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19536 Accepted: 8970 Des ...
- HDU-1301 Jungle Roads(最小生成树[Prim])
Jungle Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
随机推荐
- JRE,JVM,JDK
JRE,JVM,JDK的关系.JRE(Java Runtime Environment)java运行环境,我们可以把它看成是一个操作系统.也就是说JRE提供了Java执行的软件平台. JVM (Jav ...
- Chrome谷歌浏览器屏蔽百度搜索右侧广告推荐方法
先上图百度广告,其实屏蔽广告很简单 主要分成以下三步: 下载Adblock Plus插件 安装Adblock Plus插件 开启屏蔽 一.下载Adblock Plus插件(官网离线版) 二.安装Adb ...
- EasyBuy项目总结_20180409
一.项目技术点 1.熟练使用jsp及el和jstl表达式 el: $() jstl: 1.导包; 2.声明<%@ taglib uri=" " preffix="c ...
- CTE 中字符串拼接
1>cte语法 1.1>基础语句和递归语句中必须有字段 1.2>最后只能跟一条查询语句 1.3>字符串拼接,要将拼接的字段转成字符串类型,cast(fieldName as n ...
- jvisualvm远程监控服务器tomcat
1.在 {服务器tomcat路径}/bin/catalina.sh 中,的[# OS specific support. $var _must_ be set to either true or f ...
- 在OpenSSL中添加自定义加密算法
一.简介 本文以添加自定义算法EVP_ssf33为例,介绍在OpenSSL中添加自定义加密算法的方法 二.步骤 1.修改crypto/object/objects.txt,注册算法OID,如下: rs ...
- 使用OpenSSL进行转换
使用OpenSSL进行转换 摘自:https://cloud.tencent.com/developer/ask/29886 这些命令允许您将证书和密钥转换为不同的格式,以使它们与特定类型的服务器或软 ...
- HBase数据读写流程(1.3.1)
===数据写入流程=== 源码:https://github.com/apache/hbase/blob/master/hbase-server/src/main/java/org/apache/ha ...
- Django学习笔记:为Model添加Action
|- Django版本:1.8 |- Python版本:3.4 models.py 1 class Story(models.Model): #编辑Story状态 STATUS_CHOICES = ( ...
- Perl语言编程>>学习笔记2
1. Perl中变量的常用表示 ${var} 相当于 $var $Dog::days 在Dog包里面的变量$days $#days @days 的最后一个索引 ] $days 引用的数组 ...