最小生成树 TOJ 4117 Happy tree friends

本文介绍了一个关于最小生成树的问题,给出了具体的输入输出样例,并提供了一段使用Kruskal算法求解该问题的C++代码实现。文章重点在于如何处理特定边必须包含在最小生成树中的情况。
部署运行你感兴趣的模型镜像

链接http://acm.tju.edu.cn/toj/showp4117.html
4117.    Happy tree friends


Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 164    Accepted Runs: 60



yuebai has an undirected complete graph with n vertices. He wants to know the minimum spanning tree of the graph. It's so easy, so yuebai wants to challenge himself. He will choose one edge which must be in the spanning tree.

INPUT

There are multiple test cases.
For each test case, the first line contain an integer  n .
In the next  n  lines, there is an adjacency matrix  M Mij  denotes the weight of the edge  i  to  j .
Next line contains two dinstinct integer  u  and  v , which denotes the edge which is from  u  to  v  with the value  Muv  must be in the spanning tree.
(2n100,0Mij100) Mij=0  if and only if  i=j .

OUTPUT

For each case, print the result.

Sample Input


3
0 2 3
1 0 4
5 10 0
2 3

Sample Output


5

Hint

The edge of the spanning tree is 2->3 and 2->1



Source: TJU Team Selection 2015 Round B

用Kruskal做

只要把题目中要求的边先合和起来,其余按照模版来,题目说了无向,所以对于每边的长度取矩阵中的最小值(真是坑,一开始以为是最小树形图)

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

#define N 50005
int a[105][105];
struct graph{
  int x,y,wei;
}nodd[N];
int m,n,ufind[N];

int cmp(graph a1,graph a2){
  return a1.wei<a2.wei;
}
int find(int x){
  return ufind[x]==x? x : ufind[x]=find(ufind[x]);
}
int Kruskal(int a,int b){
	int ans=0;
	int i,j;
	for(i=1;i<=n;i++) ufind[i]=i;
	sort(nodd,nodd+m,cmp);
	ufind[a]=b;
	for(i=0;i<m;i++){
	  int x=find(nodd[i].x);  int y=find(nodd[i].y);
	  if(x!=y){
	    ans+=nodd[i].wei;
	    ufind[x]=y;
	  }
	}
	return ans;
}

int main(){
  int x;
  int sum;
  int temp;
  int a1,a2;
  int i,j,k;
  while(scanf("%d",&x)!=EOF){
  	 sum=0;
     temp=0;
     for(i=1;i<=x;i++)
       for(j=1;j<=x;j++) 
         scanf("%d",&a[i][j]);
     m=x*(x-1)/2;
     n=x;
     scanf("%d %d",&a1,&a2);
     sum+=a[a1][a2];
     a[a2][a1]=a[a1][a2];
     for(i=1;i<=x;i++)
       for(j=i+1;j<=x;j++){ 
	     nodd[temp].x=i;  nodd[temp].y=j;  nodd[temp].wei=min(a[i][j],a[j][i]);
	     temp++;
	   }
     //for(i=0;i<temp;i++) printf("%d %d %d\n",nodd[i].x,nodd[i].y,nodd[i].wei);
     printf("%d\n",Kruskal(a1,a2)+sum);
  }
}


您可能感兴趣的与本文相关的镜像

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 QueueForMcu 基于单片机实现的队列功能模块,主要用于8位、16位、32位非运行RTOS的单片机应用,兼容大多数单片机平台。 开源代码:https://.com/xiaoxinpro/QueueForMcu 一、特性 动态创建队列对象 动态设置队列数据缓冲区 静态指定队列元素数据长度 采用值传递的方式保存队列数据 二、快速使用 三、配置说明 目前QueueForMcu只有一个静态配置项,具体如下: 在文件 中有一个宏定义 用于指定队列元素的数据长度,默认是 ,可以根据需要更改为其他数据类型。 四、数据结构 队列的数据结构为 用于保存队列的状态,源码如下: 其中 为配置项中自定义的数据类型。 五、创建队列 1、创建队列缓存 由于我们采用值传递的方式保存队列数据,因此我们在创建队列前要手动创建一个队列缓存区,用于存放队列数据。 以上代码即创建一个大小为 的队列缓存区。 2、创建队列结构 接下来使用 创建队列结构,用于保存队列的状态: 3、初始化队列 准备好队列缓存和队列结构后调用 函数来创建队列,该函数原型如下: 参数说明: 参考代码: 六、压入队列 1、单数据压入 将数据压入队列尾部使用 函数,该函数原型如下: 参数说明: 返回值说明: 该函数会返回一个 枚举数据类型,返回值会根据队列状态返回以下几个值: 参考代码: 2、多数据压入 若需要将多个数据(数组)压入队列可以使用 函数,原理上循环调用 函数来实现的,函数原型如下: 参数说明: 当数组长度大于队列剩余长度时,数组多余的数据将被忽略。 返回值说明: 该函数将返回实际被压入到队列中的数据长度。 当队列中的剩余长度富余...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值