这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的

因为是裸的 Kruscal ,所以就直接上代码了~

Source Code :

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const double MINN = -999999999.9;
const int INF = 0x3f3f3f3f ;
const int MAXN = ; int root[MAXN];
struct Edge {
int from,to;
int w;
} edge[MAXN * MAXN]; int tol;
Edge ans[MAXN * MAXN];
int cnt; void addedge (int u, int v, int w) {
edge[tol].from = u;
edge[tol].to = v;
edge[tol].w = w;
++tol;
} bool cmp1 (Edge a, Edge 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 (Edge a, Edge b) {
if (a.from != b.from) return a.from < b.from;
else return a.to < b.to;
} int find (int x) {
if (root[x] == -) return x;
return root[x] = find (root[x]);
} void kruscal () {
memset (root,-,sizeof(root));
cnt = ; //加入最小生成树的边
for (int k = ; k < tol; ++k) {
int u = edge[k].from;
int v = edge[k].to;
int t1 = find(u);
int t2 = find(v);
if(t1 != t2) {
ans[cnt++] = edge[k];
root[t1] = t2;
}
}
} int main () {
int T;
scanf("%d",&T);
int n;
while (T--) {
scanf ("%d",&n);
tol = ;
int w;
for (int i = ; i <= n; ++i) {
for (int j = ; j <= n; ++j) {
scanf("%d",&w);
if(j <= i || w == ) continue;
addedge(i, j, w);
}
}
sort (edge, edge + tol, cmp1);
kruscal ();
if (cnt != n - ) {
printf("-1\n");
continue;
} else {
sort (ans, ans + cnt, cmp2);
for (int i = ; i < cnt - ; ++i) {
printf("%d %d ", ans[i].from, ans[i].to);
}
printf("%d %d\n", ans[cnt - ].from, ans[cnt - ].to);
}
}
return ;
}

ZOJ 3204 Connect them MST-Kruscal的更多相关文章

  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 继续MST

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

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

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

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

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

  5. zoj 3204 Connect them

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

  6. zoj 3204 Connect them(最小生成树)

    题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...

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

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

  8. ZOJ 1586 QS Network MST prim水题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586 题目大意: QS是一种生物,要完成通信,需要设备,每个QS需要的设备的价格 ...

  9. MST最小生成树

    首先,贴上一个很好的讲解贴: http://www.wutianqi.com/?p=3012 HDOJ 1233 还是畅通工程 http://acm.hdu.edu.cn/showproblem.ph ...

随机推荐

  1. springmvc附件上传核心代码

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.Comm ...

  2. [LeetCode]题解(python):072-Edit Distance

    题目来源: https://leetcode.com/problems/edit-distance/ 题意分析: word1最少通过多少步可以变成word2.word1只能进行一下的操作.a)插入一个 ...

  3. java Serialization and Deserializaton

    This article from JavaTuturial Java provides a mechanism, called object serialization where an objec ...

  4. Aone新拉分支

    1.进入Aone新建项目 2.测试人员填huyangjun和husong 3.进入后拉分支 4.弄个日常普通环境 5.吧环境跑起,绑定Host就可以

  5. Sublime Text 2 - There are no packages available for installation

    解决Sublime Text 2 package Control 无法安装插件的问题 错误提示 here are no packages available for installation 问题解决 ...

  6. js动态加载控件jsp页面

    例子1:(具体参照drp中的flow_card_add.jsp)<script>    var rowIndex = 0;     function addOneLineOnClick() ...

  7. linux下笔记本有线网卡"未受管理"

    前段时间因为在弄一个笔记双网卡共享上网的事情把笔记本的有线网卡弄环了,连接的时候一直出现如下情况: 1)有线网卡:未受管理 2)无线网卡:每次登录的时候必须把原来登录过的信息删除掉,然后重新输入密码, ...

  8. c语言编写经验逐步积累4

    寥寥数笔,记录我的C语言盲点笔记,仅仅为以前经历过,亦有误,可交流. 1.逻辑表达式的使用 取值 = 表达式 ? 表达式1:表达式2: 比方x = y > z ? y:z 2."+,- ...

  9. C#中Cache用法

    C#中Cache用法     Cache 是分配在服务器上的一个公共的内存片,所谓公共指的cache只要一创建是任何一个客户端浏览器都可以通过后台代码访问到它,它面向的是所有用户,相对而言sessio ...

  10. Sass入门——基本特性-基础

    本文来自慕课网大漠 声明变量 三个部分:1.声明变量的符号"$"2.变量名称3.赋予变量的值 $brand-primary : darken(#428bca, 6.5%) !def ...