首页 > 试题广场 >

还是畅通工程

[编程题]还是畅通工程
  • 热度指数:14489 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。

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


输出描述:
    对每个测试用例,在1行里输出最小的公路总长度。
示例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
头像 帅呆呆~
发表于 2022-03-13 18:15:53
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int MAXN = 1000; struct Edge{ //定义边结构体 in 展开全文
头像 大内高手
发表于 2020-03-29 11:53:07
这是一道典型的最小生成树的题目, 利用Kruskal算法,先按照road的长度排序,若当前的road未在连接好的“城市组”里,则连通它。直到所有的城市全部连通,即所铺路径最短。 // runtime: 6ms // space: 488K #include <iostream> #inc 展开全文
头像 yyer
发表于 2023-03-15 09:12:51
#include <iostream> #include <algorithm> using namespace std; const int N=102; //边结点 struct Edge{ int from; int to; int length 展开全文
头像 总之就是非常浪漫
发表于 2023-03-01 09:57:05
#include <iostream> //#include <fstream> #include <queue> #include <algorithm> using namespace std; const int MAXN=100; struc 展开全文
头像 Youlinw
发表于 2024-02-24 15:21:54
#include <bits/stdc++.h> using namespace std; const int N = 5010; struct node { int a, b, w; } no[N]; int n, m; int p[N]; int find(int x) { i 展开全文
头像 在找对象的杨桃很想被叫靓仔
发表于 2024-03-22 21:09:48
#include <algorithm> #include <iostream> #include <vector> using namespace std; const int N = 100+10; int f[N] = {0}; int find(in 展开全文
头像 土尔逊Torson
发表于 2023-06-21 23:53:02
//土尔逊Torson 编写于2023/06/21 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <algorithm> using namespac 展开全文
头像 leventsg
发表于 2024-03-12 16:47:43
#include <iostream> #include <typeindex> #include <bits/stdc++.h> #include <vector> using namespace std; typedef struct edge{ 展开全文
头像 黑乎乎的酱猪肉
发表于 2024-03-28 21:49:19
最主要的还是并查集的使用。kruskal算法只不过是在和并集合的时候添加了条件。 #include <bits/stdc++.h> using namespace std; //定义的边 struct Edge { int a; int b; int weig 展开全文
头像 牛客784280659号
发表于 2024-03-06 21:08:27
#include <iostream> #include <algorithm> using namespace std; struct Edge{ int from; int to; int length; }; const int MAX=10 展开全文

问题信息

难度:
63条回答 8115浏览

热门推荐

通过挑战的用户

查看代码