最小生成树 TOJ 4117 Happy tree friends

本文介绍了一个关于最小生成树的问题,给出了具体的输入输出样例,并提供了一段使用Kruskal算法求解该问题的C++代码实现。文章重点在于如何处理特定边必须包含在最小生成树中的情况。
Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

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

链接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/c7070e7537b1 [!NOTE] 每到答辩季我就会喜获stars,谢谢大家的支持! 欢迎推广传播本repo(https://.com/atomiechen/THU-PPT-Theme),也欢迎贡献变体,提供更多选择 清华简约主题PPT模板 Repo stars 2020年春夏之交,答辩期间很多同学都在寻找清华主题的答辩模板。 一方面有使用LaTeX制作Beamer的模板(见Overleaf上的模板THU Beamer Theme),另一方面民间也存在着一些PPT模板。 很多人可能不适应Beamer的使用(主要是内容和排版设计不是可见即所得,定制有门槛),以及我找到的PPT模板也都不太好使(要么图案设计太复杂、不好看,要么没有制作成PPT母版导致每次使用都要复制粘贴+微调,不方便)。 我制作了清华简约主题的PPT模板,后续有新的设计我会逐渐加入,也欢迎有兴趣有想法的朋友们添砖加瓦! 内容 所有模板均为 文件。 此外也提供转换脚本用于 Pandoc自动生成PPTX。 各个版本的修改历史见 CHANGELOG.md。 下载 推荐直接从 Releases 下载最新发布版。 也可以在 仓库 单独下载所需文件。 效果 16:9比例,v1留边、v1顶边、v3留边白底、v3顶边白底: demo 16:9比例,其他风格模板:v1扁平、v2扁平、v1暗光 demo2 其他变体设计参见 variants/README.md。 使用方式 可以基于所提供的文件自行修改内容,也可以在新建的PPT文稿中应用该模板。 后者在 MS Office 2019 For Mac 的 PowerPoint 里的具体使用方式为:首先选择幻灯片尺...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值