首页 > 试题广场 >

畅通工程

[编程题]畅通工程
  • 热度指数:6275 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。

输入描述:
    测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M (N, M < =100 );随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。


输出描述:
    对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
示例1

输入

3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100

输出

3
?
头像 牛客440904392号
发表于 2024-10-05 19:01:37
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class Main { private static int[] 展开全文
头像 用户抉择
发表于 2021-02-28 22:33:21
还是畅通工程+流通图=畅通工程 #include <stdio.h> typedef struct Edge{     int from,to,len; }Edge; Edge e[1000 展开全文
头像 位琬续
发表于 2021-03-12 19:20:13
这套模板适合所有使用并查集的题目,无论是检测是否连通,还是构造最小生成树。 节点怎么定义 1、利用节点ID定义;2、若节点输入为char,转换为1-n的整数;3、若没有给出明确的节点定义特征,可以使用输入顺序order表示两点,请看例题:Freckles 是否调用queue 1、若需要求最小生成树, 展开全文
头像 Huster水仙
发表于 2023-02-04 21:43:35
MST:最小生成树 Kruskal:借助并查集 定义边结构体,升序,依次合并不在同一连通集里的点 最后遍历所有结点,检查是否全部连通 #include<iostream> #include<algorithm> using namespace std; cons 展开全文
头像 尤姆
发表于 2023-03-16 10:42:17
#include<iostream> #include<cstdio> #include<vector> #include<algorithm> using namespace std; const int MAXN = 101; int father 展开全文
头像 Luka_2001
发表于 2024-02-13 12:58:21
#include <iostream> #include<algorithm> using namespace std; const int maxnum=101; int points[maxnum]; int height[maxnum]; struct path{ 展开全文
头像 lyw菌
发表于 2023-03-26 19:14:25
//这道题算是比较常规的题了,采用kruskal算法并利用并查集思想求最小生成树 #include "stdio.h" #include "queue" using namespace std; struct Edge{ int villageS;//源端的村庄编号 int vill 展开全文
头像 wbc990512
发表于 2021-01-26 13:20:23
最小生成树,kruskal,并查集 #include<stdio.h> #include<math.h> #include<stdlib.h> typedef struct Node{ int s; // 起点 int d; // 终点 i 展开全文
头像 marlin818
发表于 2024-03-15 20:03:16
#include <iostream> #include <cstring> using namespace std; const int N = 110, INF = 0x3f3f3f3f; int n, m; int d[N]; int g[N][N]; bool st[ 展开全文
头像 ponynice
发表于 2024-03-17 20:28:28
#include <algorithm> #include <iostream> #include <string> using namespace std; int S[101]; struct Edge{ int a,b; int cost; 展开全文