题意:裸最小生成树,主要是要按照字典序。

思路:模板

prim:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; #define INF 0x7fffffff
#define MAXN 128
bool vis[MAXN];
int lowu[MAXN];//记录起始边(已加入集合中的边)
int lowc[MAXN]; struct Edge{
int u,v;
};
Edge ans[MAXN*MAXN];
int cnt;//边数 bool cmp(Edge a,Edge b){
if(a.u!=b.u)return a.u<b.u;
return a.v<b.v;
} bool prim(int cost[][MAXN],int n){//标号从0开始
int i,j,minc,p;
memset(vis,false,sizeof(vis));
vis[]=true;
for(i=;i<n;++i){
lowu[i]=;//起始边都为0
lowc[i]=cost[][i];
}
for(i=;i<n;++i){
minc=INF;
p=-;
for(j=;j<n;++j)
if(!vis[j]&&(lowc[j]<minc||(lowc[j]==minc&&lowu[j]<p))){//字典序
minc=lowc[j];
p=j;
}
if(minc==INF)return false;//原图不连通 if(lowu[p]<p){
ans[cnt].u=lowu[p]+;
ans[cnt++].v=p+;
}
else{
ans[cnt].u=p+;
ans[cnt++].v=lowu[p]+;
} vis[p]=true;
for(j=;j<n;++j)
if(!vis[j]&&(cost[p][j]<lowc[j]||(cost[p][j]==lowc[j]&&p<lowu[j]))){//字典序
lowu[j]=p;//起始边变为p
lowc[j]=cost[p][j];
}
}
return true;
} int main(){
int t;
int n,m,a,b,w,i,j;
int cost[MAXN][MAXN];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
//m=n*(n-1)/2;//m边条数
for(i=;i<n;++i)
for(j=;j<n;++j){
scanf("%d",&w);
if(w==)w=INF;
cost[i][j]=w;
} cnt=;
if(prim(cost,n)){
sort(ans,ans+cnt,cmp);//字典序
for(i=;i<cnt-;++i)
printf("%d %d ",ans[i].u,ans[i].v);
printf("%d %d\n",ans[i].u,ans[i].v);
}
else printf("-1\n");
}
return ;
}

kruskal:注意sort排序是不稳定排序,那么cmp中的w相同时怎么排要指出。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; #define MAXN 110//最大点数
#define MAXM 10000//最大边数
int F[MAXN];//并查集使用 struct Edge{
int u,v,w;
}edge[MAXM];//存储边的信息,包括起点/终点/权值
int tol;//边数,加边前赋值为0
int cnt;//计算加入的边数
Edge ans[MAXM];//存储结果 void addedge(int u,int v,int w){
edge[tol].u=u;
edge[tol].v=v;
edge[tol++].w=w;
} //排序函数,将边按照权值从小到大排序
bool cmp(Edge a,Edge b){//sort排序不稳定
if(a.w!=b.w)return a.w<b.w;
if(a.u!=b.u)return a.u<b.u;
return a.v<b.v;
} bool cmp2(Edge a,Edge b){
if(a.u!=b.u)return a.u<b.u;
return a.v<b.v;
} int find(int x){
if(F[x]==-)return x;
return F[x]=find(F[x]);
} //传入点数,返回最小生成树的权值,如果不连通返回-1
void kruskal(int n){
memset(F,-,sizeof(F));
sort(edge,edge+tol,cmp); int i,u,v,w,t1,t2;
for(i=;i<tol;++i){
u=edge[i].u;
v=edge[i].v;
w=edge[i].w;
t1=find(u);
t2=find(v);
if(t1!=t2){
ans[cnt++]=edge[i];
F[t1]=t2;
}
if(cnt==n-)break;
}
// if(cnt<n-1)return -1;//不连通
// return ans;
} int main(){
int t;
int n,m,a,b,w,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
//m=n*(n-1)/2;//m边条数
tol=;cnt=;
for(i=;i<=n;++i)
for(j=;j<=n;++j){
scanf("%d",&w);
if(j<=i)continue;
if(w==)continue;
addedge(i,j,w);
} kruskal(n);
if(cnt!=n-)printf("-1\n");
else{
sort(ans,ans+cnt,cmp2);//此处控制输出排序
for(i=;i<cnt-;++i)
printf("%d %d ",ans[i].u,ans[i].v);
printf("%d %d\n",ans[i].u,ans[i].v);
}
}
return ;
}

zoj 3204 Connect them(最小生成树)的更多相关文章

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

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

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

  3. ZOJ 3204 Connect them(字典序输出)

    主要就是将最小生成树的边按字典序输出. 读取数据时,把较小的端点赋给u,较大的端点号赋值给v. 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小 ...

  4. ZOJ 3204 Connect them MST-Kruscal

    这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的 因为是裸的 Kruscal ,所以就直接上代码了- Source Code : //#pragma comme ...

  5. zoj 3204 Connect them

    最小生成树,我用的是并查集+贪心的写法. #include<stdio.h> #include<string.h> #include<math.h> #includ ...

  6. ZOJ 3204 Connect them 继续MST

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目大意: 让你求最小生成树,并且按照字典序输出哪些点连接.无解输出-1 ...

  7. zoj 3204 最小生成树,输出字典序最小的解

    注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...

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

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

  9. ZOJ 1586 QS Network (最小生成树)

    QS Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

随机推荐

  1. 【索引】理解MySQL——索引与优化

    MySQL 索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索 ...

  2. Yii2之创建定时任务

    yii开发的项目需要使用定时任务其实也可以使用一些单独的脚本文件来完成,但若是定时任务代码中需要使用到项目中的一些类,特别是需要使用应用对象Yii::$app的时候,单独的脚本想要完成就比较麻烦了.这 ...

  3. Golang中的panic和recover(捕获异常)

    func panic(interface{})和func recover() interface{}是Golang中用于错误处理的两个函数. panic的作用就是抛出一条错误信息,从它的参数类型可以看 ...

  4. python(3)- 常用快捷键及基础命令

  5. 真的了解CDN服务吗?

    Im CDN,WOW,COOL CDN知识详解 全称:Content Delivery Network或Content Ddistribute Network,即内容分发网络,CDN是构建在网络之上的 ...

  6. [JSOI2016]反质数序列

    我竟然半个小时切了一道JSOI2016,,,,不敢相信. 首先可以发现,如果N个数中1出现的次数<=1的话,我们按不能在一个集合连无向边的话,连出的一定是一个二分图. 接下来我来证明一下: 因为 ...

  7. java集合系列之LinkedList源码分析

    java集合系列之LinkedList源码分析 LinkedList数据结构简介 LinkedList底层是通过双端双向链表实现的,其基本数据结构如下,每一个节点类为Node对象,每个Node节点包含 ...

  8. BIM

    BIM进入中国已经有十来个年头,随着对BIM概念的深入了解.当前国内BIM应用逐渐由三维模型的可视化应用升级为基于BIM模型的信息进行项目精细化动态管理. 传统粗放的项目管理方法是工程项目难以进行精细 ...

  9. Unity3d插件]EasyTouch简单使用方法

    EasyTouch使用 EasyTouch 文件夹[-] 一.效果图 二.操作步骤 1.官方文档上的步骤 2.翻译一下以上的步骤 3.依据官方的这些提示.自己来做一个属于自己的人物遥感控制 对于移动平 ...

  10. 【LeetCode-面试算法经典-Java实现】【010-Regular Expresssion Matching(正則表達式匹配)】

    [010-Regular Expresssion Matching(正則表達式匹配)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Implement regular ...