HDU1102 Constructing Roads 最小生成树
|
||||||||||
Constructing RoadsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem 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
30 990 692990 0 179692 179 011 2
Sample Output
179
Source
kicc
Recommend
Eddy | We have carefully selected several similar problems for you: 1233 1301 1162 1232 1875
Statistic | Submit | Discuss | Note |
||||||||||
|
分析:
要求的是使全部村庄连通的最小道路和,实际为最小生成树问题
1.kruskal算法
在给出的边的集合中,已经构成部分连通集,接下来将所有边放入一个最小堆中,
每次取出权重最小的边,看边的两个端点是否属于同一个集合,不属于就加入这条边,
否则就舍弃,这种贪心的选择就是kruskal算法
//kruskal算法
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=120;
struct Edge
{int v,u,weight;bool friend operator < (Edge a,Edge b){return a.weight>b.weight;}
};
//并查集模板
int pre[maxn];
int get_parent(int x)
{if(-1==pre[x]) return x;return pre[x]=get_parent(pre[x]);
}
bool mix (int x,int y)
{int fx=get_parent(x),fy=get_parent(y);if(fx==fy)return 1;pre[fx]=fy;return 0;
}priority_queue<Edge> E;
int kruskal()
{int cost=0;while(!E.empty()){Edge now=E.top();E.pop();if(!mix(now.v,now.u)){//不在同一个集合中cost+=now.weight;}}return cost;
}
int main()
{int n;while(cin>>n){while(!E.empty()) E.pop();memset(pre,-1,sizeof(pre));Edge e;for(int i=1;i<=n;i++){e.v=i;for(int j=1;j<=n;j++){e.u=j;cin>>e.weight;if(i<j) E.push(e);}}int q,a,b;cin>>q;while(q--){cin>>a>>b;mix(a,b);}int ans=kruskal();cout<<ans<<endl;}return 0;
}
2.Prime算法
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAX_SIZE 105
int parent[MAX_SIZE]; //记录父节点
int E[MAX_SIZE][MAX_SIZE]; //边集合
int Prim(int N);
bool visit[MAX_SIZE]; //判断该顶点是否在集合里面
int main(){int Q, N, i, j, v, w, ans;while (scanf("%d",&N)!=EOF){memset(visit, 0, sizeof(visit)); //初始化for (i = 1; i <= N; i++)for (j = 1; j <= N; j++)scanf("%d",&E[i][j]); //输入边scanf("%d", &Q);for (i = 0; i < Q; i++){scanf("%d%d",&v,&w);E[v][w] = E[w][v] = 0; //已建好将权值设为0}//-----------------------ans = Prim(N);printf("%d\n",ans);}return 0;
}
int Prim(int N){int i, j, k,pos,lmin,ans;parent[1] =-1; //设置根节点visit[1] = 1; //表示顶点 1已加入树中int LowCost[MAX_SIZE];for (i = 1; i <= N; i++)LowCost[i] = INT_MAX; //初始化ans = 0;j = 1;for (i = 2; i <= N; i++){/*j表示上一次加入集合的顶点*/for (k = 1; k <= N; k++){if (!visit[k] && LowCost[k] > E[k][j]){ //更新j的邻接点parent[k] = j; //将k父节点暂定为 jLowCost[k] = E[k][j];}}lmin = INT_MAX, pos = 0;for (k = 1; k <= N; k++){ //找出不在树中且离树距离最小的点if (!visit[k]&&lmin > LowCost[k]){pos = k;lmin = LowCost[k];}}visit[pos] = 1; //加入树中ans += E[pos][parent[pos]]; //加入边j = pos; //更新j}return ans;
}
HDU1102 Constructing Roads 最小生成树相关推荐
- POJ2421 Constructing Roads 最小生成树
修路 时限: 2000MS 内存限制: 65536K 提交总数: 31810 接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...
- HDOJ1102 Constructing Roads【最小生成树】-----武科大ACM暑期集训队选拔赛1题
这道题目没有做出来,代码写好之后一直没有AC,本以为做了这么多最小生成树的题目,这道题一定没问题的,结果很遗憾,没有注意细节问题: 首先,如何处理已经建好的路?已经建好的路说明这两个点是连通的,只要把 ...
- 【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)
题干: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- hdu 1025 Constructing Roads In JGShining's Kingdom(DP + 二分)
此博客为转发 Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- 22.12.14补卡 Constructing Roads POJ - 2421
Constructing Roads - POJ 2421 - Virtual Judge 只有输入有点小坑 只有上三角的数据是有用的, 下三角的数据与上三角重复, 可不处理, 把i>=j的全部 ...
- 【HDU - 1102】Constructing Roads (最小生成树裸题模板)
题干: There are N villages, which are numbered from 1 to N, and you should build some roads such that ...
- Constructing Roads POJ - 2421 (最小生成树)
思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将 所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...
- hdu1025 Constructing Roads In JGShining#39;s Kingdom(二分+dp)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
最新文章
- boost::pfr::detail::fields_count相关的测试程序
- boost::hana::map_用法的测试程序
- PulseAudio 设计和实现浅析
- jmeter压力测试_用Jmeter实现对接口的压力测试
- 正确设置asp.net网站的404错误页面
- 运筹优化(十五)--应用模型之分配问题
- 【luogu P2939 [USACO09FEB]改造路Revamping Trails】 题解
- read()/write()的生命旅程——前言与目录
- php 检查货币类型_php 判断函数是否为费用类型(金额/货币:6.02)有小数点
- 单目标跟踪算法:SiamRPN++
- ubuntu桌面美化mac_教程:为你的linux桌面带来Mac OS Mojave的体验
- python检索论文_一种基于Python的音乐检索方法的研究
- 【html】edm 邮件制作指南
- 2022年双11淘宝满减规则解读
- CAD看图如何在电脑上快速找到并打开指定CAD图纸
- 58同城|TEG技术工程平台群-闪电面试专场内推(12月7日)
- 【12月英语——快乐中学习】
- 如何解决MacOS Big Sur,打印错误:ERROR invalidcontent?
- 常用MySQL字段类型解析
- 常见的HTT相应状态码