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

读取数据时,把较小的端点赋给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. 从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第三讲 WPF中 DataTemplate

    后面在我们这项目中会大量用到模板,主要指的是空间模板,数据模板会用得比较少,下面我想介绍下控件模板和数据模板,我看到有位大神写得比较不错,我整理了下,让大家能更好理解,供大家参考, 首先介绍 Data ...

  2. eth0: error fetching interface information: Device not found

    转载,原文出处:http://zh888.blog.51cto.com/1684752/775447 亲测有效,感谢作者!!! ----------------------------分割线----- ...

  3. MAC下安装与配置MySQL [转]

    一 下载MySQL 访问MySQL的官网http://www.mysql.com/downloads/ 然后在页面中会看到“MySQL Community Server”下方有一个“download” ...

  4. 一步一步实现Linux设备驱动的Helloworld模块

    学了那么多程序语言,总是有一个Hello world开头,不禁感叹Hello world的强大.呵呵,废话少说,咋们的故事当然要从这个Hello world开始. 先查看自己OS使用的内核版本[don ...

  5. 魔兽世界私服Trinity,从源码开始

    缘起因由 在一个无所事事的周末下午,突然想起魔兽世界,官方的账号很久没有上了,里面的大小号现在连满级都不是.以前曾经搭过传奇和星际争霸战网的私服自娱自乐,也听说过魔兽世界有开源的服务端模拟,既然兴致来 ...

  6. chmod命令用法

    指令名称 : chmod  使用权限 : 所有使用者  使用方式 : chmod [-cfvR] [--help] [--version] mode file...  说明 : Linux/Unix ...

  7. FPGA入门1

    FPGA入门知识介绍    近几年来,由于现场可编程门阵列(FPGA)的使用非常灵活,又可以无限次的编程,已受到越来越多的电子编程者的喜爱,很多朋友都想学习一些FPGA入门知识准备进行这个行业,现在关 ...

  8. 1. VS2010---简介

    VS2010 使用简要介绍 ------------------------------------------------- 1. 几个基本概念---源程序.目标程序和翻译程序. 源程序 就是我们用 ...

  9. 无法找到脚本文件 C:/Windows/explorer.exe:574323188.vbs

    今天打开电脑后电脑有点反常,在启动时没有运行 “局域网” 保护的程序,而且还他是 “无法找到脚本文件”如下图: 发现这个东西后,第一反应,拔掉网线.因为很有可能是中病毒了,当时就出了一身冷汗,到底是怎 ...

  10. 第三次作业,github的基本操作

    chengjiangtao@pc MINGW32 ~$ git config --global user.name "chengjiangtao" chengjiangtao@pc ...