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 ...
随机推荐
- 如何下载github项目中的部分文件(文件夹)
https://minhaskamal.github.io/DownGit/#/home 将你要下载的链接放进去即可.
- XAMPP安装报错及解决
FROM:http://www.zeeronsolutions.com/installing-xampp-on-windows-7-user-account-control-uac-warning-m ...
- 搭建 redis 3.2.8服务器
实验环境 redis 3.2.8 + RHEL 7.3 系统 软件下载地址 http://download.redis.io/releases/redis-3.2.8.tar.gz #注意,我的软件包 ...
- php获取checkbox数组的表单数据
提交表单的时候,对于checkbox多选框,name="field[]",此时php获取的数组为:从0开始的索引数组:如果name="field[n]" 有数字 ...
- 老李性能测试分享:可以没事代理刷榜赚外快了,poptest这是让你快速致富啊
老李性能测试分享:可以没事代理刷榜赚外快了,poptest这是让你快速致富啊 最近学员不断面试,不时听到令人惊喜的消息,类似应届专科毕业生获得7k月薪,小美女应聘月薪11k等等,看到学员开心的笑容 ...
- CentOS下安装node
下载 wget https://nodejs.org/dist/v7.7.4/node-v7.7.4-linux-x64.tar.gz 解压 tar -zxvf node-v7.7.4-linux-x ...
- 浏览器访问php脚本通过sendmail用mail函数发送邮件
前几天做项目遇到这样的一个问题:当某一个结点下有新的文章发表的时候,以邮件的形式通知该结点下的所有用户.这就需要用到邮件发送的功能. 因为项目是php语言做的,所以最简单的方法就是使用php自带的函数 ...
- Ubuntu常用软件安装(附带地址和卸载自带软件)
跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 上次说了安装VSCode(http://www.cnblogs.com/dun ...
- laravel安装插件laravel-ide-helper
1.插件位置laravel-ide-helper https://github.com/barryvdh/laravel-ide-helper 2.首先改变镜像源为国内的镜像源 P { margin- ...
- 文本挖掘预处理之向量化与Hash Trick
在文本挖掘的分词原理中,我们讲到了文本挖掘的预处理的关键一步:"分词",而在做了分词后,如果我们是做文本分类聚类,则后面关键的特征预处理步骤有向量化或向量化的特例Hash Tric ...