POJ 1251 Jungle Roads (最小生成树 prime )

2023-11-03

POJ 1251 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
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.
Output
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.
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个城市连接一起;每个城市后面的数字代表这个城市能和几个城市连接,然后又几个城市名和所要花费的费用。
解题思路:
求最小生成树。
prime代码如下:

#include <stdio.h>
#include <string.h>
#include<limits.h>
void Prime();
int a[305][305],b[305],c[305];//b[]表示权值,c[]表示是否能够构成最小生成树 
int i,j,n;
int main()
{
	int t,d;
    char A[2],B[2];
    while(scanf("%d",&n),n)
    {
        for(i=0; i<=n; i++)
        {
            for(j=0; j<=n; j++)
            {
            	if(i==j)
            	{
            		a[i][j]=0;
				}
                else 
				{
					a[i][j] = INT_MAX;
				}
			}
                
        }
        for(i=0; i<n-1; i++)
        {
            
            scanf("%s%d",A,&t);
            for(j=0;j<t; j++)
            {

                scanf("%s%d",B,&d);
                a[A[0]-'A'+1][B[0]-'A'+1] = a[B[0]-'A'+1][A[0]-'A'+1] = d;
            }
        }
        Prime();
    }
    return 0;
}
void Prime()
{
	int min,sum=0,p;
    memset(c,0,sizeof(c));//注意分配内存空间不然会出现语法错误 
    for(i=1; i<=n; i++)
    {
    	b[i] = INT_MAX;
	}
    b[1] = 0;
    c[0]=1;
    for(i=1; i<=n; i++)
    {
		p=-1;
        min=INT_MAX;
        for(j=1;j<=n;j++)
        {
            if(!c[j]&&b[j]<min)
            {
                min = b[j];
                p=j;
            }
        }
        c[p] =1;
        sum+=min;
        for(j=1;j<=n;j++)
        {
            if(!c[j]&&b[j]>a[p][j])
                b[j] = a[p][j];
        }
    }
    printf("%d\n",sum);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

POJ 1251 Jungle Roads (最小生成树 prime ) 的相关文章

  • qemu: could not load PC BIOS 'bios-256k.bin'

    qemu kvm 创建虚拟机时报错了 qemu could not load PC BIOS bios 256k bin 我在指定了BIOS后仍然不对 使用 find bios 256k bin 我发现 bios 256k bin是一个软连
  • 【shell】-exec和xargs

    目录 实现效果 参数说明 exec参数 xargs参数 exec和xargs 后执行多条语句 exec和xargs 执行自定义函数 如何正确组合 xargs bash c 和环境变量 exec和xargs的区别 exec和xargs的区别

随机推荐

  • C++primer U10 读书笔记 关联容器

    pair 类型 pair
  • 当出现jquery”ScriptResourceMapping时

    在使用MVC框架的时候出现这个问题 jquery ScriptResourceMapping 有以下几个参考步骤 1 添加引用 管理NuGet程序包 在搜索框中搜索jquery 版本有更新 在右侧点击安装jqu 安装后显示script文件
  • unity2D备忘志

    一 角色移动 unity里面的transform组件非常好用 transform right这种枚举值真的很方便 Vector2向量 控制移动方向 Input输入非常非常方便 后面章节有刚体移动 应用也很广泛 transform Trans
  • 质数判断算法

    有人做过这样的验算 1 2 1 41 43 2 2 2 41 47 3 2 3 41 53 于是就可以有这样一个公式 设一正数为n 则n 2 n 41的值一定是一个质数 这个式子一直到n 39时 都是成立的 但n 40时 其式子就不成立了
  • threejs使用tweenjs实现点击标签过渡到相应视角

    效果图 1 点击前 2 点击后 说明 效果就是我在给模型打标签时保存视角和坐标 点击标签的时候读取到坐标数据 再转动到对应视角 1 安装 TWEEN npm install save tweenjs tween js 2 在当前页引入 im
  • springboot整合springcache (redis)

    1 引入依赖
  • 阿里巴巴的18位创始人

    1999年 阿里巴巴集团成立 当时共有18位创始人 大部分是马云的同事 朋友和学生 这篇文章汇总了这18个人的公开资料 马云是阿里巴巴的代言人 然而 事实上 自1999年成立以来 还有17位重要人物共同创立了这家电子商务巨头 但是他们是谁
  • 微信小程序 scroll-view 组件的 bindscroll 不触发不生效

    使用微信小程序基础组件中的scroll view 但是滑动的时候 bindscroll 一直不生效
  • 授人以渔command not found: ***

    配置环境变量是每个开发人员绕不开的初级本领 搜了一下大多数博客都是列出自己系统配置的步骤 授人以鱼不如授人以渔 今天记录一下自己配置验证的方法过程 方便初学者配置 本文围绕 我在macOS配置http server的探究验证过程 1 下载
  • CMD 命令和 ENTRYPOINT 命令的区别

    目录 CMD 命令 CMD shell 形式 1 创建 Dockerfile1 2 构建和运行新镜像 3 覆盖 CMD 4 添加命令选项 CMD exec形式 1 创建Dockerfile2 构建和运行新镜像 2 覆盖 CMD和添加命令选项
  • nginx 常用指令 try_files allow root alias

    nginx 常用指令 try files allow root alias 正则匹配条件 为区分大小写匹配 为不区分大小写匹配 和 分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配 其中 f和 f用来判断是否存在文件 d和 d用来
  • Transformer:SegFormer个人总结

    文章目录 前言 一 创新点 二 算法原理 2 1 总体框架 2 2 分层的Transformer结构 2 2 1 Hierarchical Feature Representation 2 2 2 Overlapped Patch Merg
  • CVE-2013-2028 经典栈溢出漏洞复现资料整理

    一个经典的由整数溢出导致栈溢出的漏洞 下面感觉写的有点乱 复现漏洞 CVE 2013 2028 nginx 栈溢出漏洞 相关基础知识 栈的基础知识 https ctf wiki github io ctf wiki pwn linux st
  • C语言——void指针、NULL指针、指向指针的指针、常量和指针

    目录 一 void指针 二 NULL指针 三 指向指针的指针 1 指向指针的指针 2 指针数组和指向指针的指针 四 常量和指针 1 常量 2 指向常量的指针 3 常量指针 4 指向 指向常量的常量指针 的指针 一 void指针 void指针
  • Centos7软件安装系列【九】安装postgresql

    安装 cd home software tar xzvf postgresql 9 6 8 tar gz cd postgresql 9 6 8 configure prefix usr local postgresql without r
  • 不连续1的子串,据说中大2019机试真题?

    推荐一个网站 N诺 https www noobdream com 似乎很多高校的真题都有诶 大家可以去试一试 题目描述 串只包含0或者1 给定一个数字 输出以此为长度的01串不含连续1的串的个数 如输入3 则输出5 因为长度为3的01串不
  • 深度学习-tensorflow对花的品种进行分类

    深度学习 tensorflow对花的品种进行分类 这里会展示如何对花的图像进行分类 它使用keras创建一个图像分类器 顺序模型 并使用预处理 image dataset from directory加载数据 主要的流程就是加载数据集 识别
  • 目前的网络情况与特点

    现有网络无法进展安全管理与控制 缺乏可管理与安全性 一旦 网络出现病毒与网络攻击现象 将会涉与到个别部门部数据丢失与影 响相关的业务运作 1 1 1 采用普通傻瓜式交换机 目前全所各部门采用的交换机根本上为 TP LINK D LINK 1
  • 安规电容的知识

    安规电容是指用于这样的场合 即电容器失效后 不会导致电击 不危及人身安全 它包括了X电容和Y电容 x电容是跨接在电力线两线 L N 之间的电容 一般选用金属薄膜电容 Y电容是分别跨接在电力线两线和地之间 L E N E 的电容 一般是成对出
  • POJ 1251 Jungle Roads (最小生成树 prime )

    POJ 1251 Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem A burst of foreign aid money was