描述

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.

输入

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 i and j (cij = 0 means that you cannot connect them). 0 <= cij <= 60000, cij = cji, cii = 0, 1 <= i, j <= n.

输出

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.

样例输入

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

样例输出

1 2 1 3
-1

提示

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 that ai = bi for all 0 < i < r and ar < br
OR
(2) p < q and ai = bi for all 0 < i <= p

题目来源

ZJCPC 2009

主要是选取权值最小的边构成树,kruskal+并查集

选完之后保存起来,还需要二次字典序排。

#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN=; int N,cnt;
int lis[MAXN];
struct node{
int u,v,w;
}edge[MAXN*];
struct r{
int a,b;
}ans[MAXN]; int find(int x){
int temp=lis[x];
while(temp!=lis[temp]){
temp=lis[temp];
}
return temp;
}
void merge(int x, int y){
lis[x]=y;
} bool cmp1(node a, node b){
if(a.w!=b.w)return a.w < b.w;
return a.u < b.u;
}
bool cmp2(r a, r b){
if(a.a!=b.a)return a.a < b.a;
return a.b < b.b;
} int kruskal(){
int flag=;
sort(edge,edge+cnt,cmp1);
for(int i=; i<cnt; i++){
int x=find(edge[i].u);
int y=find(edge[i].v);
if(x!=y){
merge(x,y);
ans[flag].a=edge[i].u;
ans[flag].b=edge[i].v;
flag++;
}
}
return flag;
}
int main(int argc, char *argv[])
{
int T,v;
scanf("%d",&T);
while(T--){
cnt=;
scanf("%d",&N);
for(int i=; i<=N; i++){
lis[i]=i;
}
for(int i=; i<=N; i++){
for(int j=; j<=N; j++){
scanf("%d",&v);
if(i<j && v>){
edge[cnt].u=i;
edge[cnt].v=j;
edge[cnt].w=v;
cnt++;
}
}
}
int f=kruskal();
if(f!=N-){
printf("-1\n");
}else{
int flag=;
sort(ans,ans+f,cmp2);
for(int i=; i<f; i++){
if(flag)printf(" ");
printf("%d %d",ans[i].a,ans[i].b);
flag=;
}
printf("\n");
}
}
return ;
}

TOJ 2815 Connect them (kruskal+并查集)的更多相关文章

  1. Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)

    题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0 ...

  2. Minimum Spanning Tree.prim/kruskal(并查集)

    开始了最小生成树,以简单应用为例hoj1323,1232(求连通分支数,直接并查集即可) prim(n*n) 一般用于稠密图,而Kruskal(m*log(m))用于系稀疏图 #include< ...

  3. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  4. hdu 1863 畅通工程(Kruskal+并查集)

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. POJ 3723 Conscription (Kruskal并查集求最小生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14661   Accepted: 5102 Des ...

  6. [CF891C] Envy - Kruskal,并查集

    给出一个 n 个点 m条边的无向图,每条边有边权,共 Q次询问,每次给出 \(k\)条边,问这些边能否同时在一棵最小生成树上. Solution 所有最小生成树中某权值的边的数量是一定的 加完小于某权 ...

  7. BZOJ3545 [ONTAK2010]Peaks kruskal 并查集 主席树 dfs序

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3545 题意概括 Description 在Bytemountains有N座山峰,每座山峰有他的高度 ...

  8. BZOJ3551 [ONTAK2010]Peaks加强版 kruskal 并查集 主席树 dfs序

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3551 题意概括 Description 在Bytemountains有N座山峰,每座山峰有他的高度 ...

  9. 习题:过路费(kruskal+并查集+LCA)

    过路费  [问题描述]在某个遥远的国家里,有 n 个城市.编号为 1,2,3,…,n.这个国家的政府修 建了 m 条双向道路,每条道路连接着两个城市.政府规定从城市 S 到城市 T 需 要收取的过路费 ...

随机推荐

  1. 多个fragment中重叠问题的解决方法

    这个方法适用性有限. 我的是一个mainActivity,然后下部四个按钮,点击时先隐藏所有的fragment,然后再new一个新的出来,如果存在,则直接显示出来,看上去一切都没有问题. 但是通过fr ...

  2. angular ng-content

    <p> child works! </p> <ng-content></ng-content> <app-child> 父组件投影 < ...

  3. C# WPF Webbrowser 强制所有网页链接在同一页面打开

    只要搞懂Winform的  WPF稍微改一改就可以了 主类:负责跳转的 using System; using System.Collections.Generic; using System.Com ...

  4. 使用sshkey,禁用密码登陆,以及git仓库的搭建

    使用sshkey,可以实现免密码登陆服务器,同时关闭ssh service的使用账号密码登陆功能即可 1.首先在客户机添加sshkey(如果是window系统的话需要安装shell终端工具,例如xsh ...

  5. Kylin -- Dup key found 问题

    kylin 构建 cube 时,抛出了如下的错误: org.apache.kylin.engine.mr.exception.HadoopShellException: java.lang.Runti ...

  6. 导出当前python安装了哪些第三方模块+批量安装python模块

    pip freeze > mokuai.txt  #导出你当前python环境里面有哪写第三方模块 pip install -r mokuai.txt #从文件里面批量安装模块

  7. Windowns DOS For 循环实例

    update_all.bat代码示例: @echo off echo ***************************************************************** ...

  8. 题解 P3717 【[AHOI2017初中组]cover】

    题目链接 本题的大致思路就是搜索. 将矩阵初始化成false.先把灯塔标记.在搜一遍灯塔能照到的点并标记.最后搜一遍找被灯塔标记的个数. 详细解释见题解. 题解走起. #include<bits ...

  9. 洛谷P3358 最长k可重区间集问题(费用流)

    传送门 因为一个zz错误调了一个早上……汇点写错了……spfa也写错了……好吧好像是两个…… 把数轴上的每一个点向它右边的点连一条边,容量为$k$,费用为$0$,然后把每一个区间的左端点向右端点连边, ...

  10. nodejs post 数据到php $_POST["content"]接收不到的问题

    今天写了一段代码,要用到ajax调用php的接口,因为是https的,所以ajax不能跨域,于是使用nodejs调用接口,但是传输数据后 $_POST["content"]接收不到 ...