题干:

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

题目大意:

大概意思就是给你一个图,然后告诉你每两个点之间的距离,然后给你一个q,下面q行,每行两个数代表这两个点是连通的,问你为了让整个图是个连通图,最短还需要修多长的路。

解题报告:

最小生成树裸题,,复习一下,,不解释了、、最后就是,加不加那个cnt计数对这道题都无所谓,因为数据量太小了,都是31ms。下面两个代码都贴上。

AC代码:(加cnt计数)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int n,tot;
int f[205];
struct Edge {int u,v;int w;Edge(){}Edge(int u,int v,int w):u(u),v(v),w(w){}
} e[MAX<<1];
bool cmp(Edge a,Edge b) {return a.w < b.w;
}
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);if(t1!=t2) f[t2]=t1;
}
int main()
{while(~scanf("%d",&n)) {tot=0;for(int i = 1; i<=n; i++) f[i] = i;for(int i = 1,w; i<=n; i++) {for(int j = 1; j<=n; j++) {scanf("%d",&w);if(i==j) continue;e[++tot] = Edge(i,j,w);e[++tot] = Edge(j,i,w);}}  int q,cnt = 0;scanf("%d",&q);while(q--) {int u,v;scanf("%d%d",&u,&v);if(getf(u)!=getf(v)) merge(u,v),cnt++;}sort(e+1,e+tot+1,cmp);ll ans = 0;for(int i = 1; i<=tot; i++) {int u = e[i].u,v = e[i].v;if(getf(u) == getf(v)) continue;merge(u,v);cnt++;ans += 1LL * e[i].w;if(cnt == n-1) break;}printf("%lld\n",ans);}return 0 ;}

AC代码2:(不加cnt计数)

//不带cnt计数  版本
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int n,tot;
int f[205];
struct Edge {int u,v;int w;Edge(){}Edge(int u,int v,int w):u(u),v(v),w(w){}
} e[MAX<<1];
bool cmp(Edge a,Edge b) {return a.w < b.w;
}
int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);if(t1!=t2) f[t2]=t1;
}
int main()
{while(~scanf("%d",&n)) {tot=0;for(int i = 1; i<=n; i++) f[i] = i;for(int i = 1,w; i<=n; i++) {for(int j = 1; j<=n; j++) {scanf("%d",&w);if(i==j) continue;e[++tot] = Edge(i,j,w);e[++tot] = Edge(j,i,w);}}  int q;scanf("%d",&q);while(q--) {int u,v;scanf("%d%d",&u,&v);merge(u,v);}sort(e+1,e+tot+1,cmp);ll ans = 0;for(int i = 1; i<=tot; i++) {int u = e[i].u,v = e[i].v;if(getf(u) == getf(v)) continue;merge(u,v);ans += 1LL * e[i].w;}printf("%lld\n",ans);}return 0 ;}

【HDU - 1102】Constructing Roads (最小生成树裸题模板)相关推荐

  1. 【OpenJ_Bailian - 1258】【POJ - 1258】Agri-Net (最小生成树裸题)

    题干: Farmer John has been elected mayor of his town! One of his campaign promises was to bring intern ...

  2. HDU1102 Constructing Roads 最小生成树

    点击打开链接 Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Ha ...

  3. CSP认证201412-4 最优灌溉[C++题解]:最小生成树裸题、Kruskal算法求最小生成树

    题目分析 来源:acwing 分析:这是一道最小生成树的裸题. 这里默写Kruskal求最小生成树的最小费用的模板. 最小生成树模板请参考笔者的另一篇博文: 最小生成树板子-AcWing 859. K ...

  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. hdu 1301 Jungle Roads 最小生成树

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

  6. HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)

    点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...

  7. BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】

    1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 2925  Solved: 1927 [Submit][S ...

  8. hdu 1025 Constructing Roads In JGShining's Kingdom

    http://acm.hdu.edu.cn/showproblem.php?pid=1025 题意:题目的意思就是有两种城市,穷和富,要富的运到穷的里面问你最多能建几条路. 思路:就是按穷的递增序列来 ...

  9. POJ2421 Constructing Roads 最小生成树

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

最新文章

  1. 【Android 高性能音频】AAudio 音频流 缓冲区 简介 ( AAudio 音频流内部缓冲区 | 缓冲区帧容量 | 缓冲区帧大小 | 音频数据读写缓冲区 )
  2. list.h在用户态下的应用
  3. HTTPS和HTTPS证书
  4. 【bzoj2238】Mst(树链剖分+线段树)
  5. 前端学习(668):分支导读
  6. erlang安装_消息中间件(八)- RabbitMQ - 安装
  7. mysql数据变化通通知机制_深入理解Notification机制
  8. 超能竞速大开眼界,iQOO 5系列正式发布
  9. 小米8屏幕指纹版(UD) 稳定版miui11刷magisk、太极参考
  10. 顶岗实习周记java方向_java程序员的实习周记
  11. oracle的单引号和双引号的深入举例分析
  12. 独孤求败——浅谈FireFox中file控件不能取到客户端文件的完整路径的问题
  13. 局域网即时通讯软件_无线局域网中,安卓手机和电脑的资源如何实现互传互访?...
  14. 【项目】#防翟天临老师翻车神器# ——实现文本查重
  15. 消防管道标志色号_消防标志的起步问题
  16. iOS-高德地图API的定位与搜索功能
  17. android调用截屏功能,调用安卓原生的截图功能
  18. 手机传输文件服务器,手机文件传输到服务器
  19. flush()的作用
  20. js中break关键字的用法。

热门文章

  1. 【数据结构与算法】二分查找
  2. [剑指offer]面试题第[57]题[Leetcode][第167题][JAVA][和为s的两个数字][两数之和][HashSet][二分][双指针]
  3. android在主程序中调用图片,009android初级篇之APP中使用系统相机相册等集成应用...
  4. python自动化框架测试实操_自动化框架之 python+selenium+pytest
  5. 了解WWW服务与HTTP协议 【入门与应用】
  6. php无限加载,php递归无限页面加载
  7. php代码expl,php – 参数号无效:参数未定义Explination
  8. windows 修改nginx端口号_分享Nginx搭建图片服务器简单实现
  9. 使用Jedis源码生成Jedis.jar
  10. html GPS坐标实现,JavaScript 实现GPS坐标点距离计算(两个经/纬度间的距离计算)...