还是一道很简单的基础题,就是一个最短路径树的类型题目

我们首先可以发现这棵树必定满足从1出发到其它点的距离都是原图中的最短路

换句话说,这棵树上的每一条边都是原图从1出发到其它点的最短路上的边

那么直接跑最短路,SPFA,不存在的?我只信DJ,然后记录那些边在最短路上

然后直接跑MST即可。是不是很经典的水题

然后我又莫名拿了Rank1(没办法天生自带小常数

CODE

  1. #include<cstdio>
  2. #include<cctype>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. typedef long long LL;
  8. const int N=3e5+5;
  9. struct edge
  10. {
  11. int from,to,next,v;
  12. }e[N<<1];
  13. struct heap
  14. {
  15. int num; LL s;
  16. bool operator < (const heap a) const { return a.s<s; }
  17. };
  18. struct data
  19. {
  20. int l,r,s;
  21. }a[N];
  22. priority_queue <heap> small;
  23. int head[N],cnt,father[N],n,m,x,y,z,s,tot;
  24. LL dis[N];
  25. bool vis[N];
  26. inline char tc(void)
  27. {
  28. static char fl[100000],*A=fl,*B=fl;
  29. return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
  30. }
  31. inline void read(int &x)
  32. {
  33. x=0; char ch; while (!isdigit(ch=tc()));
  34. while (x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=tc()));
  35. }
  36. inline void double_add(int x,int y,int z)
  37. {
  38. e[++cnt].from=x; e[cnt].to=y; e[cnt].next=head[x]; e[cnt].v=z; head[x]=cnt;
  39. e[++cnt].from=y; e[cnt].to=x; e[cnt].next=head[y]; e[cnt].v=z; head[y]=cnt;
  40. }
  41. inline bool cmp(data a,data b)
  42. {
  43. return a.s<b.s;
  44. }
  45. inline int getfather(int k)
  46. {
  47. return father[k]^k?father[k]=getfather(father[k]):k;
  48. }
  49. inline LL MST(void)
  50. {
  51. register int i; LL ans=0;
  52. sort(a+1,a+tot+1,cmp);
  53. for (i=1;i<=n;++i)
  54. father[i]=i;
  55. for (i=1;i<=tot;++i)
  56. {
  57. int fx=getfather(a[i].l),fy=getfather(a[i].r);
  58. if (!vis[a[i].r]&&fx!=fy) father[fx]=fy,ans+=a[i].s,vis[a[i].r]=1;
  59. }
  60. return ans;
  61. }
  62. int main()
  63. {
  64. //freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
  65. int i; read(n); read(m);
  66. memset(head,-1,sizeof(head)); memset(e,-1,sizeof(e));
  67. for (i=1;i<=m;++i)
  68. read(x),read(y),read(z),double_add(x,y,z);
  69. memset(dis,63,sizeof(dis)); read(s);
  70. dis[s]=0; small.push((heap){s,0});
  71. while (!small.empty())
  72. {
  73. int now=small.top().num; small.pop();
  74. if (vis[now]) continue; vis[now]=1;
  75. for (i=head[now];i!=-1;i=e[i].next)
  76. if (dis[e[i].to]>dis[now]+1LL*e[i].v)
  77. {
  78. dis[e[i].to]=dis[now]+1LL*e[i].v;
  79. small.push((heap){e[i].to,dis[e[i].to]});
  80. }
  81. }
  82. memset(vis,0,sizeof(vis));
  83. for (i=1;i<=cnt;++i)
  84. if (dis[e[i].from]+1LL*e[i].v==dis[e[i].to]) a[++tot]=(data){e[i].from,e[i].to,e[i].v};
  85. return printf("%lld",MST()),0;
  86. }

51Nod 1443 路径和树的更多相关文章

  1. 51nod 1443 路径和树(最短路)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1443 1443 路径和树 题目来源: CodeForces ...

  2. 51nod 1443 路径和树(最短路树)

    题目链接:路径和树 题意:给定无向带权连通图,求从u开始边权和最小的最短路树,输出最小边权和. 题解:构造出最短路树,把存留下来的边权全部加起来.(跑dijkstra的时候松弛加上$ < $变成 ...

  3. 51Nod 1443 路径和树 —— dijkstra

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1443 首先要得到一个最短路树: 注意边权和最小,因为在最短路中,每 ...

  4. 51nod 1443 路径和树——最短路生成树

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1443 不只是做一遍最短路.还要在可以选的边里选最短的才行. 以为是 ...

  5. [BZOJ1576] [BZOJ3694] [USACO2009Jan] 安全路径(最短路径+树链剖分)

    [BZOJ1576] [BZOJ3694] [USACO2009Jan] 安全路径(最短路径+树链剖分) 题面 BZOJ1576和BZOJ3694几乎一模一样,只是BZOJ3694直接给出了最短路树 ...

  6. 51nod 1681 公共祖先 | 树状数组

    51nod 1681 公共祖先 有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另一个未知的平行宇宙,这n人的祖辈关系仍然是树形结构,但他们相互之间的关系却完 ...

  7. 51Nod 1737 配对(树的重心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1737 题意: 思路: 树的重心. 树的重心就是其所以子树的最大的子树结点 ...

  8. 51nod 1272 思维/线段树

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 1272 最大距离 题目来源: Codility 基准时间限制:1 ...

  9. 51Nod 1967 路径定向 —— 欧拉回路

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1967 显然是欧拉回路问题,度数为奇数的点之间连边,跑欧拉回路就可以 ...

随机推荐

  1. Linux 磁盘分区方案简析

    Linux 磁盘分区方案简析 by:授客 QQ:1033553122   磁盘分区 任何硬盘在使用前都要进行分区.硬盘的分区有两种类型:主分区和扩展分区.一个硬盘上最多只能有4个主分区,其中一个主分区 ...

  2. 安卓开发_数据存储技术_sqlite

    一.SQLite SQLite第一个Alpha版本诞生于2000年5月,它是一款轻量级数据库,它的设计目标是嵌入式的,占用资源非常的低,只需要几百K的内存就够了.SQLite已经被多种软件和产品使用 ...

  3. webrtc学习: 部署stun和turn服务器

    webrtc的P2P穿透部分是由libjingle实现的. 步骤顺序大概是这样的: 1. 尝试直连. 2. 通过stun服务器进行穿透 3. 无法穿透则通过turn服务器中转. stun 服务器比较简 ...

  4. DevOps自动化工具集合

    版本控制&协作开发:GitHub.GitLab.BitBucket.SubVersion.Coding.Bazaar 自动化构建和测试:Apache Ant.Maven .Selenium.P ...

  5. sql 删除默认索引,对象 依赖于 列,由于一个或多个对象访问此列

    declare @name varchar(50)select  @name =b.name from sysobjects b join syscolumns aon b.id = a.cdefau ...

  6. Java计模模式之六 ----- 组合模式和过滤器模式

    前言 在上一篇中我们学习了结构型模式的外观模式和装饰器模式.本篇则来学习下组合模式和过滤器模式. 组合模式 简介 组合模式是用于把一组相似的对象当作一个单一的对象.组合模式依据树形结构来组合对象,用来 ...

  7. 5、爬虫之scrapy框架

    一 scrapy框架简介 1 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Sc ...

  8. 迷宫问题 dfs bfs 搜索

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  9. android Application Component研究之Activity(一)

    http://blog.csdn.net/windskier/article/details/7096521 终于下定决心写写ActivityManagerService的源码分析的文章了,Activ ...

  10. Redis String类型的API使用

    package com.daxin.jedis_datastructure; import org.junit.After; import org.junit.Before; import org.j ...