首页 技术 正文
技术 2022年11月22日
0 收藏 384 点赞 3,786 浏览 1029 个字
题目描述:
    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
输入:

测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
    当N为0时,输入结束,该用例不被处理。

输出:

对每个测试用例,在1行里输出最小的公路总长度。

样例输入:
3
1 2 1
1 3 2
2 3 4
4
1 2 1
1 3 4
1 4 1
2 3 3
2 4 2
3 4 5
0
样例输出:
3
5
 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#define MAX 102
#define inf 0x3f3f3f3f
int flag[MAX];
int cost[MAX][MAX];
int lowCost[MAX]; int main(int argc, char const *argv[])
{
int n,m;
scanf("%d",&n);
while(n != ) {
int count = ;
m = n * (n-)/;
for(int i = ; i <= n; i++) {
flag[i] = ;
for(int j = ; j <= n; j++) {
cost[i][j] = inf;
}
}
for(int i = ; i < m; i++) {
int a,b,c,d;
scanf("%d %d %d",&a,&b,&c);
cost[a][b]= cost[b][a] = c; } int sumCost = ;
for(int i = ; i <= n; i++) {
lowCost[i] = cost[][i];
}
flag[] = ; for(int i = ; i <= n; i++) {
int min = inf;
int v = -;
for(int i = ; i <= n; i++) {
if(flag[i] == && lowCost[i] < min) {
min = lowCost[i];
v = i;
}
}
flag[v] = ;
sumCost = sumCost + lowCost[v]; for(int i = ; i <= n; i++) {
if(cost[v][i] < lowCost[i]) {
lowCost[i] = cost[v][i];
}
}
} printf("%d\n",sumCost); scanf("%d",&n);
} return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848