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 =============================以下是最小生成树+并 ...
随机推荐
- Day1-python理论基础
一.python介绍 Python 的创始人为Guido van Rossum.Guido为了打发圣诞节的无趣,于1989年发明,在荷兰国家数学和计算机科学研究所设计出来的(作为ABC 语言的一种继 ...
- 软件各种版本的含义!例如RC,M,GA等等
RC版本 RC:(Release Candidate) Candidate是候选人的意思,用在软件上就是候选版本.Release是发行.发布的意思.Release.Candidate.就是发行 ...
- 2016 ACM/ICPC Asia Regional Qingdao Online 1002 Cure
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- IOS GCD图片数据异步下载,下载完成后合成显示
关于GCD使用详解,请看我的上一篇blog:http://www.cnblogs.com/xin-lang/p/6278606.html 前段时间遇到个需要异步下载,下载完成后再组合显示的东西.这里我 ...
- HTML <button>标签
如果<button>标签在<form>中不加type="button",那么默认含义是"submit". <button>标 ...
- DotNetZip 压缩下载
var fs = Response.OutputStream; using (ZipFile zip = new ZipFile(System.Text.Encoding.UTF8)) //编码是解决 ...
- 单身福利来了:VR恋人为你量身定制一个女朋友
相对于传统视频体验,VR视频给人带来了更加真实的体验.特别是对于单身狗来说,能在VR中拥有一个虚拟的恋人可谓是莫大的心灵安慰.近日,上海微雁文化传媒有限公司正式发布了首款养成类手机应用VR恋人. VR ...
- html+css基础篇
2016年11月19号,计划把基础在看一下,听大神说好的东西就要多看几遍,知识是学来用的解决问题的,加油 接下来的是我在自学中的小笔记吧,每天都在保持几个小时的学习思考状态,由于要升本所以学前端的时间 ...
- Minicom配置及使用详解
因为现在电脑基本不配备串行接口,所以,usb转串口成为硬件调试时的必然选择.目前知道的,PL2303的驱动是有的,在dev下的名称是ttyUSB*. minicom,tkterm都是linux下应用比 ...
- java基础第二天
学习了关键字,标识符,数据类型.变量和常量,运算符和表达式,开始编写一些简单的输入输出运算的程序了.