C - 畅通工程

Description

省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。 
 

Input

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

Output

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

Sample Input

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

Sample Output

3
?
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 105
struct edge{
    int a,b,len;
}cost[N*(N-1)/2];
int d[N],groups;
int find(int x)             
{
	return x==d[x]?x:d[x]=find(d[x]);
}
bool cmp(edge x,edge y)      
{
	return x.len>y.len?0:1;
}
void merge(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx==fy) return ;
		groups--;
	if(fx<fy)  
	  d[fy]=fx;
    else
      d[fx]=fy; 
}
int main()
{
	int n,min,m,p,q;
	while(~scanf("%d%d",&n,&m),n)
	{
		min=0;
		groups=m-1;
		for(int i=1;i<=m;i++)
		  d[i]=i;
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d%d",&cost[i].a,&cost[i].b,&cost[i].len);
			p=cost[i].a;q=cost[i].b;
		    merge(p,q);
		}
		
 	   for(int i=1;i<=m;i++)                  //再次初始化 
		  d[i]=i;                    
		  
		sort(cost+1,cost+n+1,cmp);                 

		for(int i=1;i<=n;i++)
		   if(find(cost[i].a)!=find(cost[i].b))  
		   {
   			   min+=cost[i].len;
   			   d[find(cost[i].a)]=cost[i].b;   
   		   }
       if(groups==0)                         //如果足以保证每点可以相连保持畅通输出最小值 
	    printf("%d\n",min);   
       else 
        printf("?\n");
	} 
}


### 关于畅通工程的C语言实现 为了满足题目需求,可以采用 **Kruskal算法** 来解决最小生成树问题。以下是基于 Kruskal 算法的一个完整的 C 语言实现方案。 #### 思路解析 1. 题目要求找到连接所有村庄所需的最低成本路径集合,这实际上是一个典型的最小生成树问题。 2. 使用并查集(Union-Find Set)来判断边是否形成环路,并维护连通分量的数量。 3. 对所有的边按照权重从小到大排序,依次选取不构成环路的边加入生成树中,直到所有节点都连通为止。 --- ```c #include <stdio.h> #include <stdlib.h> #define MAX_N 100 // 最大村庄数量 typedef struct { int u, v; // 边的两个端点 int cost; // 边的成本 } Edge; // 并查集结构体定义 int parent[MAX_N]; // 查找根节点函数 int find_set(int x) { if (parent[x] != x) { parent[x] = find_set(parent[x]); // 路径压缩优化 } return parent[x]; } // 合并两个集合 void union_set(int x, int y) { int rootX = find_set(x); int rootY = find_set(y); if (rootX != rootY) { parent[rootY] = rootX; } } // 按照cost升序排列边 int compare(const void *a, const void *b) { return ((Edge *)a)->cost - ((Edge *)b)->cost; } int main() { int N; // 村庄数目 while (scanf("%d", &N) && N != 0) { // 输入村庄数,当为0时退出循环 int M = N * (N - 1) / 2; // 计算可能的道路总数 Edge edges[M]; // 存储所有道路的信息 int total_cost = 0; // 初始化总成本为0 int connected_edges = 0; // 已经选入生成树中的边数 // 初始化并查集 for (int i = 1; i <= N; ++i) { parent[i] = i; } // 输入所有边的数据 for (int i = 0; i < M; ++i) { scanf("%d %d %d", &(edges[i].u), &(edges[i].v), &(edges[i].cost)); edges[i].cost *= !((edges[i].u == edges[i].v)); // 如果已建,则忽略其成本[^1] } // 排序所有边按成本升序排列 qsort(edges, M, sizeof(Edge), compare); // 构造最小生成树 for (int i = 0; i < M && connected_edges < N - 1; ++i) { int u_root = find_set(edges[i].u); int v_root = find_set(edges[i].v); if (u_root != v_root) { // 不在同一集合中,不会成环 union_set(u_root, v_root); // 合并集合 total_cost += edges[i].cost; // 加入总成本 connected_edges++; // 增加已选边计数 } } printf("%d\n", total_cost); // 输出最终结果 } return 0; } ``` --- ### 代码说明 1. **数据结构设计** - `Edge` 结构体用于存储每条边的相关信息:起点 (`u`)、终点 (`v`) 和成本 (`cost`)。 - 利用全局数组 `parent[]` 实现并查集操作,初始化时每个节点作为自己的父节点。 2. **核心逻辑** - 将所有边按成本升序排序,优先考虑低成本的边。 - 使用并查集检测当前边是否会引入环路,如果不会则将其纳入生成树中,并更新总成本。 3. **特殊情况处理** - 若某条边已经被修建(即状态为1),则直接跳过这条边的成本计算[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值