描述

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中公用类型Car必须在它自己的文件中定义

    熟悉java的过程中发现了一些小问题,定义的类Car老是提示必须在它自己的文件中定义.自己想了想试试把Car继承的类Vehicle中的public换到Car类中,结果发现输出问题很大.它只显示了一个输 ...

  2. Arduino I2C + 数字式环境光传感器BH1750FVI

    BH1750FVI是日本罗姆(ROHM)半导体生产的数字式环境光传感IC.其主要特性有: I2C数字接口,支持速率最大400Kbps 输出量为光照度(Illuminance) 测量范围1~65535 ...

  3. Android 自定义ViewGroup,实现侧方位滑动菜单

    侧方位滑动菜单 1.现在adnroid流行的应用当中很多都是用的侧方位滑动菜单如图:

  4. Replication--复制问答

    在发布表尾部增加字段,需要重新初始化订阅么?答:在发布表尾部增加字段,不需要不需要重新初始化订阅,该修改会自动同步到订阅段,也不需要对复制做任何修改.但如果在同一个发布中增加新的项目,需要重新初始化订 ...

  5. asp.net mvc 中通过url字符串获取controller和action

    在项目中遇到需要通过url字符串获取controller和action的情况,百度了 一下找到了一个可以用的方法 ,在这里分享和记录一下 这个方法是在博客园的博问里看到的 原文地址是http://q. ...

  6. MSSQL中数据库对象类型解释

    public string GetObjectTypeName(object oType) { switch (oType+"") { case "U": re ...

  7. 和Webbrowser进行简单交互

    作为第一篇,简单的控件使用就不说了. 直接从简单的交互开始吧! C#使用网页中已有的js函数 webBrowser.Document.InvokeScript("Stop");// ...

  8. 《spring 攻略》笔记1

    chapter1 spring简介 两种spring ioc容器实现类型: BeanFactory ApplicationContext 应用程序上下文 DI技巧: @Autowired(requir ...

  9. springMVC 学习笔记(一):springMVC 入门

    springMVC 学习笔记(一):spring 入门 什么是 springMVC springMVC 是 spring 框架的一个模块,springMVC 和 spring 无需通过中间整合层进行整 ...

  10. iOS开发时间戳与时间NSDate,时区的转换,汉字与UTF8,16进制的转换

    http://blog.sina.com.cn/s/blog_68661bd80101njdo.html 标签: ios时间戳 ios开发时间戳 ios16进制转中文 ios开发utf8转中文 ios ...