题目链接:http://poj.org/problem?id=2421

实际上又是考最小生成树的内容,也是用到kruskal算法。但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出最小生成树来。

一般来说,过到这四组数据大体上就能AC了。  1、题目给出的案例数据    2、连接的道路可能把所有的村庄都已经连通了       3、两个村庄给出多次,即连接这两个村庄的道路是重复的!。

2、3这两种情况的数据如下(为了好看,自己出了一组,当然Sample Input 那组也行):

情况2(所有村庄已连通):                   情况3:

还有第4种情况:

 #include <iostream>
#include <algorithm>
using namespace std; const int maxe = + ; // 这里开小点也行,因为只存储矩阵不够一半的数据
int rep[maxe], vis[maxe]; struct sets
{
int v1, v2;
int value;
} exa[maxe]; int cmp(sets a, sets b)
{
return a.value < b.value;
} int find(int x)
{
return x == rep[x] ? x : find(rep[x]);
} int main()
{
int i, j, n, a, b, x, y, t, cnt, flag;
while (scanf("%d", &n) != EOF)
{
for (i = ; i <= n; i++)
{
rep[i] = i;
}
cnt = ;
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
scanf("%d", &a);
if (i < j) // 只存储上三角矩阵
{
exa[cnt].v1 = i;
exa[cnt].v2 = j;
exa[cnt].value = a;
cnt++;
}
}
}
sort(exa, exa+cnt, cmp); // 对长度进行从小到大排序
flag = ;
memset(vis, , sizeof(vis));
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &a, &b);
if (a > b)
swap(a, b); // 刚开始就保持小的元素的祖先是大的元素
x = find(a);
y = find(b);
if (x > y)
{
swap(x, y); // 合并集合的时候,有可能使得大的元素的祖先变成了是比它小的元素
}
if (vis[a] && vis[b]) // 两个村庄在之前已经被访问过,这是为了处理情况4的情况的
rep[x] = y;
else
{
if (x == y) // 所有村庄都有路可通
flag = ;
if (!vis[a] && !vis[b])
{
rep[x] = y;
vis[a] = vis[b] = ;
}
else if (!vis[a])
{
rep[x] = y;
vis[a] = ;
}
else if (!vis[b])
{
rep[x] = y;
vis[b] = ;
}
}
}
if (!flag)
{
int minval = ;
for (i = ; i < cnt; i++)
{
x = find(exa[i].v1);
y = find(exa[i].v2);
if (x != y)
{
minval += exa[i].value;
if (x < y) // 为了与前面相一致,也是小的元素的祖先是大的元素
rep[x] = y;
else
rep[y] = x;
}
}
printf("%d\n", minval);
}
else
printf("0\n");
}
return ;
}

其实,还有一种比较简单的方法,但是目前还不会实现。Dwylkz给的解法:题目给的已连通的村庄的value都设为0。感觉这样会少讨论很多情况,希望以这种方法过了的读者可以指点一下,好像在上面的代码修改比较难实现。

poj 2421 Constructing Roads 解题报告的更多相关文章

  1. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  2. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  3. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

  4. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  5. POJ - 2421 Constructing Roads (最小生成树)

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  6. POJ 2421 Constructing Roads(最小生成树)

    Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...

  7. POJ - 2421 Constructing Roads(最小生成树&并查集

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  8. POJ 2421 Constructing Roads

    题意:要在n个城市之间建造公路,使城市之间能互相联通,告诉每个城市之间建公路的费用,和已经建好的公路,求最小费用. 解法:最小生成树.先把已经建好的边加进去再跑kruskal或者prim什么的. 代码 ...

  9. [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads

    给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...

随机推荐

  1. Oracle中的伪列

    分页查询中,需要用到伪列rownum,代码如下: select * from (select rownum rn, name from cost where rownum <= 6) where ...

  2. UVA 1398 Meteor

    传送门 Solution: 记一颗流星在视野内的时间段为(L, R), 为了使所有(L, R)都取整数,首先将坐标放大. 放大倍数可取为 LCM(1, 2, ..., 10)= 2520 接着计算:从 ...

  3. php扩展redis

    Redis安装整理(window平台) +php扩展redis 分类: Web开发2013-03-23 18:51 8258人阅读 评论(3) 收藏 举报                        ...

  4. JS Resizable Panel 练习

    Resizable Panel HTML <!doctype html> <html> <head> <title>Resizable Panel< ...

  5. MSF溢出实战教程

    1.  进入终端,开启MSF相关服务 2.  连接数据库 3.  主机扫描 发现如果有MS08_067漏洞,就可以继续渗透 4.  开始溢出 溢出成功的话 sessions -l         查看 ...

  6. linux安全加固(2)

    目录:1.BIOS2.SSH安全3.禁用telnet4.禁用代码编译5.ProFTP6.TCPwrappers7.创建一个SU组8.root通知9.history安全10.欢迎信息11.禁用所有特殊账 ...

  7. property中的strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解

    iOS5中加入了新知识,就是ARC,其实我并不是很喜欢它,因为习惯了自己管理内存.但是学习还是很有必要的. 在iOS开发过程中,属性的定义往往与retain, assign, copy有关,我想大家都 ...

  8. 下面将详细说明useradd与usermod 的参数及用法!

    下面将详细说明useradd与usermod 的参数及用法! 说到这里要另外两句,关于linux下口令相关的文件存放位置说明/usr/bin/passwd 包含 passwd 命令. /etc/pas ...

  9. 转<<C#集合Dictionary中按值的降序排列

    转载地址:http://blog.sina.com.cn/s/blog_5c5bc9070100pped.html C#集合Dictionary中按值的降序排列 static void Main(st ...

  10. Spark Streaming和Flume-NG对接实验

    Spark Streaming是一个新的实时计算的利器,而且还在快速的发展.它将输入流切分成一个个的DStream转换为RDD,从而可以使用Spark来处理.它直接支持多种数据源:Kafka, Flu ...