zoj 3204 Connect them
最小生成树,我用的是并查集+贪心的写法。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn = ;
int c[maxn][maxn];
int father[maxn];
int flag[maxn];
struct abc{
int fei, a, b;
}dt[maxn*maxn];
struct ans{
int aa, bb;
}anss[maxn*maxn];
bool cmp(const abc&a, const abc&b)
{
if (a.fei == b.fei&&a.a == b.a) return a.b < b.b;
if (a.fei == b.fei) return a.a < b.a;
return a.fei < b.fei;
}
bool cmp2(const ans&a, const ans&b)
{
if (a.aa == b.aa) return a.bb < b.bb;
return a.aa < b.aa;
}
int find(int x)
{
if (x != father[x]) father[x] = find(father[x]);
return father[x];
}
int main()
{
int sb;
scanf("%d", &sb);
while (sb--)
{
int n, i, j;
scanf("%d", &n);
memset(c, , sizeof(c));
memset(father, , sizeof(father));
memset(flag, , sizeof(flag));
int q = ;
for (i = ; i <= ; i++) father[i] = i;
for (i = ; i <= n; i++) for (j = ; j <= n; j++) scanf("%d", &c[i][j]);
for (i = ; i <= n; i++)
{
for (j = i + ; j <= n; j++)
{
if (c[i][j] != )
{
dt[q].a = i;
dt[q].b = j;
dt[q].fei = c[i][j];
q++;
}
}
}
sort(dt, dt + q, cmp);
int qq = ;
for (i = ; i < q; i++)
{
int x = find(dt[i].a);
int y = find(dt[i].b);
if (x != y)
{
father[x] = y;
anss[qq].aa = dt[i].a;
anss[qq].bb = dt[i].b;
qq++;
}
}
for (i = ; i <= n; i++){int u = find(i);flag[u]++;}
int dd = ;
for (i = ; i <= n; i++){if (flag[i] > && flag[i] == n){dd = ;break;}}
if (dd==)
{
sort(anss, anss + qq, cmp2);
for (i = ; i < qq; i++)
{
if (i < qq-)printf("%d %d ", anss[i].aa, anss[i].bb);
else printf("%d %d\n", anss[i].aa, anss[i].bb);
}
}
else if (dd == ) printf("-1\n");
}
return ;
}
zoj 3204 Connect them的更多相关文章
- 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(最小生成树+最小字典序)
Connect them Time Limit: 1 Second Memory Limit: 32768 KB You have n computers numbered from 1 t ...
- ZOJ 3204 Connect them(字典序输出)
主要就是将最小生成树的边按字典序输出. 读取数据时,把较小的端点赋给u,较大的端点号赋值给v. 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小 ...
- ZOJ 3204 Connect them MST-Kruscal
这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的 因为是裸的 Kruscal ,所以就直接上代码了- Source Code : //#pragma comme ...
- zoj 3204 Connect them(最小生成树)
题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...
- ZOJ 3204 Connect them 继续MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目大意: 让你求最小生成树,并且按照字典序输出哪些点连接.无解输出-1 ...
- zoj 3204 最小生成树,输出字典序最小的解
注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...
- MST最小生成树
首先,贴上一个很好的讲解贴: http://www.wutianqi.com/?p=3012 HDOJ 1233 还是畅通工程 http://acm.hdu.edu.cn/showproblem.ph ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
随机推荐
- JS算法之快排&冒泡
1.快速排序思想: 1.1 先找数组的最中间的一个数为基准 1.2 把数组通过此基准分为小于基准的left数组和大于基准的right数组, 1.3 递归重复上面的两个步骤, 代码如下: functio ...
- C++ STD inner_product函数
C++ STD函数 inner_product是c++标准库封装的一个函数. 函数原型: 函数1: inner_product(beg1, end1, beg2, init) 函数2: inner ...
- CentOS 7 引导 -- GRUB2
如果安装完 CentOS 7 之后,原来的 Windows 引导菜单被覆盖了,vi /boot/grub2/grub.cfg 添加如下代码: ## BEGIN WINDOWS 10 menuentry ...
- Linux上传与下载(sz-rz)
linux使用rz和sz命令上传和下载文件! sz命令发送文件到本地: # sz filename rz命令本地上传文件到服务器: # rz 执行该命令后,在弹出框中选择要上传的文件即可. 说明:打开 ...
- git stash让bug来的更猛烈些吧
git stash可以用来暂存当前正在进行的工作,比如想pull最新的代码,又不想加新commit, 或者有一个紧急的bug需要修复,但是这个bug又与你已经在做的工作(还没完成)有关联.这个时候有的 ...
- 关于react-native遇到Can't find variable: TouchableHighlight
当你遇到这样的问题时,是由于你没有import中添加TouchableHighlight import { AppRegistry, StyleSheet, Text, View, PixelRati ...
- UVa 1395 Slim Span
问题:给出一个n结点的图,求最大边与最小边差值最小的生成树 my code: #include <iostream> #include <cstdio> #include &l ...
- redis(1)
一.Redis介绍 Redis 是一款开源的,基于 BSD 许可的,高级键值 (key-value) 缓存 (cache) 和存储 (store) 系统.由于 Redis 的键包括 string,ha ...
- java 异常的捕获及处理
在没有异常处理的程序中如果要回避异常,需要使用大量的判断语句,配合所想到的错误状况来捕捉程序中可能发生的错误.但是这样势必会导致程序运行效率降低.java异常处理机制具有易于使用,可自定义异常类,处理 ...
- scip学习
最近在学习scip 准备把里面比较有价值的题分别用lisp和c语言写出来.这样或许能更加深入的理scheme的函数式编程. tip:我用的c语言环境是vs2015,lisp环境为Drracket;