题目链接

BZOJ2753

题解

完了我连\(kruskal\)裸题都做不出来了。。

题目是求最小树形图,即有向图最小生成树

我们不能直接上\(kruskal\),而要保证先加入前面的点,

所以我们排序的时候第一关键字改为高度即可

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 1000005,maxm = 2000005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int high[maxn],n,m;
int h[maxn],ne;
struct EDGE{int from,to,nxt; LL w;}ed[maxm];
inline void build(int u,int v,LL w){
ed[++ne] = (EDGE){u,v,h[u],w}; h[u] = ne;
}
int tot;
struct edge{int a,b; LL w;}e[maxm];
inline bool operator <(const edge& a,const edge& b){
return high[a.b] == high[b.b] ? a.w < b.w : high[a.b] > high[b.b];
}
int vis[maxn],q[maxn],head,tail,N;
void bfs(){
q[head = tail = 0] = 1; vis[1] = true; N = 1;
int u;
while (head <= tail){
u = q[head++];
Redge(u) if (!vis[to = ed[k].to]){
N++; vis[to] = true; q[++tail] = to;
}
}
}
int pre[maxn];
int find(int u){return u == pre[u] ? u : pre[u] = find(pre[u]);}
void kruskal(){
for (int i = 1; i <= n; i++) pre[i] = i;
LL ans = 0; int cnt = N,fa,fb;
sort(e + 1,e + 1 + tot);
for (int i = 1; i <= tot && cnt > 1; i++){
fa = find(e[i].a); fb = find(e[i].b);
if (fa != fb){
pre[fb] = fa;
cnt--;
ans += e[i].w;
}
}
printf("%d %lld\n",N,ans);
}
int main(){
n = read(); m = read();
for (int i = 1; i <= n; i++) high[i] = read();
int a,b,w;
for (int i = 1; i <= m; i++){
a = read(); b = read(); w = read();
if (high[a] >= high[b]) build(a,b,w);
if (high[b] >= high[a]) build(b,a,w);
}
bfs();
for (int i = 1; i <= ne; i++){
if (vis[ed[i].from] && vis[ed[i].to])
e[++tot] = (edge){ed[i].from,ed[i].to,ed[i].w};
}
kruskal();
return 0;
}

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

  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]滑雪与时间胶囊(特殊的有向树形图)

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

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

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

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

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

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

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

  8. CDOJ 42/BZOJ 2753 滑雪与时间胶囊 kruskal

    2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1376  Solved: 487[Submit][St ...

  9. 2753: [SCOI2012]滑雪与时间胶囊

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

随机推荐

  1. 使用virtual安装Windows系列操作系统总结

    最近在安装Windows操作系统的过程中,发现总是报错,无法安装成功,后来经过不断地摸索,发现根本的问题在于镜像,所以在以后的大文件传输下载后,一定要校验其MD5值是否与源文件一致,需要的朋友可以联系 ...

  2. python并发编程之多进程、多线程、异步、协程、通信队列Queue和池Pool的实现和应用

    什么是多任务? 简单地说,就是操作系统可以同时运行多个任务.实现多任务有多种方式,线程.进程.协程. 并行和并发的区别? 并发:指的是任务数多余cpu核数,通过操作系统的各种任务调度算法,实现用多个任 ...

  3. 谈谈WPF中的CollectionView与CollectionViewSource (1)

    原文:谈谈WPF中的CollectionView与CollectionViewSource (1) 谈谈WPF中的CollectionView与CollectionViewSource (1)     ...

  4. 关于springboot 连接mysql 数据库报错问题

    springboot连接MySQL运行报错: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more ...

  5. 使用TFS需要注意的地方

    1. 用管理员添加了本地映射,然后用其他用户就添加不了映射,一定要先用管理员账户去把映射 删除掉: 2. 在正式使用TFS时,一定需要在VS工具的设置里面,设置一下,签出时自动获取最新的代码.(默认是 ...

  6. python内置模块[re]

    python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...

  7. ArcGIS Server远程处理服务器(环境设置)

    当使用ArcGIS Server做远程处理服务器执行影像处理操作时,提示ERROR 999999通用错误代码,如下: Start Time: Mon Jul 03 13:49:06 2017Distr ...

  8. 预装win8的笔记本如何重装win7

    测试电脑联想T440. 开机按F1,然后Enter,进入Bios设置. 先关闭Secure Boot,然后设置为Legacy Boot. 之后才能设置U盘为第一启动盘. 进入老毛桃的PE系统,使用Di ...

  9. mysql修改外部访问权限

    mysql>use mysql; mysql>update user set host =’%’ where user=’root’ mysql>select host,user f ...

  10. 面向对象 公有私有 property classmethod staticmethod

    接口类(抽象类)--------就是一种规范 面向对象的私有与公有 对于每一个类的成员而言都有两种形式: 公有成员,在任何地方都能访问 私有成员,只有在类的内部才能方法 私有成员和公有成员的访问限制不 ...