ZOJ 3204 Connect them(最小生成树:kruscal算法)

大模型训练(1):流水线并行 Gpipe的方法是,在训练时计算和运用的是micro-batch里的均值和方差,但同时持续追踪全部mini-batch的移动平均和方差,以便在测试阶段进行使用。流水线并行的核心思想是:在模型并行的基础上,进一步引入数据并行的办法,即把原先的数据再划分成若干个更小的batch,分批送入GPU进行训练,算一部分就让下一个GPU动起来。从实验结果可知,在关掉NVlinks的情况下,Gpipe一样也能实现随着GPU数量的增加,训练速度也增加的效果。前文说过,随着模型的增加,每块GPU中存储的中间结果也会越大。 阅读详情

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3204


Connect them

Time Limit: 1 Second     Memory Limit:32768 KB

You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN).All connections are two-way (that is connecting computersi andj is the same as connecting computersj and i). The cost of connecting computeri and computerj is cij. You cannot connect some pairs of computers due to some particular reasons. You want to connect them so that every computer connects to any other one directly or indirectly and you also want to pay as little as possible.

Given n and each cij , find the cheapest way to connect computers.

Input

There are multiple test cases. The first line of input contains an integerT (T <= 100), indicating the number of test cases. ThenT test cases follow.

The first line of each test case contains an integern (1 <n <= 100). Thenn lines follow, each of which containsn integers separated by a space. Thej-th integer of thei-th line in thesen lines is cij, indicating the cost of connecting computersi andj (cij = 0 means that you cannot connect them). 0 <=cij <= 60000,cij = cji,cii = 0, 1 <=i, j <=n.

Output

For each test case, if you can connect the computers together, output the method in in the following fomat:

i1 j1 i1 j1 ......

where ik ik (k >= 1) are the identification numbers of the two computers to be connected. All the integers must be separated by a space and there must be no extra space at the end of the line.If there are multiple solutions, output the lexicographically smallest one (see hints for the definition of "lexicography small")If you cannot connect them, just output "-1" in the line.

Sample Input

2
3
0 2 3
2 0 5
3 5 0
2
0 0
0 0

Sample Output

1 2 1 3
-1
Hints:
A solution A is a line of p integers: a1, a2, ...ap.
Another solution B different from A is a line of q integers:b1,b2, ...bq.
A is lexicographically smaller than B if and only if:
(1) there exists a positive integer r (r <= p, r <=q) such thatai =bi for all 0 <i < r andar <br
OR
(2) p < q and ai = bi for all 0 <i <=p


题目大意:题目大意:有几台电脑,怎么用最少的费用把他

           们连接起来

   

解题思路:最小生成树问题。先将边从小到大排序,依次把

           边两端的点用并查集合并。需要注意的是,题目中

           给出的矩阵上三角和下三角是相同的,即只需处理

           一半就可以啦。


<span style="font-size:24px;">///知识点:最小生成树
 #include<iostream>
 #include<cstdio>
 #include<algorithm>
 #include<cstring>
 using namespace std;

 const int maxn=110;
 int f[maxn];
 struct Edge
 {
     int from,to;
     int w;
 }edge[maxn*maxn];
 int tol;
 Edge ans[maxn*maxn];
 int cnt;
 void addedge(int u,int v,int w)
 {
     edge[tol].from=u;
     edge[tol].to=v;
     edge[tol].w=w;
     tol++;
 }
 bool cmp1(Edge a,Edge b)
 {
     if(a.w!=b.w) return a.w<b.w;
     else if(a.from!=b.from) return a.from<b.from;
     else return a.to<b.to;
 }
 bool cmp2(Edge a,Edge b)
 {
     if(a.from!=b.from) return a.from<b.from;
     else return a.to<b.to;
 }
 int find(int x)
 {
     if(f[x]==-1) return x;
     return f[x]=find(f[x]);
 }
 void kruscal()
 {
     memset(f,-1,sizeof(f));
     cnt=0;
     for(int k=0;k<tol;k++)
     {
         int u=edge[k].from;
         int v=edge[k].to;
         int t1=find(u);
         int t2=find(v);
         if(t1!=t2)
         {
             ans[cnt++]=edge[k];
             f[t1]=t2;
         }
     }
 }
 int main()
 {
     int T;
     scanf("%d",&T);
     int n;
     while(T--)
     {
         scanf("%d",&n);
         tol=0;
         int w;
         for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
         {
             scanf("%d",&w);
             if(j<=i) continue;
             if(w==0) continue;
             addedge(i,j,w);
         }
         sort(edge,edge+tol,cmp1);
         kruscal();
         if(cnt!=n-1)
         {
             printf("-1\n");
             continue;
         }
         else
         {
             sort(ans,ans+cnt,cmp2);
             for(int i=0;i<cnt-1;i++)
                printf("%d %d ",ans[i].from,ans[i].to);
             printf("%d %d\n",ans[cnt-1].from,ans[cnt-1].to);
         }
     }
     return 0;
 }</span>



