最小生成树,我用的是并查集+贪心的写法。

#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的更多相关文章

  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(最小生成树+最小字典序)

    Connect them Time Limit: 1 Second      Memory Limit: 32768 KB You have n computers numbered from 1 t ...

  3. ZOJ 3204 Connect them(字典序输出)

    主要就是将最小生成树的边按字典序输出. 读取数据时,把较小的端点赋给u,较大的端点号赋值给v. 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小 ...

  4. ZOJ 3204 Connect them MST-Kruscal

    这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的 因为是裸的 Kruscal ,所以就直接上代码了- Source Code : //#pragma comme ...

  5. zoj 3204 Connect them(最小生成树)

    题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...

  6. ZOJ 3204 Connect them 继续MST

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目大意: 让你求最小生成树,并且按照字典序输出哪些点连接.无解输出-1 ...

  7. zoj 3204 最小生成树,输出字典序最小的解

    注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...

  8. MST最小生成树

    首先,贴上一个很好的讲解贴: http://www.wutianqi.com/?p=3012 HDOJ 1233 还是畅通工程 http://acm.hdu.edu.cn/showproblem.ph ...

  9. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

随机推荐

  1. Json 使用小结

    关于Json: content={ news_item=[ { title=a, digest=tan for test, content=just for test, content_source_ ...

  2. 酒店管理web项目总结

    酒店管理web项目总结 半个月的努力,一个完整的酒店项目也就新鲜出炉了,在项目的制作中总结了一些酒店管理项目的特点. 1.需求分析,酒店管理需要什么? 1)首先系统的安全性,对于任何一个系统来说,安全 ...

  3. 函数四种调用模式以及其中的this指向

    第一种:函数直接执行模式 function add(a,b){ console.log(this); return a+b; } add(10,20)//this===window 第二种:对象方法的 ...

  4. YUI Compressor压缩失效的场景-eval和with

    一.官方文档的说明 in the face of evil features such as eval or with, the YUI Compressor takes a defensive ap ...

  5. Dockerfile文件格式的简单介绍

    # This dockerfile uses the ubuntu image # VERSION 2 - EDITION 1 # Author: docker_user # Command form ...

  6. url截取

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. postgresql----serial类型和序列

    postgresql序列号(SERIAL)类型包括smallserial(smallint,short),serial(int)和bigserial(bigint,long long int),不管是 ...

  8. IOS GCD使用实例大全

    GCD是大家在IOS开发过程中经常使用的一种多线程管理机制.原理这里就不多说了,大家关心的大部分都是它的使用,下面主要介绍GCD的主要方法及其实例. 1.认识主队列,感受串行队列的运行,运行结果打印的 ...

  9. CreateMutex()参数报错问题

    举例: hMutex = CreateMutex(NULL, FALSE,"screen"); 报错 error C2664:"CreateMutexW": 不 ...

  10. sqlcipher for android

    github 地址 https://github.com/sqlcipher/android-database-sqlcipher 官网教程 https://www.zetetic.net/sqlci ...