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 ...
随机推荐
- Oracle查看锁表
查看锁表进程SQL语句1: select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, a ...
- document.body.scrollTop 各浏览器兼容性解决
document.compatMode:获取当前浏览器采用的渲染方式.主要是浏览器的模式,有两个:BackCompat,CSS1Compat.其中前者是怪异模式,后者是标准模式. IE默认是BackC ...
- Linux Systemcall By INT 0x80、Llinux Kernel Debug Based On Sourcecode
目录 . 系统调用简介 . 系统调用跟踪调试 . 系统调用内核源码分析 1. 系统调用简介 关于系统调用的基本原理,请参阅另一篇文章,本文的主要目标是从内核源代码的角度来学习一下系统调用在底层的内核中 ...
- POJ2309BST(树状数组)
BST Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9182 Accepted: 5613 Description C ...
- pthread_detach pthread_join pthread_create
pthread_create:创建线程以后线程直接开始运行: pthread_detach pthread_join:线程资源的释放方式. 创建一个线程默认的状态是joinable, 如果一个线程结束 ...
- POI读写Excel简述之读取
一.POI读取Excel文件(以Excel2003版为例,2007版就是根据文件扩展名xlsx将HSSFWorkbook换为XSSFWorkbook,及其Sheet.Row.Cell也相应替换) // ...
- Spring依赖注入:注解注入总结
更多11 spring 依赖注入 注解 java 注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.S ...
- C#里List.Sort的用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...
- linux 的iptables防火墙
.a文件就是*.o文件的集合, 是object文件的归档文件, 所以, 用nm -A ???.a看到的 symbolic符合名称都是 相应的, 包含的 .o文件.... linux 2.4内核中 ...
- linux下可以禁用的一些服务
linux下多软件/多脚本之间的配合: 包括做好 “实体”和“配置”两个方面的事情 “实体”是指实实在在的脚本文件,服务脚本: “配置”是指其他与之交互的.协同工作的软件.脚本,要进行适当的配置,告知 ...