rk3588 适配音频解码芯片 ALC5616 在 RK3588 的高性能方案中,选择 **ALC5616**(来自 Realtek 瑞昱)通常意味着项目更侧重于**音频性能的稳定性**以及**全球供应链的通用性**。 阅读详情

相关推荐

泛微E8流程监控指南:如何用SQL实时追踪审批节点状态?

本文详细介绍了如何利用SQL实时监控泛微E8系统中的审批流程状态,包括核心数据表结构解析、全链路监控SQL实战以及智能预警机制设计。通过数据库直接查询和状态关联分析,帮助企业快速定位卡顿节点,提升流程效率。适用于需要优化OA系统流程管理的企业IT人员和业务主管。

weixin_30687051的博客 437

ZOJ-3204 Connect them(最小生成树)

ZOJ-3204 Connect them

Linoy 1045

Cesium实战:全球海洋动态水面与自定义区域波浪效果实现

本文详细介绍了在Cesium中实现全球海洋动态水面与自定义区域波浪效果的实战方法。通过解析核心概念Primitive与Geometry,并深入讲解Water材质的关键参数调节,指导开发者快速创建逼真的动态海洋。文章重点阐述了如何利用多边形坐标定义不规则图形区域,实现精准的局部水域波浪效果,并提供了性能优化与视觉提升的实用技巧。

weixin_29256771的博客 501

ZOJ3204:Connect them(字典序最小生成树)

Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are

第七殿堂 654

ArcFace vs 传统算法:实测MTCNN+ArcFace在PyCharm中的识别准确率(含完整对比数据)

本文通过PyCharm开发环境实测MTCNN+ArcFace组合在人脸识别中的性能表现,并与传统算法进行多维度对比。实验数据显示,ArcFace在LFW数据集上达到99.83%的准确率,显著优于传统方法。文章还提供了完整的代码实现和工程优化建议,为开发者提供实用的技术参考。

醉三国 233

Speedtree学习笔记(案例)——拥有多种材质的植物如何导入使用(以草为例)

注:Speedtree无论保存还是导入模型其存储路径不可有中文。 第一步拆分草的模型 根据所需的材质去拆分(例:假若草的果实,茎秆和叶子是不同的材质那么就需拆分为三份;假若画的全部颜色及材质一样就没有必要去拆分模型) 第二步导入草的模型 在Mesh面板中依次导入草的模型——Do nothing additional right now 第三步扩大草的生长区域范围(可省略)...

L人青的博客 5722

ZOJ 3204 Connect them

Description You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is the

Occult 669

定制PYNQ的overlay

一、用HLS生成一个定制IP(加速器)(1)首先打开vivado hls,本人的版本是2017.2,点击创建一个新工程。(2)设置工程名和路径,顶层函数设置为add,创建一个新的C++程序,名字叫做adder.cpp,不用在testbench中添加文件。选择板子的时候搜索xc7z020clg400-1,这是PYNQ的板子号,选择完成。             (3)在左侧的菜单栏里选择Sourc...

bramblewalls的博客 1万+

ZOJ 3204 Connect them(最小生成树+最小字典序)

Connect themTime Limit:1 Second Memory Limit:32768 KB You havencomputers numbered from 1 tonand you want to connect them to make a small local area network (LAN). All connections are two-w...

weixin_30488085的博客 166

AWS入门实践-如何在AWS云上创建一个内外网隔离的生产环境

在 AWS 上建立一个内外网分离的生产环境,可以减少应用服务的暴露面,有效的保证你的应用服务器的安全。通常我们会将web应用放在外网的子网内,数据库服务器等放在内网的子网。我们将按照下图来部署动手实践环境,实现在public subnet的EC2虚拟机可以直接通过互联网可以访问,在Private subnet的EC2虚拟机不能通过互联网直接访问,但是却可以在内部来访问互联网安装应用等等。

