【bzoj2753】[SCOI2012]滑雪与时间胶囊
2016-07-23 19:52
435 查看
#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 ,vis ,q ; LL h ; 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; }
相关文章推荐
- 枚举+求桥 Codeforces701F Break Up
- java-学习-自我规划
- POJ1995 Raising Modulo Numbers
- 【bzoj1007】[HNOI2008]水平可见直线
- [概率dp 矩阵乘法] poj 3744 Scout YYF I
- IPTABLES-小结
- POJ 1258 Agri-Net
- java-面试-阿里
- POJ3468(线段树成段增加)
- hdu 5730(分治FFT)
- svn1
- 结构体和方法
- 【bzoj2462】[BeiJing2011]矩阵模板
- HDU 1863 畅通工程
- cocoapods的安装和使用 总结
- LeetCode[368] Largest Divisible Subset
- CodeForces 427A Police Recruits
- 杭电ACM--HDU-2053a/b+c/d和HDU-2504又见gcd
- poj1611 简单并查集
- 【bzoj4320】ShangHai2006 Homework