题目链接: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. KMP算法C语言实现。弄了好久才搞好。。。

    我的这个算法中数组的第一位没有像教材中那样用来存数组的大小,所以会有些许的不同.                                                             ...

  2. ConversionService接口

    spring已经为我们提供了一个既可以使用GenericConversionService,又可以注入Converter的类,那就是ConversionServiceFactoryBean.该类为我们 ...

  3. 大数据量场景下storm自定义分组与Hbase预分区完美结合大幅度节省内存空间

    前言:在系统中向hbase中插入数据时,常常通过设置region的预分区来防止大数据量插入的热点问题,提高数据插入的效率,同时可以减少当数据猛增时由于Region split带来的资源消耗.大量的预分 ...

  4. 老李案例分享:Weblogic性能优化案例

    老李案例分享:Weblogic性能优化案例 POPTEST的测试技术交流qq群:450192312 网站应用首页大小在130K左右,在之前的测试过程中,其百用户并发的平均响应能力在6.5秒,性能优化后 ...

  5. hdu 3032 Nim or not Nim? (sg函数打表找规律)

    题意:有N堆石子,每堆有s[i]个,Alice和Bob两人轮流取石子,可以从一堆中取任意多的石子,也可以把一堆石子分成两小堆 Alice先取,问谁能获胜 思路:首先观察这道题的数据范围  1 ≤ N ...

  6. cassandra高级操作之JMX操作

    需求场景 项目中有这么个需求:统计集群中各个节点的数据量存储大小,不是记录数. 一开始有点无头绪,后面查看cassandra官方文档看到Monitoring章节,里面说到:Cassandra中的指标使 ...

  7. Opencv4android的Android Studio项目配置及实例下载

    因为软件竞赛的项目会用到Opencv fo rAndroid,所以就研究了一下如何在Android Studio上配置Opencv4Android 环境概述: Android Studio 2.3 O ...

  8. less学习笔记(一)

    less的写法如下 .content { ul{ list-style: none; } li{ height: 25px; line-height: 25px; padding-left: 15px ...

  9. Webdriver API之操作(一)

    一. 控制浏览器 1. 控制浏览器大小 driver.set_window_size(480,800) #浏览器宽480,高800显示 dirver.maximize_window() #浏览器最大化 ...

  10. Java --- JSP2新特性

    自从03年发布了jsp2.0之后,新增了一些额外的特性,这些特性使得动态网页设计变得更加容易.jsp2.0以后的版本统称jsp2.主要的新增特性有如下几个: 直接配置jsp属性 表达式语言(EL) 标 ...