zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    面向对象-类
    模块04
    总结
    昨天的新的解决方法
    感冒了~ vs中py和vb实现一个小算法
    vs2015社区版不支持installshield
    网站被黑了
    2018/11/18(python)
    2018/11/14
    2018/11/12(python)
  • 原文地址:https://www.cnblogs.com/lokiii/p/8963826.html
Copyright © 2011-2022 走看看