ZOJ - 3204 Connect them 最小生成树
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 connecting computers j and i). The cost of connecting computer i and computer j 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 integer T (T <= 100), indicating the number of test cases. Then T test cases follow.
The first line of each test case contains an integer n (1 < n <= 100). Then n lines follow, each of which contains n integers separated by a space. The j-th integer of the i-th line in these n lines is cij, indicating the cost of connecting computers iand j (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.
<b< dd="">
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 题意:最小生成树,但要求最小字典序的解。
那么变化就是在cmp的函数书写的,不能仅仅的比较value,还要把from,to按字典序的顺序来加入,当然输出也是如此。
所以重写两个cmp函数就可以了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=;
int T,x;
int n,p,q;
int cnt,sum;
struct Node
{
int from,to;
double value;
}node[maxn*maxn],ans[maxn*maxn];
int fa[maxn];
bool cmp(Node a,Node b)
{
if(a.value!=b.value)
return a.value<b.value;
else if(a.from!=b.from)
return a.from<b.from;
else
return a.to<b.to;
}
bool cmpp(Node a,Node b)
{
if(a.from==b.from)
return a.to<b.to;
else
return a.from<b.from;
}
void init()
{
for(int i=;i<maxn;i++)
fa[i]=i;
}
int findd(int x)
{
if(fa[x]==x)
return x;
else
return fa[x]=findd(fa[x]);
}
void Kruskal()
{
sum=;
for(int i=;i<=cnt;i++)
{
int fx=findd(node[i].from);
int fy=findd(node[i].to);
if(fx!=fy)
{
ans[sum++]=node[i];
fa[fx]=fy;
}
}
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
cnt=;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&x);
if(x==||j<=i)
continue;
cnt++;
node[cnt].from=i;node[cnt].to=j;
node[cnt].value=x;
}
}
init();
sort(node+,node+cnt+,cmp);
sum=;
Kruskal();
if(sum!=n-)
{
printf("-1\n");
continue;
}
else
{
sort(ans,ans+sum,cmpp);
for(int i=;i<sum-;i++)
printf("%d %d ",ans[i].from,ans[i].to);
printf("%d %d\n",ans[sum-].from,ans[sum-].to);
}
}
return ;
}
ZOJ - 3204 Connect them 最小生成树的更多相关文章
- ZOJ 3204 Connect them(最小生成树+最小字典序)
Connect them Time Limit: 1 Second Memory Limit: 32768 KB You have n computers numbered from 1 t ...
- zoj 3204 Connect them(最小生成树)
题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...
- ZOJ 3204 Connect them(字典序输出)
主要就是将最小生成树的边按字典序输出. 读取数据时,把较小的端点赋给u,较大的端点号赋值给v. 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小 ...
- ZOJ 3204 Connect them MST-Kruscal
这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的 因为是裸的 Kruscal ,所以就直接上代码了- Source Code : //#pragma comme ...
- zoj 3204 Connect them
最小生成树,我用的是并查集+贪心的写法. #include<stdio.h> #include<string.h> #include<math.h> #includ ...
- ZOJ 3204 Connect them 继续MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目大意: 让你求最小生成树,并且按照字典序输出哪些点连接.无解输出-1 ...
- zoj 3204 最小生成树,输出字典序最小的解
注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...
- zoj3204 connect them 最小生成树 暴力
Connect them Time Limit: 1 Second Memory Limit:32768 KB You have n computers numbered from 1 to ...
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
随机推荐
- 2-12 tensorflow运算原理
#opencv tensorflow #类比 语法 api 原理 #基础数据类型 运算符 流程 字典 数组 import tensorflow as tf #data1 = tf.constant(2 ...
- asp.net调用oracle存储过程
oracle内的存储过程是通过游标返回结果集的 DataTable dt = new DataTable(); OracleParameter[] paras = ]; paras[] = new O ...
- cardboard sdk for unity 下载地址
https://github.com/googlesamples/cardboard-unity
- 又一篇Centos7下的asp.net core部署教程
历程2个多月的学习,我终于从PHP转.Net开发了. 从壹开始前后端分离[ .NETCore2.1 +Vue 2 +AOP+DI]框架 感谢老张的博客,我对asp.net core入门主要就是靠他的博 ...
- 对dynamic和lambda的学习
var, object, dynamic的区别以及使用 dynamic(2) – ExpandoObject的使用 .NET中的Lambda表达式与匿名方法
- 简单备份11g db (文件系统)
1.more check.sqlsqlplus / as sysdba << EOF!banner start dbstartupselect name from v\$database; ...
- sed附加命令
追加命令(命令a) sed '[address] a the-line-to-append' input-file 在第二行后面追加一行(原文这里可能有问题,没有写名行号) [root@sishen ...
- 转】MongoDB主从复制实验 master/slave
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/4/ 感谢! Posted: May 31, 2013 Ta ...
- Android与H5互调(通过实例来了解Hybrid App)
前些日子,Android原生开发将被取缔的吵得火热,JavaScript是能做一个完全的APP,但只使用JavaScript做出来的APP也不会牛逼到哪里去.最好的是混合(Hybrid)开发,在需要的 ...
- [Windows Server 2008] PHP安装Memcached
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:Windows ...