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 = cjicii= 0, 1 <= ij <= 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 最小生成树的更多相关文章

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

    Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 t ...

  2. zoj 3204 Connect them(最小生成树)

    题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...

  3. ZOJ 3204 Connect them(字典序输出)

    主要就是将最小生成树的边按字典序输出. 读取数据时,把较小的端点赋给u,较大的端点号赋值给v. 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小 ...

  4. ZOJ 3204 Connect them MST-Kruscal

    这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的 因为是裸的 Kruscal ,所以就直接上代码了- Source Code : //#pragma comme ...

  5. zoj 3204 Connect them

    最小生成树,我用的是并查集+贪心的写法. #include<stdio.h> #include<string.h> #include<math.h> #includ ...

  6. ZOJ 3204 Connect them 继续MST

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目大意: 让你求最小生成树,并且按照字典序输出哪些点连接.无解输出-1 ...

  7. zoj 3204 最小生成树,输出字典序最小的解

    注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...

  8. zoj3204 connect them 最小生成树 暴力

    Connect them Time Limit: 1 Second      Memory Limit:32768 KB You have n computers numbered from 1 to ...

  9. ZOJ 1586 QS Network (最小生成树)

    QS Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

随机推荐

  1. vs2010 每行代码显示虚线

    快捷键:Ctrl+R,W或Ctrl+E,S,即可去除 或者是编辑菜单——高级——查看空白 VS12010代码编辑器横向滚动条 工具----选项-----文本编辑器---所有语言---右侧 自动换行去掉

  2. E20170524-gg

    Awesome   adj. 可怕的; 令人敬畏的; 使人畏惧的; 极好的;

  3. Unix\Linux | 总结笔记 | 邮件发送

    实验:在本地实现不同用户收发邮件 #root发送邮件 #stu 收邮件 #stu 查看邮件 并回复邮件 #root 查看stu回复的邮件

  4. _bzoj1500 [NOI2005]维修数列【真·Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 注意MAX-SUM的时候,不可以是空串. #include <cstdio> ...

  5. 229 Majority Element II 求众数 II

    给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 你的算法应该在O(1)空间中以线性时间运行. 详见:https://leetcode.com/problems/major ...

  6. 使用Dotfuscator保护.NET DLL加密DLL,防止DLL反编译

    1.下载地址 https://pan.baidu.com/s/1ztWlBxw1Qf462AE7hQJQRg 2.操作步骤 2.1安装后打开DotfuscatorPro软件,如下图所示: 2.2 选择 ...

  7. FastDFS的简单使用

    互联网中有海量的文件,比如电商网站有海量的图片文件,视频网站有海量的视频文件,如果使用传统的模式上传文件,肯定是不可取的.因此需要使用第三方服务器来存储图片 . 一.FastDFS简介 ​ FastD ...

  8. jQuery实现复选框的全选与全不选

    对于复选框的选中checked属性,实在是无力吐槽. 从上图可以看出,当复选框不设置checked属性时,默认没有被选中:其它三种情况,设置checked属性但不设置属性值即置空,或者将checked ...

  9. struts2的action是线程安全的,struts1的action不是线程安全的真正原因

    为什么struts2的action是线程安全的,struts1的action不是线程安全的? 先对struts1和struts2的原理做一个简单的讲解 对于struts1 ,当第一次**.do的请求过 ...

  10. vue2.0 自定义指令详解

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...