Jungle Roads---poj1251 hdu1301
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
- 最小生成树模板题
- #include<stdio.h>
- #include<string.h>
- #include<map>
- #include<iostream>
- #include<algorithm>
- #include<math.h>
- #define N 110
- #define INF 0xfffffff
- using namespace std;
- int dist[N], vis[N], n, maps[N][N];
- void Init()
- {
- for(int i=; i<=n; i++)
- {
- vis[i] = ;
- dist[i] = INF;
- for(int j=; j<=n; j++)
- {
- maps[i][j] = INF;
- }
- }
- }
- int Prim(int start)
- {
- int ans = ;
- for(int i=; i<=n; i++)
- dist[i] = maps[start][i];
- vis[start] = ;
- for(int i=; i<n; i++)
- {
- int Min = INF,index = -;
- for(int j=; j<=n; j++)
- {
- if(vis[j]== && Min > dist[j])
- Min = dist[j], index = j;
- }
- if(index == -) break;
- vis[index] = ;
- ans += Min;
- for(int j=; j<=n; j++)
- {
- if(vis[j]== && dist[j] > maps[index][j])
- {
- dist[j] = maps[index][j];
- }
- }
- }
- return ans;
- }
- int main()
- {
- int i, j, x, a, b, ans, m;
- char s[];
- while(scanf("%d", &n), n)
- {
- Init();
- for(i=; i<n; i++)
- {
- scanf("%s%d", s, &m);
- a = s[] - 'A' + ;
- for(j=; j<m; j++)
- {
- scanf("%s%d", s, &x);
- b = s[] - 'A' + ;
- maps[a][b] = maps[b][a] = min(maps[a][b],x);
- }
- }
- ans = Prim();
- printf("%d\n", ans);
- }
- return ;
- }
Jungle Roads---poj1251 hdu1301的更多相关文章
- 暑假集训(3)第二弹 -----Jungle Roads(Hdu1301)
问题梗概:自从上次某个acmer来设计了拉格瑞圣岛的交通路线后,岛上的酋长就相当苦恼,他发现,虽然这些修好的公路便利了岛上的 交通,并且让拉格瑞圣岛的旅游业更加兴旺,甚至他们还收到了一笔不小的国际资金 ...
- HDU1301&&POJ1251 Jungle Roads 2017-04-12 23:27 40人阅读 评论(0) 收藏
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25993 Accepted: 12181 De ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 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 ...
- POJ1251 Jungle Roads(Kruskal)(并查集)
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23882 Accepted: 11193 De ...
- HDU1301 Jungle Roads
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...
- POJ1251 Jungle Roads Kruskal+scanf输入小技巧
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
随机推荐
- MQTT-C-PUB
/* ============================================================================ Name : mqtest ...
- 《C++标准程序库》笔记之四
本篇博客笔记顺序大体按照<C++标准程序库(第1版)>各章节顺序编排. ---------------------------------------------------------- ...
- Masonry — 使用纯代码进行iOS应用的autolayout自适应布局
本文转载至 http://www.ios122.com/2015/09/masonry/ 简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstrain ...
- var_dump出现省略号的问题
xdebug.var_display_max_children=128xdebug.var_display_max_data=512xdebug.var_display_max_depth=5
- 获取访客IP、地区位置信息、浏览器、来源页面
<?php //这个类似用来获取访客信息的 //方便统计 class visitorInfo { //获取访客ip public function getIp() { $ip=false; if ...
- 【技术分享会】 iOS开发简述
前言 Objective-C(简称OC)也是面向对象的编程语言,运用的许多面向对象的编程思想和C# . Java .C++等变成语言都是相通的: 本次技术讲座主要讲一些设计模式.设计思想等计算机语言通 ...
- 原生js--cookie操作的封装
封装cookie的操作:查询cookie个数.查询所有cookie的键.获取cookie.设置cookie.删除cookie.清除全部cookie /** * cookieStorage */func ...
- 怎么使用jstack精确找到异常代码
1.代码demo //一个CPU密集型线程的demo: package chapter1; public class FindJavaThreadInTaskManager { public stat ...
- flex常用兼容写法
一般放在common.css中: .flex{ display: -webkit-box; display: -webkit-flex; display: -moz-box; display: -ms ...
- How to Use Postman to Manage and Execute Your APIs
How to Use Postman to Manage and Execute Your APIs Postman is convenient for executing APIs because ...