这道题目麻烦在输出的时候需要按照字典序输出,不过写了 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. The introduction to Web.config of ASP.NET #Reprinted#

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  2. jQuery File Upload

    jQuery File Upload介绍.............................................. 2 实现基本原理......................... ...

  3. perl5 第十一章 文件系统

    第十一章  文件系统 by flamephoenix 一.文件输入/输出函数  1.基本I/O函数    1)open函数    2)用open重定向输入    3)文件重定向    4)指定读写权限 ...

  4. 基于Visual C++2013拆解世界五百强面试题--题18-程序结果分析2-终结篇

    第二部分程序结果分析,分析流程还是写入代码注释中 分析下面程序的输出: #include <stdio.h> int main() { char *a = "hello" ...

  5. Android Animations动画使用详解

    一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XML配置文件中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画 ...

  6. js中赋值表达式的值为右边

    var name="the window";var o={ name:"the object", getName:function(){ console.log ...

  7. BZOJ 1856: [Scoi2010]字符串( 组合数 )

    求(0,0)->(n,m)且在直线y=x下方(可以在y=x上)的方案数...同 http://www.cnblogs.com/JSZX11556/p/4908648.html --------- ...

  8. 纯css画哆啦A梦

    今天有点无聊,照着网上的图写了个哆啦A梦,无技术可言,纯考耐心. <!doctype html> <html lang="en"> <head> ...

  9. LNMP一键安装结果

    ============================== Check install ============================== Checking ... Nginx: OK M ...

  10. .net MVC 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值

    在.net mvc的controller中,方法返回JsonResult,一般我们这么写: [HttpPost] public JsonResult QueryFeature(string url, ...