POJ 2421 Constructing Roads (最小生成树)
Constructing Roads
题目链接:
http://acm.hust.edu.cn/vjudge/contest/124434#problem/D
Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
##题意:
求最小花费使得所有点联通.
##题解:
裸的最小生成树.
读入邻接矩阵后再建图.
对于已经联通的边,直接把它们的距离赋成0. (即能用就优先用).
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 110
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
struct node{
int left,right,cost;
}road[maxn*maxn];
int cmp(node x,node y) {return x.cost<y.cost;}
int p[maxn],m,n;
int find(int x) {return p[x]=(p[x]==x? x:find(p[x]));}
int kruskal()
{
int ans=0;
for(int i=1;i<=n;i++) p[i]=i;
sort(road+1,road+m+1,cmp);
for(int i=1;i<=m;i++)
{
int x=find(road[i].left);
int y=find(road[i].right);
if(x!=y)
{
ans+=road[i].cost;
p[x]=y;
}
}
return ans;
}
int dis[maxn][maxn];
int main(int argc, char const *argv[])
{
//IN;
while(scanf("%d", &n) != EOF)
{
m = 0;
memset(road,0,sizeof(road));
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
scanf("%d", &dis[i][j]);
}
}
int q; cin >> q;
while(q--) {
int x,y; scanf("%d %d", &x,&y);
dis[x][y] = dis[y][x] = 0;
}
for(int i=1; i<=n; i++) {
for(int j=i+1; j<=n; j++) {
road[++m].left = i;
road[m].right = j;
road[m].cost = dis[i][j];
}
}
int ans=kruskal();
printf("%d\n", ans);
}
return 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 (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- 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(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
- [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads
给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...
- Poj 2421 Constructing Roads(Prim 最小生成树)
题意:有几个村庄,要修最短的路,使得这几个村庄连通.但是现在已经有了几条路,求在已有路径上还要修至少多长的路. 分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被P ...
- 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 解题报告
题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...
随机推荐
- iosblock用法
看了很多的block用法,还是小糊涂. 最后还是自己尝试吧. #import "FirstViewController.h" @interface FirstViewControl ...
- java中四种操作(dom、sax、jdom、dom4j)xml方式详解与比较
1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找特 ...
- Null-conditional Operators
https://msdn.microsoft.com/en-us/library/dn986595.aspx x?.y – null conditional member access. Return ...
- CentOS 6.5系统使用yum方式安装LAMP环境和phpMyAdmin详细过程
介绍如何在CentOs6.2下面使用YUM配置安装LAMP环境,一些兄弟也很喜欢使用编译的安装方法,个人觉得如果不是对服务器做定制,用yum安装稳定简单,何必去download&make&am ...
- bzoj2245: [SDOI2011]工作安排
费用流. 这道题的模型比较明显,拆点也是很容易看出来的. #include<cstdio> #include<algorithm> #include<cstring> ...
- 【笨嘴拙舌WINDOWS】API
如今,相对于大行其道的对象,服务,API概念的提出要早很多,却依然经久不衰:所谓万变不离其宗,如今很多服务(Web Services,云服务)的提供方式和API如出一辙. Windows API(Ap ...
- windows2003 IIS6网络负载平衡设置
问题 随着计算机技术的不断发展,单台计算机的性能和可靠性越来越高.但现实中还是有许多应用是单台计算机难以达到,例如: 1.银行存储用户数据的数据库服务器必须保证24小时不间断的运转,并在发生严重硬件故 ...
- C# winform 窗体从右下角向上弹出窗口效果
参考自 http://blog.csdn.net/yilan8002/article/details/7197981 /// <summary> /// 窗体动画函数 注意:要引用Syst ...
- 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)
本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明Himi) 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-c ...
- HelloX操作系统网络功能简介及使用和开发指南
HelloX网络功能简介及使用和开发指南 HelloX网络功能简介 作为物联网操作系统,网络功能是必备的核心功能之一.按照规划,HelloX实现了两个不同类型的TCP/IP协议栈,一个面向资源受限的嵌 ...