描述

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. Java变量的修饰符

    1.public public的类.类属变量及方法,包内及包外的任何类均可以访问: 2.protect protected的类.类属变量及方法,包内的任何类,及包外的那些继承了此类的子类才能访问: 3 ...

  2. C# 微信openid 用户信息

    前段demo index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  3. 八、Node.js-http模块

    JS代码如下: /* 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi,来处理客户端的请求相应. 不过对 N ...

  4. 正确处理类的复合关系------新标准c++程序设计

    假设要编写一个小区养狗管理程序,该程序需要一个“主人”类,还需要一个“狗”类.狗是有主人的,主人也有狗.假定狗只有一个主人,但一个主人可以有最多10条狗.该如何处理“主人”类和“狗”类的关系呢?下面是 ...

  5. C# winform调用类似按钮点击的事件时自带参数该怎么写

    //按钮事件 private void btn_Click(object sender, EventArgs e) {} //自己的函数 private void myFunc() { //程序中其他 ...

  6. C# Winform下一个热插拔的MIS/MRP/ERP框架12(数据处理基类)

    作为ERP等数据应用程序,数据库的处理是重中之重. 在框架中,我封装了一个数据库的基类,在每个模组启动或窗体启动过程中,实例化一个基类即可调用CRUD操作(create 添加read读取 update ...

  7. 将php命令加入cenos环境变量

    0.修改/etc/profile文件使其永久性生效,并对所有系统用户生效,在文件末尾加上如下两行代码 1.PATH=$PATH:/usr/local/webserver/php/bin:/usr/lo ...

  8. struts2的主要工作流程

    struts2的框架结构图 struts2的主要工作流程: 1.客户端请求一个HttpServletRequest的请求,如在浏览器中输入http://localhost: 8080/bookcode ...

  9. 【BZOJ4800】[CEOI2015 Day2]世界冰球锦标赛 (折半搜索)

    [CEOI2015 Day2]世界冰球锦标赛 题目描述 译自 CEOI2015 Day2 T1「Ice Hockey World Championship」 今年的世界冰球锦标赛在捷克举行.\(Bob ...

  10. SDUT OJ 顺序表应用2:多余元素删除之建表算法

    顺序表应用2:多余元素删除之建表算法 Time Limit: 3 ms Memory Limit: 600 KiB Submit Statistic Discuss Problem Descripti ...