ZOJ 3204 Connect them(字典序输出)
主要就是将最小生成树的边按字典序输出。
读取数据时,把较小的端点赋给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(字典序输出)的更多相关文章
- 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 ...
- ZOJ 3204 Connect them MST-Kruscal
这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的 因为是裸的 Kruscal ,所以就直接上代码了- Source Code : //#pragma comme ...
- ZOJ 3204 Connect them 继续MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目大意: 让你求最小生成树,并且按照字典序输出哪些点连接.无解输出-1 ...
- ZOJ 3204 Connect them(最小生成树+最小字典序)
Connect them Time Limit: 1 Second Memory Limit: 32768 KB You have n computers numbered from 1 t ...
- zoj 3204 Connect them(最小生成树)
题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...
- zoj 3204 Connect them
最小生成树,我用的是并查集+贪心的写法. #include<stdio.h> #include<string.h> #include<math.h> #includ ...
- poj 1041(字典序输出欧拉回路)
John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8641 Accepted: 2893 Spe ...
- 二叉排序树: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) ...
- poj 1041 John's trip——欧拉回路字典序输出
题目:http://poj.org/problem?id=1041 明明是欧拉回路字典序输出的模板. 优先队列存边有毒.写跪.学习学习TJ发现只要按边权从大到小排序连边就能正常用邻接表了! 还有一种存 ...
随机推荐
- 使用dom4j技术对xml文件的基本操作
1.pojo类:Notice package com.green.notice.storage; import java.util.ArrayList; import java.util.List; ...
- SQL注入式攻击
百度百科:http://baike.baidu.com/link?url=GQbJ2amTzTahZA7XJSBDLYYkN3waQ9JCoJ0l--tCWlvKQibe0YaH4hpmgEnLyn0 ...
- Spring源码下载
Spring已经将源码从SVN迁移到了Github,而且也改为基于Gradle的构建来构建项目,它取代了之前的ANT+Ivy系统,所以要构建Spring源码要先安装Github和Gradle. 首先假 ...
- Android--WebView显示Html,让其中的图片适应屏幕宽度
//设置 防止图片太大超出屏幕 tv_web_danGe.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COL ...
- scjp考试准备 - 1 - 循环控制
判断如下代码最后的执行结果. public class Breaker{ static String o = ""; public static void main(String[ ...
- 【分享】生成带数据的Insert语句工具(源码)
这个工具用于导出带数据的Insert语句,方便在不同版本的数据库上导出数据.有人说sql server 2008自带导出带数据的Insert语句(连接)但是我的数据库版本没有这个选项,无奈之下自己简单 ...
- GitFlow教程
GitFlow教程 这份教程是博主学到的git基础,仅适合小团队使用,仅供参考 配置Git 配置github上面的账号,首先需要自己在git上注册一个账号 git config --global us ...
- CS小分队第一阶段冲刺站立会议(5月7日)
昨日完成任务:1.完成了游戏2048退出自动保存功能,进入自动读取存档功能, 2.完成游戏失败判断函数, 3.为游戏添加了背景音乐并且可以手动开关 遇到的困难:使用数据库时由于使用的ACCESS版本比 ...
- jQuery中ready与load事件
jQuery中ready与load事件(来自慕课网) jQuery有3种针对文档加载的方法 $(document).ready(function() { // ...代码... }) //docume ...
- Android:apk文件结构
Android apk文件,即Android application package文件. 每个要安装到Android平台的应用都要被编译打包为一个单独的文件,后缀名为.apk,其中包含了应用的二进制 ...