Codeforces Round #529 (Div. 3) F.Make It Connected
题意:
有 n 个顶点,每个顶点有个花费 a[ i ],连接顶点 u,v 需要花费 a[v]+a[u]的代价。
有 m 个特殊边,每条边有三个参数 u,v,w 代表的意思是连接 u,v 的花费可以不是 a[v]+a[u] 而是 w(当然选择小的那个啦)。
求联通所有的顶点需要的最少花费?
题解:
首先,需要建图,改如何建呢?
考虑到贪心的思路,首先不考虑 m 个特殊边,如何用最少的花费联通所有顶点呢?
答案是找到 a[ i ] 最少的顶点 i,其他 n-1 个顶点全部连向 i 。
将这 n-1 条边记录下来,在加上 m 条特殊边,然后,就是求最小生成树的模板题了,emmmmmm
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define ll __int64
#define pb(x) push_back(x)
const int maxn=2e5+; int n,m;
ll a[maxn];
struct Node
{
int u,v;
ll w;
Node(int a,int b,ll c):u(a),v(b),w(c){}
};
vector<Node >G;
void addEdge(int u,int v,ll w)
{
G.pb(Node(u,v,w));
} int fa[*maxn];
int Find(int x)
{
int r=x;
while(r != fa[r])
r=fa[r];
while(fa[x] != r)
{
int temp=fa[x];
fa[x]=r;
x=temp;
}
return r;
}
bool Union(int x,int y)
{
x=Find(x),y=Find(y);
if(x != y)
{
fa[x]=y;
return true;
}
return false;
}
bool cmp(Node _a,Node _b)
{
return _a.w < _b.w;
}
ll Solve()
{
sort(G.begin(),G.end(),cmp);
ll res=;
for(int i=;i < G.size();++i)
{
Node e=G[i];
if(Union(e.u,e.v))
res += e.w;
}
return res;
}
int main()
{
scanf("%d%d",&n,&m);
int minP=;
for(int i=;i <= n;++i)
{
fa[i]=i;
scanf("%I64d",a+i);
minP=(minP == || a[minP] > a[i] ? i:minP);
}
for(int i=;i <= n;++i)
addEdge(i,minP,a[i]+a[minP]);
for(int i=;i <= m;++i)
{
int u,v;
ll w;
scanf("%d%d%I64d",&u,&v,&w);
addEdge(u,v,w);
}
printf("%I64d",Solve());
return ;
}
Kruskal
Codeforces Round #529 (Div. 3) F.Make It Connected的更多相关文章
- Codeforces Round #529 (Div. 3) F. Make It Connected (贪心,最小生成树)
题意:给你\(n\)个点,每个点都有权值,现在要在这\(n\)个点中连一颗最小树,每两个点连一条边的边权为两个点的点权,现在还另外给了你几条边和边权,求最小权重. 题解:对于刚开始所给的\(n\)个点 ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- C-Lodop对大小写敏感 不要使用大小混写
C-Lodop是对大小写敏感的,而以前的Lodop控件,对于大小混写有可能可以用,而目前由于高版本的火狐谷歌不再支持np插件,为了兼容所有浏览器,就要使用c-lodop,或像Lodop官网的样例一样, ...
- How to blog on Github
git clone https://github.com/test/test.github.io.git cd ~/test.github.io git config --global push.de ...
- Nintex Forms Drop-Down "z-index"
Now we’ve got the issue, that if we are working with a “Person-Column”, the drop-down where you can ...
- ES6字符串操作
讨论字符串操作之前,我们先来了解一下Unicode 编码的由来,因为Js中的字符串就是一系列Unicode码的集合. 我们都知道,世界上存在着各种各样的语言,汉语,英语,日语等,相对应的,也就存在各种 ...
- sql练习题及经典题
https://blog.csdn.net/mrbcy/article/details/68965271 经典例题 19.查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录. S ...
- IDEA下载依赖时提示 resolving dependencies of xxx, yyy
IDEA下载依赖时提示 resolving dependencies of xxx, yyy ,卡住不动 使用Maven命令可以更清楚地分析问题,在IDEA命令行窗口执行mvn compile命令,提 ...
- CentOS 安装git
git安装 yum install git 初始化git “git init --bare 文件夹” 添加所有 “git add . ” 提交 “git commit -m "提交信息&qu ...
- windows新增/修改/删除系统环境变量bat示例,一键配置JAVA_HOME
setx JAVA_HOME "C:\Program Files\java\jdk1.6.0_27" /m setx classpath = ".;%JAVA_HOME% ...
- kubernetes ceph-rbd挂载步骤 类型PersistentVolume
k8s集群每一台上面都要安装客户端: ceph-deploy install k8s的ip地址 创建一个k8s操作用户: ceph auth add client.k8s mon 'allow r ...
- bzoj 1015: [JSOI2008]星球大战starwar (逆向思维+并查集)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1015 思路: 题目是要我们对当前图拆掉k个点,问,每拆一个点后图中有多少个联通块,我们可以逆 ...