weixin_39108752的博客 2994

浅显易懂地看LLC变换器(三)

第3期 谐振与增益的定性关系 由简单的谐振电路说起 我们将以电感和电容的基本电路的特性逐渐了解谐振电路的特性 与电阻不同,电感和电容都不是纯阻性线性器件,电感的感抗XL和电容的容抗Xc都与频率有关,当加在电感和电容上的频率发生变化时,它们的感抗XL和容抗Xc会发生变化。 1) 如下图RL电路,当输入源Vin的频率增加时,电感的感抗增大,输出电压减小,增益Gain=Vo/Vin随频率增加而减小。 2) 如下图RC电路,相反,当输入源Vin的频率增加时...

iFTrue电力电子专栏 6736

zoj3204(最小生成树)

Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections a

yypurpose的博客 488

【IoT】 产品设计之结构设计:如何设计产品按键

1、硅胶按键功能实现有两种接触方式 1)硅胶按键按压在DOME片(锅仔片)上 通常用在手机、儿童手表手机、防水手机等防水设备上。 2)硅胶按键增加导电基,一种黑色的导电碳粒,实现回路导电 通常用在遥控器、电脑键盘、特殊设备上。 两种方式结构设计原理是一样的,按键的固定与配合间隙与行程要确保好。 2、硅胶遥控器按键的结构方式(导电基) 按键帽为大颗粒实心的,触点为黑色导电基,通过压注成型融合为一体,那么这种按键结构设计时需要注意那些呢? 最主要的是按键按压回弹的手感,由于没有DOME片实现

产品线负责人 8667

ZOJ 3204 Connect them MST-Kruscal

这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的 因为是裸的 Kruscal ,所以就直接上代码了~ Source Code : //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <io...

weixin_30839881的博客 169

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

ZOJ 3204 Connect them最小生成树

ZOJ 3204 Connect them 最小生成树,输出字典序最小的解。 先排序,用kruscal算法,输出的时候按照字典序排序。

remember_your_dream的博客 355

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

ZOJ 3204 Connect them(最短路kruscal)

Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two...

其实我是C先生--的博客 301

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

C C++最全ZOJ 3204 Connect them(最短路kruscal)_zoj3204 kruscal(2),C C++入门基础

/用到pair ,默认就是先按照first再是second。

2401_84975579的博客 407

C C++最新ZOJ 3204 Connect them(最短路kruscal)_zoj3204 kruscal,20道C C++工程师面试必问题

/用到pair ,默认就是先按照first再是second。

2401_84976691的博客 397

ZOJ 3204 Connect them 【最小生成树+输出选取的边】

Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections a

mengxiang000000 986

ZOJ 3204 Connect them(最小生成树)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3204 题意:给出一个图的链接矩阵,要求按照字典序最小的情况输出连接边 Prim算法加father数组记录父亲节点,更新时在距离相同的情况下维护父亲节点值最小,然后根据父亲节点和当前节点的大小创建边,最后排序并输出 #include #include #

宝树啊宝树 550

2815 Connect them

 Connect them时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte总提交: 7            测试通过: 1http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2815描述You have n computers n

技术土豆 534

1203 Swordfish

/*1203SwordfishTime Limit: 1 Second      Memory Limit: 32768 KBThere exists a world within our worldA world beneath what we call cyberspace.A world protected by firewalls,passwords and t

轻疯 清风 1053

zoj3204 Connect them 最小生成树 MST

这道题,有几个地方,来看看:#include #include typedef struct { int x,y; int w; }NODE; typedef struct { int x1; int x2; }xx; xx

1002

Connect them ZOJ - 3204(最小生成树)

You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is the same as conne...

码农的一亩三分地 275

zoj 3204 Connect them

最小生成树,我用的是并查集+贪心的写法。 #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 111; int c[maxn][maxn]; int fa...

weixin_33736048的博客 136

zoj 3204

整理下昨晚写的题。。wa了数次。。 /* zoj_3204 最小生成树 kruskal写的,最让我纠结的是priority_queue和vector一起出现而且两个都要重载过比较函数。。 写错了数次。。特别是priority_queue对<的重载,特别注意啊。。 注意:在

I am coming! 633
上一篇: ZOJ 3699 Dakar Rally(贪心)
下一篇: CodeForces 555C Case of Chocolate(STL:map的使用)
hellohelloC
博客等级 码龄12年 15粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值