这道题目麻烦在输出的时候需要按照字典序输出,不过写了 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. Nginx 的 Echo 模块 —— echo-nginx-module(转)

    Nginx 有个 echo 模块可以用来输出一些简单的信息,例如: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...

  2. 基于Visual C++2013拆解世界五百强面试题--题10-找出N个数种最大的K个数

    有一亿个整数,请找出最大的 1000 个,要求时间越短越好, 空间占用越好越好. 如果不考虑时间效率,很容易想到解决方法,我们只需存储前一千个数, 然后依次读入后面的数和这一千个数组比较,替换其中比较 ...

  3. MFC自绘控件学习总结

    前言:从这学期开始就一直在学习自绘控件(mfc),目标是做出一款播放器界面,主要是为了打好基础,因为我基础实在是很烂....说说我自己心得体会以及自绘控件的方法吧,算是吐槽吧,说的不对和不全的地方,或 ...

  4. C++模板:欧拉函数

    单个欧拉函数 int eular(int n){ int ret=1,i; for(i=2;i*i<=n;i++) if(n%i==0){ n/=i,ret*=i-1; while(n%i==0 ...

  5. mySQL中replace的用法

    MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函数方面能有所启迪   mysql replace实例说明: ...

  6. Android的电源管理框架

    Android的电源管理框架 Android通过锁和定时器来切换系统的状态,使系统的功耗降至最低,整个系统的电源管理框架分成五个部分:应用层,framework层,JNI层,HAL层和内核层.电源管理 ...

  7. HDU1029时钟(排序)

    题意:是用两个指针的一个模拟时钟的时针和分针.两个指针形成一个角度.角度测量两个指针之间的最小角度.两只手之间的角度是大于或等于0,且小于或等于180度的度量.由于一个序列的五个不同的写入时间,格式为 ...

  8. D - D 田忌赛马

    D - D    田忌赛马   解题报告 hdu 1052 Tian Ji -- The Horse Racing 链接:http://acm.hust.edu.cn/vjudge/contest/v ...

  9. man命令重定向后有^H乱码问题

    在  man ld.so>ld.so后 vim打开ld.so后出现重叠乱码问题 但是cat.less可以正常查看 解决办法:  man ld.so|col -b >ld.so col命令是 ...

  10. 在一个数组中是否存在两个数A、B的和为M

    #include <iostream>#include <algorithm>//#include <vector>using namespace std; int ...