主要就是将最小生成树的边按字典序输出。

读取数据时,把较小的端点赋给u,较大的端点号赋值给v。 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小到大,用kruskal求出最小生成树。 第二次将求出的最小生成树的边在排序,这次只要按u、v从小到大排序即可。

#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; int index1;
int n,cost,idx,t;
int ans; struct Edge{
int u,v;
int cost; }edge[],MST[]; struct UF{
int father[]; void unit(){
for(int i=;i<=n;i++){
father[i]=i;
}
} int find_root(int x){
if(father[x]!=x)
father[x]=find_root(father[x]);
return father[x];
} void Union(int fa,int fb){
father[fb]=fa;
}
}uf; bool cmp1(const Edge t1,const Edge t2){
if(t1.cost!=t2.cost)
return t1.cost<t2.cost;
if(t1.u!=t2.u)
return t1.u<t2.u;
else
return t1.v<t2.v;
} bool cmp2(const Edge t1,const Edge t2){
if(t1.u!=t2.u)
return t1.u<t2.u;
else
return t1.v<t2.v;
} int main()
{
scanf("%d",&t);
for(int q=;q<t;q++){
scanf("%d",&n);
index1=;
for(int i=;i<=n;i++){
for(int j=;j<=i;j++)
scanf("%d",&cost);
for(int j=i+;j<=n;j++){
scanf("%d",&cost);
if(cost!=){
edge[index1].u=i;
edge[index1].v=j;
edge[index1].cost=cost;
index1++;
}
}
} sort(edge,edge+index1,cmp1);
uf.unit();
int count=;
idx=; for(int i=;i<index1;i++){
int u=edge[i].u;
int v=edge[i].v;
int fu=uf.find_root(u);
int fv=uf.find_root(v);
if(count==n-){
break;
}
if(fu!=fv){
MST[idx].u=u;
MST[idx].v=v;
MST[idx].cost=cost;
idx++;
count++;
uf.Union(fu,fv);
}
}
if(count<n-){
printf("-1\n");
}
else{
sort(MST,MST+idx,cmp2);
for(int i=;i<idx;i++){
//果真要按照下面的输入,否则为presentation error 。。。
if(i==)
printf("%d %d",MST[i].u,MST[i].v);
else
printf(" %d %d",MST[i].u,MST[i].v);
}
printf("\n");
}
}
return ;
}

ZOJ 3204 Connect them(字典序输出)的更多相关文章

  1. ZOJ - 3204 Connect them 最小生成树

    Connect them ZOJ - 3204 You have n computers numbered from 1 to n and you want to connect them to ma ...

  2. ZOJ 3204 Connect them MST-Kruscal

    这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的 因为是裸的 Kruscal ,所以就直接上代码了- Source Code : //#pragma comme ...

  3. ZOJ 3204 Connect them 继续MST

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目大意: 让你求最小生成树,并且按照字典序输出哪些点连接.无解输出-1 ...

  4. ZOJ 3204 Connect them(最小生成树+最小字典序)

    Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 t ...

  5. zoj 3204 Connect them(最小生成树)

    题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...

  6. zoj 3204 Connect them

    最小生成树,我用的是并查集+贪心的写法. #include<stdio.h> #include<string.h> #include<math.h> #includ ...

  7. poj 1041(字典序输出欧拉回路)

    John's trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8641   Accepted: 2893   Spe ...

  8. 二叉排序树:HUD3999-The order of a Tree(二叉排序树字典序输出)

    The order of a Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  9. poj 1041 John's trip——欧拉回路字典序输出

    题目:http://poj.org/problem?id=1041 明明是欧拉回路字典序输出的模板. 优先队列存边有毒.写跪.学习学习TJ发现只要按边权从大到小排序连边就能正常用邻接表了! 还有一种存 ...

随机推荐

  1. [转]ORACLE的ProC用法讲解

    pro*c是高级的用法,OCI是oracle的基础用法 如何编译.pc文件: proc code=cpp  parse=none iname=filename.pc oname=filename.cp ...

  2. meteor 安装 android sdk慢的改进方法

    网上方法很多,最后总结一下比较靠谱的一个,到~/.meteor/android_bundle/ 目录下, 执行tools/android,手动下载 API 19 和 intel X86 Atom Sy ...

  3. Python-memcached的基本使用

    想学Python,又想研究下memcached的客户端,于是拿Python-memcached研究研究~~~ 1.memcached的安装 请参考本博另一文章<Linux下安装memcached ...

  4. ios学习笔记之block在ios开发中的应用

    一.什么是Blocks      Block是一个C级别的语法以及运行时的一个特性,和标准C中的函数(函数指针)类似,但是其运行需要编译器和运行时支持,从ios4.0开始就很好的支持Block. 二. ...

  5. 为什么要用Message Queue

    摘录自博客:http://dataunion.org/9307.html?utm_source=tuicool&utm_medium=referral 为什么要用Message Queue 解 ...

  6. [转]ubuntu 14.04 系统设置不见了

    [转]ubuntu 14.04 系统设置不见了 http://blog.sina.com.cn/s/blog_6c9d65a10101i0i7.html 不知道删除什么了,系统设置不见了! 我在终端运 ...

  7. [转]流媒体协议介绍(rtp/rtcp/rtsp/rtmp/mms/hls)

    [转]流媒体协议介绍(rtp/rtcp/rtsp/rtmp/mms/hls) http://blog.csdn.net/tttyd/article/details/12032357 RTP       ...

  8. UISwitch swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  9. UIAlertControl swift

    let alertController = UIAlertController(title: "开始!", message: "游戏就要开始,你准备好了吗?", ...

  10. OC学习笔记之属性详解和易错点

    属性的概念在OC1.0中就存在,格式是定义实例变量,然后定义setter和getter方法,用点操作符操作属性 举例,类的接口部分 @interface Father : NSObject { NSI ...