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

我们首先可以发现这棵树必定满足从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. python 定时修改数据库

    当需要定时修改数据库时,一般我们都选择起一个定时进程去改库.如果将这种定时任务写入业务中,写成一个接口呢,定时进程显得有些不太合适?如果需要定时修改100次数据库,常规做法会启动100个进程,虽然这种 ...

  2. [20171107]dbms_shared_pool.pin.txt

    [20171107]dbms_shared_pool.pin.txt --//昨天与别人聊天提到,如果dbms_shared_pool.pin对象,可以改变对应的chunk的类型.我自己也不确定,做一 ...

  3. PowerDesin把name复制到Comment,把Comment复制到Name

    PowerDesin把name复制到Comment,把Comment复制到Name的方法: PowerDesigner->Tools->Execute Commands->Edit/ ...

  4. table表格(笔记)

    <table class="table table-hover2 pick_carTable"> <thead> <tr class="bl ...

  5. 解决wordpress上传文件出现http错误问题

    解决wordpress上传文件出现http错误问题 问题现象 今天上传约1.4m大小的gif文件到wordpress的媒体库时失败,提示http错误. 原因 由于之前一直上传图片都是可以的,所以推测最 ...

  6. SQL易错总结1

    SQL易错总结1 进阶 select语句.select * 查询所有不规范,写出要查的属性.distinct慎用,性能消耗很大 like 模糊查询 ,空值判断是 is null 单行函数:lower( ...

  7. 26_ArrayList_HashSet的比较及Hashcode分析

    实体类: package com.itcast.day1; public class ReflectPoint { private int x; public int y; public Reflec ...

  8. Instruments模板介绍(更新中...)

    第一章.Instruments(仪器)快速入门 Instruments可以用来收集关于一个或者多个系统进程的性能和行为的数据,并跟踪睡着时间产生的数据.它提供了好几个分析模板: Blank 任何类型都 ...

  9. Pandas 库中excel的读写方法介绍

    首选:https://blog.csdn.net/u014597198/article/details/83104653 其次:https://blog.csdn.net/qq_34377830/ar ...

  10. SQL注入的绕过

    一.常用符号的绕过 1.空格 1 空格代替:+ %20 %09 %0a %0b %0c %0d %a0 %00 /**/ /*!*/ 2 括号绕过:常用于基于时间延迟的盲注,例如构造语句: ?id=1 ...