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

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

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

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

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

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

CODE

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;
const int N=3e5+5;
struct edge
{
int from,to,next,v;
}e[N<<1];
struct heap
{
int num; LL s;
bool operator < (const heap a) const { return a.s<s; }
};
struct data
{
int l,r,s;
}a[N];
priority_queue <heap> small;
int head[N],cnt,father[N],n,m,x,y,z,s,tot;
LL dis[N];
bool vis[N];
inline char tc(void)
{
static char fl[100000],*A=fl,*B=fl;
return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(int &x)
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=tc()));
}
inline void double_add(int x,int y,int z)
{
e[++cnt].from=x; e[cnt].to=y; e[cnt].next=head[x]; e[cnt].v=z; head[x]=cnt;
e[++cnt].from=y; e[cnt].to=x; e[cnt].next=head[y]; e[cnt].v=z; head[y]=cnt;
}
inline bool cmp(data a,data b)
{
return a.s<b.s;
}
inline int getfather(int k)
{
return father[k]^k?father[k]=getfather(father[k]):k;
}
inline LL MST(void)
{
register int i; LL ans=0;
sort(a+1,a+tot+1,cmp);
for (i=1;i<=n;++i)
father[i]=i;
for (i=1;i<=tot;++i)
{
int fx=getfather(a[i].l),fy=getfather(a[i].r);
if (!vis[a[i].r]&&fx!=fy) father[fx]=fy,ans+=a[i].s,vis[a[i].r]=1;
}
return ans;
}
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
int i; read(n); read(m);
memset(head,-1,sizeof(head)); memset(e,-1,sizeof(e));
for (i=1;i<=m;++i)
read(x),read(y),read(z),double_add(x,y,z);
memset(dis,63,sizeof(dis)); read(s);
dis[s]=0; small.push((heap){s,0});
while (!small.empty())
{
int now=small.top().num; small.pop();
if (vis[now]) continue; vis[now]=1;
for (i=head[now];i!=-1;i=e[i].next)
if (dis[e[i].to]>dis[now]+1LL*e[i].v)
{
dis[e[i].to]=dis[now]+1LL*e[i].v;
small.push((heap){e[i].to,dis[e[i].to]});
}
}
memset(vis,0,sizeof(vis));
for (i=1;i<=cnt;++i)
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};
return printf("%lld",MST()),0;
}

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. 网络基础 Windows控制台下Ftp使用简介

    Windows控制台下Ftp使用简介 by:授客 QQ:1033553122 测试环境: ftp服务器所在主机ip:172.25.75.2 ftp用户目录:F:\ftp   C:\Users\laif ...

  2. (后台)There is already 'jy.controller.jyadmin.JyDealerPackingReturnController' bean method

    项目报了一个错误,百度翻译了一下: “我已经有jy.controller.jyadmin.jydealerpackingreturncontroller豆方法公共org.springframework ...

  3. JavaScript大杂烩14 - 使用JQuery(上)

    JQuery意义 - Why? 为什么要使用JQuery,从我个人来说,就是这么几点:简化代码 + 统一行为 + 功能强大 + 搭配方便. 简化代码是从写代码的角度来说的,实现同样的功能,如果用Jav ...

  4. JS笔记(二):对象

    (一) 对象 对象是JS的基本数据类型,类似于python的字典.然而对象不仅仅是键值对的映射,除了可以保持自有的属性,JS对象还可以从一个称为原型的对象继承属性,对象的方法通常是继承的属性.(这种对 ...

  5. mybatis学习系列五--插件及类型处理器

    2 插件编写(80-81) 单个插件编写 2.1实现interceptor接口(ibatis) invocation.proceed()方法执行必须要有,否则不会无法实现拦截作用 2.2 使用@int ...

  6. EntityFramework Code-First 简易教程(九)-------一对多

    一对多(One-to-Many)关系: 下面,我们来介绍Code-First的一对多关系,比如,在一个Standard(年级)类中包含多个Student类. 如果想了解更多关于one-to-one,o ...

  7. EntityFramework Code-First 简易教程(八)-------一对一

    配置一对一(One-to-One)关系: 两个实体中,如果一个实体的一个实例与另一个实体相关,则我们就叫做一对一关系 查看如下代码: public class Student { public Stu ...

  8. C#语言————选择结构

    int[] num = new int[] {23,76,54,87,51,12 }; //冒泡排序 for (int i = 0; i < num.Length - 1; i++) { for ...

  9. Beta冲刺! Day5 - 砍柴

    Beta冲刺! Day5 - 砍柴 今日已完成 晨瑶:陪全队肝到最后一刻 昭锡:更改了主页UI 永盛:剩余的接口改动和新增 立强:文章增加缩略图预览,收藏功能第三方编辑器整合. 炜鸿:继续完成站内信功 ...

  10. GUI练习3

    将阿里山的积分卡拉斯的发生的咖啡机啊圣考虑到发送到敬爱费卢卡斯加