poj 2421 Constructing Roads 解题报告
题目链接: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 解题报告的更多相关文章
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- POJ - 2421 Constructing Roads (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- POJ 2421 Constructing Roads(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
- POJ - 2421 Constructing Roads(最小生成树&并查集
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- POJ 2421 Constructing Roads
题意:要在n个城市之间建造公路,使城市之间能互相联通,告诉每个城市之间建公路的费用,和已经建好的公路,求最小费用. 解法:最小生成树.先把已经建好的边加进去再跑kruskal或者prim什么的. 代码 ...
- [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads
给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...
随机推荐
- Android中获取图片的宽和高
在Android中,我们想获取图片的宽和高应该怎么办?一.正常加载图片的方法下获取宽和高 举一个简单的例子:创建一个图片的副本 //加载原图 Bitmap bmSrc = BitmapFactory. ...
- LinkedHashMap实现LRU算法
LinkedHashMap特别有意思,它不仅仅是在HashMap上增加Entry的双向链接,它更能借助此特性实现保证Iterator迭代按照插入顺序(以insert模式创建LinkedHashMap) ...
- MySQL安装最后一步apply security settings错误
网上查了很久都是说删除各种文件什么的,直接百度apply security settings,说是mysql没卸载干净.不是的. 看日志发现 You must SET PASSWORD before ...
- 【bzoj1005】 HNOI2008—明明的烦恼
http://www.lydsy.com/JudgeOnline/problem.php?id=1005 (题目链接) 题意 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多 ...
- hdu1695 莫比乌斯反演
莫比乌斯反演:可参考论文:<POI XIV Stage.1 <Queries>解题报告By Kwc-Oliver> 求莫比乌斯函数mu[i]:(kuangbin模板) http ...
- Xcode的一些有用的插件
** --Alcatraz:Xcode插件管理 ** 安装:curl -fsSL https://raw.github.com/supermarin/Alcatraz/master/Scripts/ ...
- centOS下yum安装配置samba
centOS下yum安装配置samba 2010-03-29 15:46:00 标签:samba yum centOS 安装 休闲 注意:本文的原则是只将文件共享应用于内网服务器,并让将要被共享的目 ...
- bootstrap-select搜索框输入中文
bootstrap-select 的搜索框无法输入中文,解决办法: 删除源码中这两行代码 that.$lis.not('.hidden, .divider, .dropdown-header').eq ...
- mlecms v2.2版权
inc\tools\smarty 下的Smarty.class.php文件. 找到 187行左右 我们会发现原来的 $dopud =$_template->libfile($dopud);已经 ...
- ajax局部更新
js //点击启用 $(".status").on("click",function(){ var id = $(this).attr("status ...