zoukankan      html  css  js  c++  java
  • 【bzoj2753】[SCOI2012]滑雪与时间胶囊

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    using namespace std;
     
    typedef long long LL;
     
    #define N 1000010
     
    struct edge
    {
        LL to,next;
    }e[N<<1];
    LL head[N<<1];
    LL cnt;
     
    struct Node
    {
        LL x,y,z;
    }a[N<<1];
     
    LL n,m;
    LL x,y,z;
    LL tot,ans;
     
    LL fa[N],vis[N],q[N];
     
    LL h[N];
     
    int find(LL x)
    {
        return fa[x]==x ? x : fa[x]=find(fa[x]);
    }
     
    void link(LL x,LL y,LL z)
    {
        e[++cnt]=(edge){y,head[x]};
        head[x]=cnt;
        a[cnt]=(Node){x,y,z};
    }
     
    int cmp(Node x,Node y)
    {
        return h[x.y]>h[y.y] || (h[x.y]==h[y.y] && x.z<y.z);
    }
     
    void bfs()
    {
        queue<LL>q;
        q.push(1);
        vis[1]=1;
        while (!q.empty())
        {
            LL now=q.front();
            q.pop();
            for (LL i=head[now];i;i=e[i].next)
            {
                LL t=e[i].to;
                if (!vis[t])
                {
                    q.push(t);
                    vis[t]=1;
                    tot++;
                }
            }
        }
    }
     
    int main()
    {
        scanf("%lld%lld",&n,&m);
        for (LL i=1;i<=n;i++)
            scanf("%lld",&h[i]);
        for (LL i=1;i<=m;i++)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            if (h[x]>=h[y])
                link(x,y,z);
            if (h[x]<=h[y])
                link(y,x,z);
        }
        bfs();
        printf("%lld ",tot+1);
        for (LL i=1;i<=n;i++)
            fa[i]=i;
        sort(a+1,a+cnt+1,cmp);
        for (LL i=1;i<=cnt;i++)
        {
            x=a[i].x;
            y=a[i].y;
            if (!vis[x] || !vis[y])
                continue;
            LL r1=find(x),r2=find(y);
            if (r1!=r2)
                fa[r1]=r2,ans+=a[i].z;
        }
        printf("%lld
    ",ans);
        return 0;
    }
    

      

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    using namespace std;
     
    typedef long long LL;
     
    #define N 1000010
     
    struct edge
    {
        LL to,next;
    }e[N<<1];
    LL head[N<<1];
    LL cnt;
     
    struct Node
    {
        LL x,y,z;
    }a[N<<1];
     
    LL n,m;
    LL x,y,z;
    LL tot,ans;
     
    LL fa[N],vis[N],q[N];
     
    LL h[N];
     
    int find(LL x)
    {
        return fa[x]==x ? x : fa[x]=find(fa[x]);
    }
     
    void link(LL x,LL y,LL z)
    {
        e[++cnt]=(edge){y,head[x]};
        head[x]=cnt;
        a[cnt]=(Node){x,y,z};
    }
     
    int cmp(Node x,Node y)
    {
        return h[x.y]>h[y.y] || (h[x.y]==h[y.y] && x.z<y.z);
    }
     
    void bfs()
    {
        queue<LL>q;
        q.push(1);
        vis[1]=1;
        while (!q.empty())
        {
            LL now=q.front();
            q.pop();
            for (LL i=head[now];i;i=e[i].next)
            {
                LL t=e[i].to;
                if (!vis[t])
                {
                    q.push(t);
                    vis[t]=1;
                    tot++;
                }
            }
        }
    }
     
    int main()
    {
        scanf("%lld%lld",&n,&m);
        for (LL i=1;i<=n;i++)
            scanf("%lld",&h[i]);
        for (LL i=1;i<=m;i++)
        {
            scanf("%lld%lld%lld",&x,&y,&z);
            if (h[x]>=h[y])
                link(x,y,z);
            if (h[x]<=h[y])
                link(y,x,z);
        }
        bfs();
        printf("%lld ",tot+1);
        for (LL i=1;i<=n;i++)
            fa[i]=i;
        sort(a+1,a+cnt+1,cmp);
        for (LL i=1;i<=cnt;i++)
        {
            x=a[i].x;
            y=a[i].y;
            if (!vis[x] || !vis[y])
                continue;
            LL r1=find(x),r2=find(y);
            if (r1!=r2)
                fa[r1]=r2,ans+=a[i].z;
        }
        printf("%lld ",ans);
        return 0;
    }
  • 相关阅读:
    Java 集合深入理解(15):AbstractMap
    数据库服务器的性能调优
    重温数据结构:哈希 哈希函数 哈希表
    Linux 虚存 linux2.6内核特性
    不想做却不得不做某份工作,怎么破?
    Java 集合深入理解(14):Map 概述
    Linux 虚存的性能问题
    Android重写getResources规避用户调整系统字体大小影响Android屏幕适配
    Android CardView设置成普通的Framelayout
    linux系统性能监控--网络利用率
  • 原文地址:https://www.cnblogs.com/yangjiyuan/p/5699405.html
Copyright © 2011-2022 走看看