ZOJ 3204 Connect them MST-Kruscal
这道题目麻烦在输出的时候需要按照字典序输出,不过写了 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的更多相关文章
- 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 ...
- ZOJ 3204 Connect them 继续MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目大意: 让你求最小生成树,并且按照字典序输出哪些点连接.无解输出-1 ...
- ZOJ 3204 Connect them(字典序输出)
主要就是将最小生成树的边按字典序输出. 读取数据时,把较小的端点赋给u,较大的端点号赋值给v. 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小 ...
- ZOJ 3204 Connect them(最小生成树+最小字典序)
Connect them Time Limit: 1 Second Memory Limit: 32768 KB You have n computers numbered from 1 t ...
- zoj 3204 Connect them
最小生成树,我用的是并查集+贪心的写法. #include<stdio.h> #include<string.h> #include<math.h> #includ ...
- zoj 3204 Connect them(最小生成树)
题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...
- zoj 3204 最小生成树,输出字典序最小的解
注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...
- ZOJ 1586 QS Network MST prim水题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586 题目大意: QS是一种生物,要完成通信,需要设备,每个QS需要的设备的价格 ...
- MST最小生成树
首先,贴上一个很好的讲解贴: http://www.wutianqi.com/?p=3012 HDOJ 1233 还是畅通工程 http://acm.hdu.edu.cn/showproblem.ph ...
随机推荐
- PHP的排列组合问题 分别从每一个集合中取出一个元素进行组合,问有多少种组合?
首先说明这是一个数学的排列组合问题C(m,n) = m!/(n!*(m-n)!) 比如:有集合('粉色','红色','蓝色','黑色'),('38码','39码','40码'),('大号','中号') ...
- The process "E:\Qt\4.8.5\bin\qmake.exe" exited with code 2.(不能包含中文路径,qmake够弱智的)
打开某个项目的时候,编译出现类似的错误 21:46:44: The process "E:\Qt\4.8.5\bin\qmake.exe" exited with code 2. ...
- PC游戏编程(入门篇)(前言写的很不错)
PC游戏编程(入门篇) 第一章 基石 1. 1 BOSS登场--GAF简介 第二章 2D图形程式初体验 2.l 饮水思源--第一个"游戏"程式 2.2 知其所以然一一2D图形学基础 ...
- BZOJ 1008 越狱 (组合数学)
题解:正难则反,从总数中减去全部相邻不相同的数目就是答案,n*(n-1)^(m-1):第一个房间有n中染色方案,剩下m-1个房间均只有n-1种染色方案,用总数减就是答案. #include <c ...
- Flex 按钮添加图标
第一种是在Flex应用中创建一个变量,利用[Bindable]和[Embed] ,在代码中以参数形式传入制定图标(icon)的路径,然后利用类似icon="{Icon}"的代码嵌入 ...
- A计划(bfs)
A计划 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissio ...
- CSS的display、hover、overflow、©(版权符号)、borer-radius(边框圆角)
一.display: none 隐藏 block显示 visibility:hidden隐藏 visible显示 display:none和visibility:hidden这两个属性对应的值 ...
- Django forms 关于select和checkbox设置初始选中值及让前端选中指定值
Django的forms和models一样很牛逼.他有两种功能,一是生成form表单,还有就是form表单的验证. 这里主要说一下生成form表单时经常用到的需要设置 初始值 / 默认值 的情况. 1 ...
- awk参数详解
wk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢出或是处理缓慢的问题,通常用来格式化文本信息 awk处理过程: 依次对每一行进行处理,然后输出 awk命令形式: awk [-F| ...
- 字符串操作函数<string.h>相关函数strcpy,strcat,等源码。
首先说一下源码到底在哪里找. 我们在文件中包含<cstring>时,如果点击右键打开文档, 会打开cstring,我们会发现路径为: D:\Program Files\visual stu ...