贪心+MST——cf1095F
开始看错求最短路了。。
但是MST的思路和最短路也差不多
就是先不考虑特殊边,用最小点做一个生成树
然后加入特殊边,进行一次krus即可
#include<bits/stdc++.h>
#include<queue>
using namespace std;
#define maxn 400005
#define ll long long
ll s,ans,n,m,a[maxn],Min,tot;
struct Edge{ll x,y,w;}e[maxn<<];
int cmp(Edge a,Edge b){return a.w<b.w;}
int F[maxn];
int find(int x){return F[x]==x?x:F[x]=find(F[x]);}
void add(ll u,ll v,ll w){
e[++tot].x=u;e[tot].y=v;e[tot].w=w;
} void krus(){
sort(e+,e++m,cmp);
for(int i=;i<=m;i++){
int f1=find(e[i].x),f2=find(e[i].y);
if(f1!=f2){
F[f1]=f2;
ans+=e[i].w;
}
}
} int main(){
cin>>n>>m;
for(int i=;i<=n;i++)F[i]=i;
Min=0x3f3f3f3f3f3f3f3f;
for(int i=;i<=n;i++){//找最小的点
scanf("%lld",&a[i]);
if(a[i]<Min){
Min=a[i];
s=i;
}
} for(int i=;i<=n;i++)
if(i!=s)
add(s,i,Min+a[i]); for(int i=;i<=m;i++){
ll x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
add(x,y,z);
} m+=tot;//边的规模
krus(); cout<<ans<<endl;
}
贪心+MST——cf1095F的更多相关文章
- Codeforces.888G.Xor-MST(Borůvka算法求MST 贪心 Trie)
题目链接 \(Description\) 有一张\(n\)个点的完全图,每个点的权值为\(a_i\),两个点之间的边权为\(a_i\ xor\ a_j\).求该图的最小生成树. \(n\leq2*10 ...
- HDU 5627 Clarke and MST &意义下最大生成树 贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5627 题意:Bestcoder的一道题,让你求&意义下的最大生成树. 解法: 贪心,我们从高位 ...
- 基于MST的立体匹配及相关改进(A Non-Local Cost Aggregation Method for Stereo Matching)
怀着很纠结的心情来总结这篇论文,这主要是因为作者提虽然供了源代码,但是我并没有仔细去深究他的code,只是把他的算法加进了自己的项目.希望以后有时间能把MST这一结构自己编程实现!! 论文题目是基于非 ...
- codeforces 721B B. Passwords(贪心)
题目链接: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 480A A. Exams(贪心)
题目链接: A. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- codeforces 484A A. Bits(贪心)
题目链接: A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- codeforces 709C C. Letters Cyclic Shift(贪心)
题目链接: C. Letters Cyclic Shift 题意: 现在一串小写的英文字符,每个字符可以变成它前边的字符即b-a,c-a,a-z这样,选一个字串变换,使得得到的字符串字典序最小; 思路 ...
- UVALive 5713 Qin Shi Huang's National Road System秦始皇修路(MST,最小瓶颈路)
题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最 ...
- codeforces 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
随机推荐
- ajax的两种应用方式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- python的magic methods
https://pycoders-weekly-chinese.readthedocs.io/en/latest/issue6/a-guide-to-pythons-magic-methods.htm ...
- 如何实现qq消息轰炸
1.新建一个文本文档复制以下代码 Set WshShell = WScript.CreateObject("Wscript.Shell")WshShell.AppActivate& ...
- pandas读书笔记、重新索引
重新索引 pandas对象的一个重要方法是 reindex ,其作用是创建一个适应新索引的新对象. #reindex函数的参数 reindex(index,method,fill_value, ...
- 2019.7.3模拟 光影交错(穷举+概率dp)
题目大意: 每一轮有pl的概率得到正面的牌,pd的概率得到负面的牌,1-pl-pd的概率得到无属性牌. 每一轮过后,都有p的概率结束游戏,1-p的概率开始下一轮. 问期望有多少轮后正面的牌数严格大于负 ...
- luoguP3370 【模板】字符串哈希 [hash]
题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...
- Python 空值和非空值
1)任何值为0的值都是false,任何非0的值都是true if -0.0: print 'yes' #不打印yes if -0.1: print 'yes' #打印yes 2)任何为空的值都是fla ...
- Codeforces768B-Code For 1-类似线段树-枚举+单点更新or区间更新
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. 每次把\(n\ ...
- 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryById
ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryById 1.返回顶部 1. templateMessage.getTem ...
- nginx启停脚本
安装nginx时,源码包中未带官方的启动脚本,也就无法使用service nginxd start这种启动方式,查了下资料自己写了一个: #!/bin/bash #@version: #@author ...