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 =============================以下是最小生成树+并 ...
随机推荐
- CSS多列、用户界面属性
CSS多列 常用属性: column-count 分几列 column-gap 列间距 column-rule 列分割线的样式(写法和border一样) 例如: 一个div分三列,列之间间距为10px ...
- Matlab最新的官方文档中文翻译
文章翻译的是Matlab最新的官方文档R2016b,可能后续如果我还有时间会继续翻译,希望能够帮到大家,翻译的不好请大家不要吐槽. Matlab官方文档地址:http://cn.mathworks.c ...
- JS实现的在线推荐逻辑
import _ from 'lodash';import cfg from '../cfg/cfg';import {Response} from '../shared/lib/response'; ...
- [Q]手动加载菜单方法
一般情况下,安装程序会自动安装依云软件菜单,但可能由于某些原因未能自动安装的话,您可以手动加载菜单,步骤如下: 在AoutCAD命令行输入"CUILOAD",会弹出"加载 ...
- CentOS 6.5 安装MySQL5.7 RPM
一.新特性 MySQL 5.7可谓是一个令人激动的里程碑,在默认了InnoDB引擎的基础上,新增了ssl.json.虚拟列等新特性.相对于postgreSQL和MariaDB而言,MySQL5.7做了 ...
- CSS Hank兼容浏览器的
color:red; /* 所有浏览器都支持 */ color:red !important; /* 除IE6外 */ _color:red; /* IE6支持 */ *color:red; /* I ...
- ModelState.IsValid一直为false的原因
一,问题:ModelState.IsValid一直为false 二,解决方法和原因, 由于这个方法中传过来的RegisterForm模型的字段,某一个为空值,则会造成这个验证验证为false,去注释掉 ...
- IIS 支持 ajax 跨域
第一,首先要浏览器支持. Firefox和Chrome都支持了. IE10 也支持. IE8,9 需要使用其他方式才能支持 第二, 加上HTTP header. IIS 7 的方式比较容易. 就在we ...
- 关于CGI、FastCGI和PHP-FPM的关系
1.CGI是干嘛的? CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...
- 利用python 与 wmi 获取WINDOWS基本信息
#!/usr/bin/env python3.5 # -*- coding:utf8 -*- import platform import subprocess import wmi def serv ...