点击打开链接
Online Judge Online Exercise Online Teaching Online Contests Exercise Author
F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests
    DIY | Web-DIY beta
Recent Contests
liuhulin
Mail 0(0)
Control Panel
Sign Out

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26460    Accepted Submission(s): 10130

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

Home | Top Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2018 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.004000(s) query 5, Server time : 2018-05-06 22:29:14, Gzip enabled
Administration

分析:

题目中已经给出一些道路,这些道路将村庄将村庄构成了部分连通集
要求的是使全部村庄连通的最小道路和,实际为最小生成树问题

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算法

       由于题目中给定了一些边,处理的办法是将这些边权值设置为0.定义一个visit[i]数组记录顶点i是否在树中。
#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 最小生成树相关推荐

  1. POJ2421 Constructing Roads 最小生成树

    修路 时限: 2000MS   内存限制: 65536K 提交总数: 31810   接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...

  2. HDOJ1102 Constructing Roads【最小生成树】-----武科大ACM暑期集训队选拔赛1题

    这道题目没有做出来,代码写好之后一直没有AC,本以为做了这么多最小生成树的题目,这道题一定没问题的,结果很遗憾,没有注意细节问题: 首先,如何处理已经建好的路?已经建好的路说明这两个点是连通的,只要把 ...

  3. 【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)

    题干: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  4. hdu 1025 Constructing Roads In JGShining's Kingdom(DP + 二分)

    此博客为转发 Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  5. 22.12.14补卡 Constructing Roads POJ - 2421

    Constructing Roads - POJ 2421 - Virtual Judge 只有输入有点小坑 只有上三角的数据是有用的, 下三角的数据与上三角重复, 可不处理, 把i>=j的全部 ...

  6. 【HDU - 1102】Constructing Roads (最小生成树裸题模板)

    题干: There are N villages, which are numbered from 1 to N, and you should build some roads such that ...

  7. Constructing Roads POJ - 2421 (最小生成树)

    思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将    所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...

  8. hdu1025 Constructing Roads In JGShining#39;s Kingdom(二分+dp)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem ...

  9. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

最新文章

  1. boost::pfr::detail::fields_count相关的测试程序
  2. boost::hana::map_用法的测试程序
  3. PulseAudio 设计和实现浅析
  4. jmeter压力测试_用Jmeter实现对接口的压力测试
  5. 正确设置asp.net网站的404错误页面
  6. 运筹优化(十五)--应用模型之分配问题
  7. 【luogu P2939 [USACO09FEB]改造路Revamping Trails】 题解
  8. read()/write()的生命旅程——前言与目录
  9. php 检查货币类型_php 判断函数是否为费用类型(金额/货币:6.02)有小数点
  10. 单目标跟踪算法:SiamRPN++
  11. ubuntu桌面美化mac_教程:为你的linux桌面带来Mac OS Mojave的体验
  12. python检索论文_一种基于Python的音乐检索方法的研究
  13. 【html】edm 邮件制作指南
  14. 2022年双11淘宝满减规则解读
  15. CAD看图如何在电脑上快速找到并打开指定CAD图纸
  16. 58同城|TEG技术工程平台群-闪电面试专场内推(12月7日)
  17. 【12月英语——快乐中学习】
  18. 如何解决MacOS Big Sur,打印错误:ERROR invalidcontent?
  19. 常用MySQL字段类型解析
  20. 常见的HTT相应状态码

热门文章

  1. RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密
  2. (六)6-3Mysql操作据二
  3. 读《我是一只IT小小鸟》笔记
  4. 百度地图动态插入标注
  5. 锁分区提升并发,以及锁等待实例
  6. NYOJ-172 小珂的图表
  7. 在WCF中使用Flag Enumerations
  8. 做倒计时一天_不知道这6个管理时间的工具,做不好时间管理
  9. ssh项目放到服务器上出现404,项目运行一段时间,后台程序无法启动,404错误
  10. python调用ffmpeg_Python - FFmpeg