开始看错求最短路了。。

但是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的更多相关文章

  1. Codeforces.888G.Xor-MST(Borůvka算法求MST 贪心 Trie)

    题目链接 \(Description\) 有一张\(n\)个点的完全图,每个点的权值为\(a_i\),两个点之间的边权为\(a_i\ xor\ a_j\).求该图的最小生成树. \(n\leq2*10 ...

  2. HDU 5627 Clarke and MST &意义下最大生成树 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5627 题意:Bestcoder的一道题,让你求&意义下的最大生成树. 解法: 贪心,我们从高位 ...

  3. 基于MST的立体匹配及相关改进(A Non-Local Cost Aggregation Method for Stereo Matching)

    怀着很纠结的心情来总结这篇论文,这主要是因为作者提虽然供了源代码,但是我并没有仔细去深究他的code,只是把他的算法加进了自己的项目.希望以后有时间能把MST这一结构自己编程实现!! 论文题目是基于非 ...

  4. codeforces 721B B. Passwords(贪心)

    题目链接: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. codeforces 480A A. Exams(贪心)

    题目链接: A. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. codeforces 484A A. Bits(贪心)

    题目链接: A. Bits time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. codeforces 709C C. Letters Cyclic Shift(贪心)

    题目链接: C. Letters Cyclic Shift 题意: 现在一串小写的英文字符,每个字符可以变成它前边的字符即b-a,c-a,a-z这样,选一个字串变换,使得得到的字符串字典序最小; 思路 ...

  8. UVALive 5713 Qin Shi Huang's National Road System秦始皇修路(MST,最小瓶颈路)

    题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最 ...

  9. codeforces 613B B. Skills(枚举+二分+贪心)

    题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

随机推荐

  1. Samza基本概念

  2. yppasswd, ypchfn, ypchsh - 修改你在NIS数据库中的密码

    SYNOPSIS(总览) yppasswd [-f] [-l] [-p] [user] ypchfn [user] ypchsh [user] DESCRIPTION(描述) 在Linux中,标准的 ...

  3. python pathlib模块详解

    python pathlib模块详解    

  4. MYSQL分数排名

    编写一个 SQL 查询来实现分数排名.如果两个分数相同,则两个分数排名(Rank)相同.请注意,平分后的下一个名次应该是下一个连续的整数值.换句话说,名次之间不应该有“间隔”. +----+----- ...

  5. Dubbo执行流程?

    Dubbo执行流程? 0 start: 启动Spring容器时,自动启动Dubbo的Provider 1.register: Dubbo的Provider在启动后自动会去注册中心注册内容.注册的内容包 ...

  6. node-express(1)建立post、get、跨域问题解决方案

    首先下载express:npm i express let ess=require('express'); let app=ess(); let bodyParser=require('body-pa ...

  7. 【POJ3155】生活的艰辛Hard Life

    题面 Description ADN公司内部共 n个员工,员工之间可能曾经因为小事有了过节,总是闹矛盾.若员工u和员工 v有矛盾,用边(u, v)表示,共 m个矛盾.最近,ADN公司内部越来越不团结, ...

  8. leetcode-685-冗余连接②

    题目描述: 参考后提交:并查集: class Solution: def findRedundantDirectedConnection(self, edges: List[List[int]]) - ...

  9. leetcode-264-丑数

    题目描述: 方法一:堆 O(nlogn) class Solution: def nthUglyNumber(self, n: int) -> int: import heapq heap = ...

  10. jQuery 遍历 - eq() 方法

    <!DOCTYPE html> <html> <head> <style> div { width:60px; height:60px; margin: ...