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. JS处理时间相关

    <script>var d=new Date(); alert(d);alert(d.getMonth());alert(d.getHours());alert(d.getYear()); ...

  2. asp.net MVC4 学习(一)

    asp.net MVC 回顾 Html.ActionLink http://www.cnblogs.com/jiagoushi/p/3905828.html 选择基本模板,视图引擎 选择Razor A ...

  3. bzoj 4500: 矩阵【差分约束】

    (x,y,z)表示格子(x,y)的值为z,也就是x行+y列加的次数等于z,相当于差分约束的条件,用dfs判断冲突即可. #include<iostream> #include<cst ...

  4. java中WGS84坐标(ios)转换BD-09坐标(百度坐标)

    iPhone的GPS定位(CLLocationManager)获得的经纬坐标是基于WGS-84坐标系(世界标准),Google地图使用的是GCJ-02坐标系(中国特色的火星坐标系),百度的经纬坐标在G ...

  5. 报错Cannot determine embedded database driver class for database type NONE解决方法

    由于我不需要数据库,启动springboot报错: Cannot determine embedded database driver class for database type NONE If ...

  6. cmdb客户端服务器信息采集一

    #cmdb脚本程序一 #!/usr/bin/python # coding:utf-8 """ 采集机器自身信息 1 主机名 2 内存 3 ip与mac地址 4 cpu信 ...

  7. 2017 JUST Programming Contest 3.0 K. Malek and Summer Semester

    K. Malek and Summer Semester time limit per test 1.0 s memory limit per test 256 MB input standard i ...

  8. DP(DAG) UVA 437 The Tower of Babylon

    题目传送门 题意:给出一些砖头的长宽高,砖头能叠在另一块上要求它的长宽都小于下面的转头的长宽,问叠起来最高能有多高 分析:设一个砖头的长宽高为x, y, z,那么想当于多了x, z, y 和y, x, ...

  9. 题解报告:poj 1195 Mobile phones(二维BIT裸题)

    Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...

  10. 题解报告:hdu 1060 Leftmost Digit

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1060 问题描述 给定一个正整数N,你应该输出N ^ N的最左边的数字. 输入 输入包含多个测试用例. ...