zoj3204 Connect them 最小生成树
题目链接: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 最小生成树的更多相关文章
- zoj3204 connect them 最小生成树 暴力
Connect them Time Limit: 1 Second Memory Limit:32768 KB You have n computers numbered from 1 to ...
- 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 ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- uvaoj 10397 - Connect the Campus【最小生成树】
uvaoj 10397 - Connect the Campus Many new buildings are under construction on the campus of the Univ ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- ZOJ 3204 Connect them(最小生成树+最小字典序)
Connect them Time Limit: 1 Second Memory Limit: 32768 KB You have n computers numbered from 1 t ...
随机推荐
- 从Properties得到数据到gson转换为json
从上一篇得到properties里的数据 Map<String,String> map = new HashMap<String,String>(); Enumeration& ...
- waypoints
http://imakewebthings.com/waypoints waypoints 滑冰122分钟 Cygwin http:/nxutils.sourceforge.net http://ba ...
- 《连载 | 物联网框架ServerSuperIO教程》- 17.支持实时数据库,高并发保存测点数据。附:3.4 发布与版本更新说明。
1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...
- 模块化规范Common.js,AMD,CMD
随着网站规模的不断扩大,嵌入网页中的javascript代码越来越大,开发过程中存在大量问题,如:协同开发,代码复用,大量文件引入,命名冲突,文件依赖. 模块化编程称为迫切的需求. 所谓的模块,就是实 ...
- Java Swing客户端小项目
记录一下两个用java swing写的客户端. 项目1: 关键词:swing jtable 代码如下: 1.主类: package com.my.agent.client; import java. ...
- VB中的GDI编程-1 设备环境DC
p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...
- SQL使用视图的优缺点
视图是为了查询方便!也就是多个表的总结!但是不能对视图增删改! 在做数据库开发中使用视图的优点有: 1.视图的好处就是在你做复杂的查询逻辑时可以简化你的思考过程. 2.用视图可以隐藏一定的信息,用过滤 ...
- Python with
简介 在编程中会经常碰到这种情况:有一个特殊的语句块,在执行这个语句块之前需要先执行一些准备动作:当语句块执行完成后,需要继续执行一些收尾动作.例如,文件读写后需要关闭,数据库读写完毕需要关闭连接,资 ...
- jquery控制图片切换
这种js现在用的很多.同时网上的js代码页很多.我直接从网上当了一个来用:代码如下: html <div class="bannerbox"> ...
- [Oracle]Audit(二)--清理Audit数据
在上一篇,初步了解了Audit的作用以及如何使用Audit,本篇记录如何手动清理Audit数据. (一) 概述 Audit的数据主要存储在sys.aud$表中,该表默认位于system表空间中,我们根 ...