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. 关于使用kafka时对于大数据消息体是遇到的问题

    kafka对于消息体的大小默认为单条最大值是1M. 但是在我们应用场景中, 常常会出现一条消息大于1M, 如果不对kafka进行配置. 则会出现生产者无法将消息推送到kafka或消费者无法去消费kaf ...

  2. Swift4 协议

    创建: 2018/02/27 完成: 2018/02/28 更新: 2018/03/07 增加类采用协议时的注意 补充只有类, 结构体, 枚举型可以采用协议 增加为类定义准备的协议( protocol ...

  3. java中static,super,final关键字辨析

    1:static关键字 利:1)对对象的共享数据提供单独的空间存储. 2)修饰的方法可以直接被类名调用 弊:1)生命周期长. 2)访问出现限制(只能访问静态) 它可以有静态方法,静态类,静态变量 2: ...

  4. bzoj 4824: [Cqoi2017]老C的键盘【树形dp】

    参考:https://www.cnblogs.com/FallDream/p/bzoj4824.html 画一画就会发现关系形成了一棵二叉树(其实看到n-1就能想到 然后dp,设f[i][j]为点i在 ...

  5. [ZPG TEST 116] 最小边权和【生成树相关】

    先将输入的边从小到大排序,对于一条边,它一定连接着两个联通块u与v,那么这条变对于答案的贡献是siz[u] * siz[v] * (边权 + 1) - 1,别问为什么这太显然了,一想就懂... #in ...

  6. vijos P1629八 容斥原理

    https://vijos.org/p/1629 注意lcm要用LL 先给一个样例 1 2 1 10 思路.其实这题就是问,给定一堆数,要求不能整除其任意一个的数字有多少个. 容辞 + lcm dfs ...

  7. 微信小程序组件解读和分析:八、checkbox复选项

    checkbox复选项组件说明: checkbox是小程序表单组件中的一个组件,作用是在表单中引导用户做出选择. 要使用checkbox组件,还需要在同组中所有的checkbox标签外使用checkb ...

  8. SceneAction$$FastClassByCGLIB$$7330f7b9.invoke(int, Object, Object[]) line: not available

    现象:在调试状态下,断点可以进入ACTION ,当调用service的时候,发现无法进入service中的断点,就报了题目中的错误. 过程:1.降低JDK.因为本工程是用JDK1.6编译的,maven ...

  9. Request.Form("id")与Request.QueryString("id")的区别

    获取以表单提交的post类型的值,也就是获取<form method="post">这种方式提交的值request包含各种方式的值,没有指名是什麽,所以Request. ...

  10. VS2010 好用的javascript扩展工具

    工具1) JScript Editor Extensions 折叠代码 下载地址: JScript Editor Extensions 工具2) Javascript parser 以树形方式查的代码 ...