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之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- 老男孩python学习自修第十九天【异常处理】
1.常见的错误 TypeError 类型错误 NameError 没有该变量 ValueError 不期望的值 AttributeError 没有该属性 UnboundLocalError 没有该局部 ...
- MyBatis的XML中使用内部类的方式
内部类需要使用$符号连接,而不是点.,如 com.pingan.job.openapi.model.SMSESBResult$ReceiveResult$ResultInfo 从CSDN论坛查到的. ...
- Java HashMap的put操作(Java1.6)
https://www.cnblogs.com/skywang12345/p/3310835.html // 存储数据的Entry数组,长度是2的幂. // HashMap是采用拉链法实现的,每一个E ...
- ubuntu 完全卸载mysql
卸载 sudo apt-get --purge remove mysql-common -y sudo apt-get --purge remove mysql* -y sudo apt-get au ...
- ABP实践学习
一.
- 深度学习+CRF解决NER问题
参考https://github.com/shiyybua/NER 1.开发环境:python3.5+tensorflow1.5+pycharm 2.从https://github.com/shiyy ...
- MySQL的FIND_IN_SET()函数
今天在做项目时,看到了一个从没见过的MySQL函数——FIND_IN_SET(),顿时就产生了浓郁的兴趣,然后就搜了搜,翻了翻. 语法:FIND_IN_SET(str,strlist) 定义: 1. ...
- 实用的几个JS新特性(es 2016)
在Chrome 55下测试,可用. 1.箭头函数(arrow function) 以前写的匿名函数是这样的 function(){}, 现在可以简单写成这样()=>{} 如果直接return,没 ...
- Ibatis中的<trim>标签应用
<trim>的主要属性如下显示: <trim prefix="" prefixOverrides="" suffix="" ...
- Vue入门基础
前面的话 Vue中文文档写得很好,界面清爽,内容翔实.但文档毕竟不是教程,文档一上来出现了大量的新概念,对于新手而言,并不友好.个人还是比较喜欢类似于<JS高级程序设计>的风格,从浅入深, ...