题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367

题目就是简单的最小生成树的模板的应用,不过最小生成树可能不唯一,答案要求输出字典序最小

代码:

 #include<cstdlib>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 110
int n,tol;
int cnt;
class node
{
public:
int from;
int to;
int w;
};
node edge[maxn*maxn];
node ans[maxn*maxn];
int parent[maxn*maxn];
void addedge(int u,int v,int w)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].w=w;
tol++;
}
void UFset()
{
for(int i=;i<maxn*maxn;i++)
parent[i]=-;
}
int Find( int x)
{
int s;
for(s=x;parent[s]>=; s=parent[s]);
while(s!=x)
{
int tmp=parent[x];
parent[x]=s;
x=tmp;
}
return s;
}
void Union(int R1, int R2)
{
int root1=Find(R1);
int root2=Find(R2); int tmp=parent[root1]+parent[root2]; if(parent[root1]> parent[root2])
{
parent[root1]=root2;
parent[root2]=tmp;
}
else
{
parent[root2]=root1;
parent[root1]=tmp;
}
}
bool cmp1( node a, node b)
{
if(a.w!=b.w)return a.w<b.w;
else if(a.from!=b.from)return a.from<b.from;
else return a.to<b.to;
}
bool cmp2(node a, node b)
{
if(a.from!=b.from)return a.from<b.from;
else return a.to<b.to;
}
void Kruskal()
{
int num=;
int u,v;
UFset();
cnt=; for(int i=;i<tol;i++)
{
u=edge[i].from;
v=edge[i].to;
if(Find(u) != Find(v))
{
ans[cnt++]=edge[i];
Union(u,v);
} }
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
tol=;
int weight;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&weight);
if(j<=i) continue;
if(weight== ) continue;
addedge(i,j,weight);
}
sort(edge,edge+tol,cmp1); Kruskal(); if(cnt!=n-)
cout<<"-1"<<endl;
else
{
sort(ans,ans+cnt,cmp2);
cout<<ans[].from<<" "<<ans[].to;
for(int i=;i<cnt;i++)
cout<<" "<<ans[i].from<<" "<<ans[i].to;
cout<<endl;
}
}
return ;
}

zoj3204 Connect them 最小生成树的更多相关文章

  1. zoj3204 connect them 最小生成树 暴力

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

  2. 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 ...

  3. hdu 3371 Connect the Cities(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  4. hdu oj 3371 Connect the Cities (最小生成树)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  6. uvaoj 10397 - Connect the Campus【最小生成树】

    uvaoj 10397 - Connect the Campus Many new buildings are under construction on the campus of the Univ ...

  7. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

  8. HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)

    解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...

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

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

随机推荐

  1. 用async 解放你的大脑

    在js中,代码嵌套和代码回调非常常见,不仅编写麻烦而且异常反人类.让我等码农很是头痛 function () {     function () {         function () {     ...

  2. python服务器环境搭建(3)——参数配置

    前面我们已安装好了python服务器运行所需要的相关软件,而最重要最繁琐的就是参数配置,写这篇就踩了好多坑,花了好多时间,遇到了各种各样的问题.好了费话少说,直接进入本篇话题. PS:本人不是专业的运 ...

  3. Hive基础知识梳理

    Hive简介 Hive是什么 Hive是构建在Hadoop之上的数据仓库平台. Hive是一个SQL解析引擎,将SQL转译成MapReduce程序并在Hadoop上运行. Hive是HDFS的一个文件 ...

  4. h5开发app之在线生成二维码

    h5通过jquery和qrcode在线生成二维码 首先我们需要下载一个qrcode.js文件,然后依次引入jquery和qrcode文件. 1.创建一个输入框以便做演示使用: <input id ...

  5. 早期练手:功能相对比较完善的 js 计算器

    第一次发博客,就先发一个自己早期,刚开始学前端时,用js写的一个计算器吧,计算功能比较少,只有 + - * / ,不过其他功能还是比较完善的,比如: 输出结果后,连续按"=",可以 ...

  6. Linux简介与厂商版本上

    Linux简介与厂商版本   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 1. Linux简介 Linux可以有狭义和广义两种 ...

  7. Python入门(一):PTVS写Python程序,调试模式下input()提示文字乱码问题

    前两天写了Python入门(一),里面提到,使用VS2013+PTVS进行Python开发. 就在准备为第二篇写个demo的时候,发现了一个问题,各种解决无果,有些纠结 Python中输入函数是inp ...

  8. filter滤镜的使用

    刚开始学css,开始遇到filter不懂什么意思后来到网上查了,觉得解释的很全面,就把它抠下来,以便自己经常来看看. CSS滤镜的使用方法:filter:filtername(parameters) ...

  9. MySQL关于Duplicate entry '1' for key 'PRIMARY'错误

    今天复习MySQL遇到Duplicate entry '1' for key 'PRIMARY'错误. 原因是主键值为'1'的数据已经存在,主键是唯一的,不可重复.

  10. 自动生成数学题型二(框架struts2)题型如((a+b)*c=d)

    1. 生成题目 1.1 生成单个题目 public static String[] twoOperatorAndOperator(int num1, int num2) { double first ...