#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\n",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\n",ans);
    return 0;
}

【bzoj2753】[SCOI2012]滑雪与时间胶囊的更多相关文章

  1. BZOJ2753 SCOI2012 滑雪与时间胶囊 【最小生成树】*

    BZOJ2753 SCOI2012 滑雪与时间胶囊 Description a180285非常喜欢滑雪.他来到一座雪山,这里分布着M条供滑行的轨道和N个轨道之间的交点(同时也是景点),而且每个景点都有 ...

  2. Bzoj2753 [SCOI2012]滑雪与时间胶囊

    2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 2282  Solved: 796 Descriptio ...

  3. bzoj2753[SCOI2012]滑雪与时间胶囊 最小生成树

    Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 2843  Solved: 993[Submit][Status][Discuss] Descripti ...

  4. BZOJ2753 SCOI2012滑雪与时间胶囊(最小生成树)

    首先显然可以把所有能到的点拎出来建个新图,这样第一问也就做好了. 剩下的部分似乎是一个裸的最小树形图.但显然这个东西是没什么学的必要的并且不太能跑过去. 考虑建出来的图有什么性质.可以发现如果没有高度 ...

  5. BZOJ2753 [SCOI2012]滑雪与时间胶囊 【kruskal】

    题目链接 BZOJ2753 题解 完了我连\(kruskal\)裸题都做不出来了.. 题目是求最小树形图,即有向图最小生成树 我们不能直接上\(kruskal\),而要保证先加入前面的点, 所以我们排 ...

  6. [BZOJ2753][SCOI2012]滑雪与时间胶囊(特殊的有向树形图)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2753 分析: 第一问:直接BFS扩展知道无法扩展 第二问: 看似就是最小树形图啊= = ...

  7. 2019.01.17 bzoj2753: [SCOI2012]滑雪与时间胶囊(最小生成树)

    传送门 最小生成树菜题. 题意:给出一些有向边,问有向的最小生成树. 思路:先dfsdfsdfs一把所有有用的边都存起来,然后按终点点权为第一关键字,边权为第二关键字给边排序保证最小生成树的合法性,排 ...

  8. BZOJ 2753 [SCOI2012] 滑雪和时间胶囊 最小生成树

    题目链接: 题目 2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MB 问题描述 a180285非常喜欢滑雪.他来到一座雪山, ...

  9. bzoj 2753: [SCOI2012]滑雪与时间胶囊 -- 最小生成树

    2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec  Memory Limit: 128 MB Description a180285非常喜欢滑雪.他来到一座雪山,这 ...

  10. 【BZOJ 2753】 2753: [SCOI2012]滑雪与时间胶囊 (分层最小树形图,MST)

    2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 2457  Solved: 859 Descriptio ...

随机推荐

  1. 小而美的Promise库——promiz源码浅析

    背景 在上一篇博客[[译]前端基础知识储备--Promise/A+规范](https://segmentfault.com/a/11...,我们介绍了Promise/A+规范的具体条目.在本文中,我们 ...

  2. python 学习总结1

    计算机与程序设计 一.计算机的概念 1.计算机是根据指令操作数据的设备. 2.计算机主要包括两个功能性一个是功能性另一个是计算性 功能性是对数据的操作,表现为数据计算,输入输出处理和结果存储 可编程性 ...

  3. tomcat官网改版后下载方式

    位于disk/tomcat目录下 http://archive.apache.org/dist/tomcat/ 具体例如:http://archive.apache.org/dist/tomcat/t ...

  4. 牛客网暑期ACM多校训练营(第四场) J 贪心

    链接: https://www.nowcoder.com/acm/contest/143/J #include<bits/stdc++.h> using namespace std; lo ...

  5. [第一波模拟\day1\T2]{分班}(divide.cpp)

    [题目描述] 小N,小A,小T又大了一岁了. 现在,他们已经是高二年级的学生了.众所周知,高二的小朋友是要进行文理科分班考试的,这样子的话,三个好朋友说不定就会不分在一个班. 于是三个人决定,都考平均 ...

  6. ES6(对象扩展)

    ES6(对象(object)新增特性) 1.简介表示法 o,k 为属性:这种为无方法的情况. 这种为有方法. 2.属性表达式 ES6中,[a]就是 b . 3.新增API 1.数组也是引用类型 2.O ...

  7. python3--产生偏移和元素:enumerate

    之前,我们讨论过通过range来产生字符串中元素的偏移值.而不是那些偏移值处的元素.不过,在有些程序中.我们两者都需要,要用的元素以及值个元素的偏移值.从传统意义来讲,这是简单的for循环,他同时也持 ...

  8. bzoj 3772 精神污染 主席树+dfs序

    精神污染 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 637  Solved: 177[Submit][Status][Discuss] Descri ...

  9. Vim pre-work

    1.先学会touch typing盲打是一切的基础 重点在于手眼协调 如果实现不了盲打.一切高效率的Vim操作都将无从做起 2.vim的使用 2.1.hjkl的移动 推荐练习贪吃蛇  和3D平衡球   ...

  10. The Bottom of a Graph

                                    poj——The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K ...