Codeforces 1095F Make It Connected(最小生成树)
题目链接:Make It Connected
题意:给定一张$n$个顶点(每个顶点有权值$a_i$)的无向图,和已连接的拥有边权$w_i$的$m$条边,顶点u和顶点v直接如果新建边,边权为$a_u+a_v$,求图连通的最小边权和。
题解:假定连接三个顶点u,v,p,顶点权值按$a_u,a_v,a_p$从小到大排序。连通三个顶点的最小边权和为$(a_u+a_v)+(a_u+a_p)$,即最小权值的顶点连接其他顶点时,花费最小,推广到n个顶点也相同。因此该题就是m+n-1条边求小最小生成树即可。
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std; #define eps 1e-8
#define pb push_back
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define clr(a,b) memset(a,b,sizeof(a)
#define bugc(_) cerr << (#_) << " = " << (_) << endl
#define FAST_IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) const int N=2e5+;
typedef long long ll;
typedef unsigned long long ull;
struct node{
int id;
ll a;
}p[N]; struct NODE{
int u,v;
ll cost;
}pi[*N]; bool cmp1(node x,node y){
return x.a<y.a;
} bool cmp2(NODE x,NODE y){
return x.cost<y.cost;
} int fa[*N];
int fi(int x){
return fa[x]==x?x:fa[x]=fi(fa[x]);
} int main(){
FAST_IO;
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++){
cin>>p[i].a;
p[i].id=i;
}
sort(p+,p++n,cmp1);
//n-1
for(int i=;i<=n;i++){
pi[i-+m].u=p[].id;
pi[i-+m].v=p[i].id;
pi[i-+m].cost=p[].a+p[i].a;
}
for(int i=;i<=m;i++){
cin>>pi[i].u>>pi[i].v>>pi[i].cost;
}
sort(pi+,pi++m+n-,cmp2);
ll ans=;
for(int i=;i<=*N;i++) fa[i]=i;
for(int i=;i<=m+n-;i++){
int x=pi[i].u;
int y=pi[i].v;
int fx=fi(x),fy=fi(y);
if(fx!=fy){
fa[fx]=fy;
ans+=pi[i].cost;
}
}
cout<<ans<<endl;
return ;
}
Codeforces 1095F Make It Connected(最小生成树)的更多相关文章
- Codeforces 1095F Make It Connected 【MST】
<题目链接> 题目大意: 给定一张n个顶点(每个顶点有点权)的无向图,并且给出边权为wi的m条边,顶点u和顶点v直接如果建边,边权为a_u + a_v,求图连通的最小边权和. 解题分析: ...
- Make It Connected CodeForces - 1095F (建图+最小生成树)
Make It Connected CodeForces - 1095F You are given an undirected graph consisting of nn vertices. A ...
- Codeforces 1108F MST Unification(最小生成树性质)
题目链接:MST Unification 题意:给定一张连通的无向带权图.存在给边权加一的操作,求最少操作数,使得最小生成树唯一. 题解:最小生成树在算法导论中有这个性质: 把一个连通无向图的生成树边 ...
- Codeforces 1120D (树形DP 或 最小生成树)
题意看这篇博客:https://blog.csdn.net/dreaming__ldx/article/details/88418543 思路看这篇:https://blog.csdn.net/cor ...
- Educational Codeforces Round 37 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 475 B Strongly Connected City【DFS】
题意:给出n行m列的十字路口,<代表从东向西,>从西向东,v从北向南,^从南向北,问在任意一个十字路口是否都能走到其他任意的十字路口 四个方向搜,搜完之后,判断每个点能够访问的点的数目是否 ...
- CodeForces - 76A:Gift (最小生成树 解决单调性问题是思想)
题意:给定N点M边的无向连通图,每条边有两个权值(g,s). 给定G,S. 让你给出一组(g0,s0)使得图中仅留下g<=g0, s<=s0的边之后,依然连通,并求Gg0+Ss0的最小值. ...
- Codeforces Round #529 (Div. 3) F. Make It Connected (贪心,最小生成树)
题意:给你\(n\)个点,每个点都有权值,现在要在这\(n\)个点中连一颗最小树,每两个点连一条边的边权为两个点的点权,现在还另外给了你几条边和边权,求最小权重. 题解:对于刚开始所给的\(n\)个点 ...
- CodeForces 76A Gift - 最小生成树
The kingdom of Olympia consists of N cities and M bidirectional roads. Each road connects exactly tw ...
随机推荐
- c#如何声明数据结构类型为null?
可以通过如下两种方式声明可为空的类型:System.Nullable<T> variable;T?variable:eg:int值是-2,147,483,648 到 2,147,483,6 ...
- Java高阶语法---transient
背景:听说transient Java高阶语法是挺进BAT必经之路. transient: Java中transient 关键字的作用,简单的说就是让某些被修饰的成员属性变量不被序列化. 这又扯到了序 ...
- Can`tconnect to MySQL server on 'localhost'(10061)问题解决
今天在登陆MySQL是登录不上出现了“Can`tconnect to MySQL server on ‘localhost’(10061)”的问题,于是便在网上到处搜资料查原因:但好多都是:让删除my ...
- SpringBoot JPA(实现查询多值)
JPA是java Persistence API简称,中文名:java持久层API,JPA是JCP组织发布的J2EE标准之一 1.创建DataSource连接池对象 <dependency> ...
- Android 最简单的测试UI卡顿
就两个类: public class BlockDetectByPrinter { private static final String START = ">>>> ...
- linux下mysql配置文件位置
在/usr/share/mysql/ 中找到my.cnf的配置文件,拷贝其中的my-huge.cnf 到 /etc/ 并命名为my.cnf
- emacs单词首字母,单词,区域大小写转换
从光标开始,处理单词后半部分: 快捷键 说明 M-c (capitalize-word) 首字母改为大写 M-u (upcase-word) 全部改为大写 M-l (downcase-word) 全部 ...
- linux 软链接的创建、删除和更新
大家都知道,有的时候,我们为了省下空间,都会使用链接的方式来进行引用操作.同样的,在系统级别也有.在Windows系列中,我们称其为快捷方式,在Linux中我们称其为链接(基本上都差不多了,其中可能有 ...
- nginx 配置 rewrite 跳转
在访问 test.com 网站时,会自动跳转到 www.test.com ,这是因为该网站做了 URL rewrite 重定向,一般网页重定向跳转分为两种,301 和 302 :301,302 都是H ...
- python selenium while 循环
while True: try: loadmore = browser.find_element_by_xpath('//div[@class="right"]/div[@clas ...