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 ...
随机推荐
- k8s podpreset 参数注入
启动apiserver时,增加 参数 --runtime-config=settings.k8s.io/v1alpha1=true kind: PodPresetapiVersion: setting ...
- git 转移
git push --mirror https://github.com/cloud-pi/drbd-docker-plugin.git
- Linux实战教学笔记43:squid代理与缓存实践(二)
第6章 squid代理模式案例 6.1 squid传统正向代理生产使用案例 6.1.1 squid传统正向代理两种方案 (1)普通代理服务器 作为代理服务器,这是SQUID的最基本功能:通过在squi ...
- 反射与dynamic
反射 var a = Assembly.GetExecutingAssembly(); Type type = a.GetType("CLRTest.ReflectClass"); ...
- 富文本编辑器和fastdfs的使用
宜立方商城的系统架构a) 功能介绍(项目架构,有哪些功能模块,这些功能模块如何实现?)b) 架构讲解工程搭建-后台工程c) 使用maven搭建工程(后台工程如何搭建?)d) 使用maven的tomca ...
- [模板]单源最短路径(Dijkstra)
如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 主要还是再打一遍最短路,这种算法我用的不多... #include<bits/stdc++.h> using namesp ...
- [JAVA] 小数转百分数
import java.text.NumberFormat; //获取格式化对象 NumberFormat format = NumberFormat.getPercentInstance(); // ...
- data-参数说明(模态弹出窗的使用)
除了通过data-toggle和data-target来控制模态弹出窗之外,Bootstrap框架针对模态弹出框还提供了其他自定义data-属性,来控制模态弹出窗.比如说:是否有灰色背景modal-b ...
- NBA常识 位置的划分 足球:越位等于抢跑
篮球:1号位——组织后卫(控球,组织)2号位——得分后卫(中远投篮,突破)3号位-----小前锋(突破,中远投篮)4号位——大前锋(二中锋,篮板,背身单打,禁区防守)5号位——中锋(篮板.背身单打,禁 ...
- 用jvm指令分析String 常量池
其他博友的不同理解方式: http://hi.baidu.com/boywell/item/d5ee5b0cc0af55c875cd3cfd 我们先来看一个类 public class javaPT ...