(kruskal)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.

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.
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
Output
216
30

分析与解答:

卧槽,为什么我一写就是wrong answer最后发现,原来是char我写成了int。。。 - - ||
还是基本的思路,把题目的数据变成两个点加他们的距离,然后用kruskal算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int pre[505];
struct data
{int x,y;int len;
} a[25005];void init()
{for(int i=0; i<505; i++)pre[i]=i;
}
int Find(int x)
{if(pre[x]!=x)return pre[x]=Find(pre[x]);return x;
}
void Merge(int x,int y)
{int X=Find(x);int Y=Find(y);if(X!=Y)pre[X]=Y;
}
int cmp(data a,data b)
{return a.len<b.len;
}int main(){int n,m;while(cin>>n){if(n==0) return 0;init();int k=0;for(int i=0;i<n-1;++i){int c;scanf(" %c",&c);int j=c-'A';//cout<<j<<endl;scanf("%d",&m);//  if(m==0) continue;for(int l=0;l<m;++l){int c2;scanf(" %c",&c2);int le;scanf("%d",&le);int j2=c2-'A';//    cout<<j2<<endl;a[k].x=j;a[k].y=j2;a[k].len=le;k++;}}sort(a,a+k,cmp);int res=0;for(int i=0;i<k;i++){int X=Find(a[i].x);int Y=Find(a[i].y);if(X!=Y){pre[X]=Y;res+=a[i].len;}}printf("%d\n",res);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/560688.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php 留言板分页显示,php有分页的留言板,留言成功后怎么返回当前页?

比如我在【index.php?p3】发布留言&#xff0c;成功后怎么返回到 index.php?p3 这个页面&#xff1f;在【index.php?p5】发布留言成功后怎么返回index.php?p5这个页面&#xff1f;location.href"这里该怎么写&#xff1f;"涉及的页面有index.php&#xff0c;doac…

(最短路)HDU Today(hdu2112)

题目 Problem Description 经过锦囊相助&#xff0c;海东集团终于度过了危机&#xff0c;从此&#xff0c;HDU的发展就一直顺风顺水&#xff0c;到了2050年&#xff0c;集团已经相当规模了&#xff0c;据说进入了钱江肉丝经济开发区500强。这时候&#xff0c;XHD夫妇也退居了…

php文件读取文件内容,PHP文件系统函数-读取文件内容几种方式

介绍几种php获取文件内容的方式介绍读取文件的方式之前&#xff0c;我们先看一下打开文件资源和关闭资源名字资源绑定到一个流 - fopen关闭一个已打开的文件指针 - fclose$handle1 fopen("/home/rasmus/file.txt", "r");fclose($hanle1);$handle2 fopen(…

欧几里得算法和扩展欧几里得算法详解

欧几里得算法&#xff1a; int gcd(int x,int y){if(y) return gcd(y,x%y);return x; }扩展欧几里得算法&#xff1a; 先说一个整体思路&#xff1a; 先求AxBygcd(A,B);的一个解x&#xff0c;y 然后我们可以求他的通解 然后求AxByC的通解 我们先看看怎么求AxBygcd(A,B);的一…

php class使用方法,PHP调试类Krumo使用教程

写程序最讨厌的是程序发生错误&#xff0c;但是却又不知道该从何debug起&#xff0c;我们通常会使用print_r 或者 var_dump 或者是 echo 慢慢的debug。如果你跟我一样使用PHP 5开发&#xff0c;现在可以使用Krumo这个简单好用的工具帮助我们做这件事情。虽然IDE也有内建的debug…

(扩展欧几里得)青蛙的约会

题意 两只青蛙在网上相识了&#xff0c;它们聊得很开心&#xff0c;于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上&#xff0c;于是它们约定各自朝西跳&#xff0c;直到碰面为止。可是它们出发之前忘记了一件很重要的事情&#xff0c;既没有问清楚对方的特…

(待定系数法)A/B

题目&#xff1a; 要求(A/B)%9973&#xff0c;但由于A很大&#xff0c;我们只给出n(nA%9973)(我们给定的A必能被B整除&#xff0c;且gcd(B,9973) 1)。 Input 数据的第一行是一个T&#xff0c;表示有T组数据。 每组数据有两个数n(0 < n < 9973)和B(1 < B < 10^…

matlab 连接数组,matlab数组操作知识点总结

其实如果单从建模来讲&#xff0c;以下大部分函数都用不到&#xff0c;但是这些都是基础。第一点&#xff1a;数组与矩阵概念的区分数组&#xff1a;与其它编程语言一样&#xff0c;定义是&#xff1a;相同数据类型元素的集合。矩阵&#xff1a;在数学中&#xff0c;矩阵(Matri…

(组合数求模=乘法逆元+快速幂) Problem Makes Problem

题目&#xff1a; As I am fond of making easier problems, I discovered a problem. Actually, the problem is ‘how can you make n by adding k non-negative integers?’ I think a small example will make things clear. Suppose n4 and k3. There are 15 solutions.…

php做图书网站,基于PHP的图书馆网站管理系统的设计与实现

《现代图书情报技术 》 年 第 期 工作交流 总第 期基于 的图书馆网站管理系统的设计与实现 郑婷婷 张 羽 辽宁大学图书馆 沈阳 【摘要 】 阐述了在 下 , 使用 十 设计并实现 了图书馆网站管理系统 。 分系统平台如何搭建 、 系统结构如何设计以及最终如何实现三大部分 。 【关键…

(小费马定理降幂)Sum

题目&#xff1a; 分析与解答&#xff1a; 参考思路&#xff1a; https://www.cnblogs.com/stepping/p/7144512.html https://blog.csdn.net/strangedbly/article/details/50996908 根据隔板定理&#xff0c;把N分成一份的分法数为C(1,n-1)&#xff0c; 把N分成两份的分法…

温度 数值模拟 matlab,西安交通大学——温度场数值模拟(matlab)

西安交通大学材料制备与成型实验——温度场数值模拟,matlab编程温度场模拟matlab代码&#xff1a;clear,clc,clfL18;L28;N9;M9;% 边长为8cm的正方形划分为8*8的格子 T0500;Tw100; % 初始和稳态温度 a0.05; % 导温系数tmax600;dt0.2; % 时间限10min和时间步长0.2s dxL1/(M-1);dy…

matlab 参数识别,[转载]自编最小二乘法的Matlab参数辨识程序(含实例)

function [sysd,sys,err] ID(Y,U,Ts)%%基于递推最小二乘法的参数辨识程序%仅针对二阶系统&#xff1a;)%出处&#xff1a;http://blog.sina.com.cn/xianfa110%---------------%Inputs:%---------------%Y nX1 vector of your model output%U nX1 vector of your model input…

(回文串全排列个数) xiaoxin juju needs help

题目 As we all known, xiaoxin is a brilliant coder. He knew palindromic strings when he was only a six grade student at elementry school. This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gav…

让apache解析html里的php代码,让Apache解析html文件中的php语句

为什么要干这种事呢&#xff1f;原因在于:对于纯粹的网页来说(不涉及对于数据库的操作)&#xff0c;可以使用一些软件来生成html代码。推荐软件Axure但是&#xff0c;当生成html文件之后&#xff0c;你发现还要写php语句对数据库进行操作时&#xff0c;就会遇到一些问题。首先&…

(找循环节)Number Sequence

题目&#xff1a; A number sequence is defined as follows: f(1) 1, f(2) 1, f(n) (A * f(n - 1) B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case contains…

git获取管理员权限 windows,windows下git怎么管理权限

一、安装软件&#xff1a;msysGit-fullinstall-1.8.1.2打开之后设置安装路径&#xff0c;默认为C:\msysgit&#xff0c;如图&#xff1a;注意&#xff1a;如果要自定义安装路径&#xff0c;请不要安装在带有空格的路径以及含有中文的路径下点击“OK”以后开始安装&#xff0c;首…

(lucas) Saving Beans

题目&#xff1a; Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They suppose that…

java applet程序设计,Java Applet程序设计基础

Java Applet程序设计基础Java Applet 是用Java 语言编写的一些小应用程序&#xff0c;这些程序是直接嵌入到页面中&#xff0c;由支持Java的浏览器(IE 或 Nescape)解释执行能够产生特殊效果的程序。它可以大大提高Web页面的交互能力和动态执行能力。包含Applet的网页被称为Java…

(矩阵快速幂)解所有类似Fibonacci 的题目

Description In the Fibonacci integer sequence, F0 0, F1 1, and Fn Fn − 1 Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … An alternative formula for the Fibonacci sequence is Gi…