欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

bzoj 1232: [Usaco2008Nov]安慰奶牛cheer【最小生成树】

发布时间:2023/12/13 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 bzoj 1232: [Usaco2008Nov]安慰奶牛cheer【最小生成树】 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

有趣
每条边在算答案的时候被算了二倍的边权值加上两个端点的权值,然后睡觉点额外加一次
所以可以用这个权做MST,然后加上点权最小的点

#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int N=100005; int n,m,a[N],f[N],ans=1e9,con; struct qwe {int u,v,w; }e[N]; bool cmp(const qwe &a,const qwe &b) {return a.w<b.w; } int read() {int r=0,f=1;char p=getchar();while(p>'9'||p<'0'){if(p=='-')f=-1;p=getchar();}while(p>='0'&&p<='9'){r=r*10+p-48;p=getchar();}return r*f; } int zhao(int x) {return x==f[x]?x:f[x]=zhao(f[x]); } int main() {n=read(),m=read();for(int i=1;i<=n;i++){a[i]=read();ans=min(ans,a[i]);f[i]=i;}for(int i=1;i<=m;i++){int x=read(),y=read(),z=read()*2+a[x]+a[y];e[i]=(qwe){x,y,z};}sort(e+1,e+m+1,cmp);for(int i=1;i<=m&&con<n-1;i++){int fu=zhao(e[i].u),fv=zhao(e[i].v);if(fu!=fv){f[fu]=fv;con++;ans+=e[i].w;}}printf("%d",ans);return 0; }

转载于:https://www.cnblogs.com/lokiii/p/8963826.html

总结

以上是生活随笔为你收集整理的bzoj 1232: [Usaco2008Nov]安慰奶牛cheer【最小生成树】的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。