题目链接: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. 【HDU 1003】 Max Sum

    题 题意 需要在o(n)时间内,求最大连续的子序列的和,及其起点和终点. 分析 一种方法是一边读,一边维护最小的前缀和 s[i] ,然后不断更新 ans = max(ans,s[j] - s[i]), ...

  2. BZOJ-1036 树的统计Count 链剖线段树(模板)=(树链剖分+线段树)

    潇爷昨天刚刚讲完...感觉得还可以...对着模板打了个模板...还是不喜欢用指针.... 1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Lim ...

  3. Process manufacturing和Discrete manufacturing的区别

    Process manufacturing(Process industry) 加工制造,或者加工工业.其一个重要特征是,原材料被加工成成品后,我们再也无法将它恢复成原料,比如,苹果罐头,我们再没法把 ...

  4. java导出txt文本

    页面 项目结构 html代码 <html> </head> <body> <form action="down/downLoad" met ...

  5. 使用Android Studio搭建Android集成开发环境

    有很长一段时间没有更新博客了,最近实在是太忙了,没有时间去总结,现在终于可以有时间去总结一些Android上面的东西了,很久以前写过这篇关于使用Android Studio搭建Android集成开发环 ...

  6. jquery------.cycle的使用

    代码下载地址:http://malsup.github.io/jquery.cycle.all.js 把里面的代码复制到jquery.cycle.all.js里面 index.jsp <scri ...

  7. Java Web 设置默认首页

    一.问题描述 这里所谓的默认首页,是指在访问项目根目录时(如 http://localhost:8080/zhx-web/ )展示的页面,通过在web.xml里配置 <welcome-file- ...

  8. --hdu 2124 Repair the Wall(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124 Ac code : #include<stdio.h> #include<st ...

  9. js正则表达式中的问号几种用法小结

    这篇文章主要介绍了js正则表达式中的问号几种用法,比如+?,*?,{2,3}?可以停止匹配的贪婪模式,感兴趣的朋友可以参考下 在表示重复的字符后面加问号,比如+?,*?,{2,3}?可以停止匹配的贪婪 ...

  10. 设置button不同状态下的背景色,即把这个颜色变成图片设置成,背景图片

    - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { [self setBack ...