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发现只要按边权从大到小排序连边就能正常用邻接表了! 还有一种存 ...
随机推荐
- JavaWeb之Servlet: ServletConfig 与 ServletContext
ServletConfig对象 什么是ServletConfig对象 ServletConfig对象,叫Servlet配置对象.主要用于加载配置文件的初始化参数. 创建时机 ServletConfig ...
- entityFramework使用 codefirst
新建项目 用nuget安装entityFramework,Install-Package Entityframework 建一个model和context //[Table("Custome ...
- Percona-Xtrabackup 2.3.3 慢查询不再堵塞备份(一)
在Percona-Xtrabackup 2.3.3[root@b28-19-17 bak]# rpm -q percona-xtrabackuppercona-xtrabackup-2.3.3-1.e ...
- oracle expdp impdp
一.不管导入还有导出都要先创建目录 1.创建目录 create directory my_dir as 'd:\yth';--生成目录(必须在指定位置先创建文件夹,名称最好与用户名一致) yth:是目 ...
- hdu 1874 畅通工程续
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...
- TextSwitcher,译为文字转换器控件
ViewSwitcher仅仅包含子类型TextView.TextSwitcher被用来使屏幕上的label产生动画效果.每当setText(CharSequence)被调用时,TextSwitcher ...
- ssh 安装
一 ssh 1.查看ssh服务的状态 输入以下命令: sudo service sshd status 如果出现 Loaded: error (Reason: No such file or dire ...
- Qt使用QStackedWidget实现堆栈窗口
Qt使用QStackedWidget实现堆栈窗口 分类: QT2012-07-25 21:59 6997人阅读 评论(0) 收藏 举报 qtlistsignal 堆栈窗口可以根据选择项的不同显示不同的 ...
- 54.xilinx_modelsim仿真错误1
在仿真DDR3核时,用modelsim编译时会出现下面错误 Error:can't read "env(XILINX)":no such variable 原因:在.do文件中指定 ...
- [无人值守安装操作系统]__RHEL6__FTP+TFTP+DHCP+Kickstart+PXE
实验环境 1.实验平台:VMware Workstation 10 2.实验OS:RHEL6 3.服务器A: (1) 10.0.10.158 (2) DHCP/FTP/TFTP (3) 有可使用的yu ...