51Nod 1443 路径和树
还是一道很简单的基础题,就是一个最短路径树的类型题目
我们首先可以发现这棵树必定满足从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 路径和树的更多相关文章
- 51nod 1443 路径和树(最短路)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1443 1443 路径和树 题目来源: CodeForces ...
- 51nod 1443 路径和树(最短路树)
题目链接:路径和树 题意:给定无向带权连通图,求从u开始边权和最小的最短路树,输出最小边权和. 题解:构造出最短路树,把存留下来的边权全部加起来.(跑dijkstra的时候松弛加上$ < $变成 ...
- 51Nod 1443 路径和树 —— dijkstra
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1443 首先要得到一个最短路树: 注意边权和最小,因为在最短路中,每 ...
- 51nod 1443 路径和树——最短路生成树
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1443 不只是做一遍最短路.还要在可以选的边里选最短的才行. 以为是 ...
- [BZOJ1576] [BZOJ3694] [USACO2009Jan] 安全路径(最短路径+树链剖分)
[BZOJ1576] [BZOJ3694] [USACO2009Jan] 安全路径(最短路径+树链剖分) 题面 BZOJ1576和BZOJ3694几乎一模一样,只是BZOJ3694直接给出了最短路树 ...
- 51nod 1681 公共祖先 | 树状数组
51nod 1681 公共祖先 有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另一个未知的平行宇宙,这n人的祖辈关系仍然是树形结构,但他们相互之间的关系却完 ...
- 51Nod 1737 配对(树的重心)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1737 题意: 思路: 树的重心. 树的重心就是其所以子树的最大的子树结点 ...
- 51nod 1272 思维/线段树
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 1272 最大距离 题目来源: Codility 基准时间限制:1 ...
- 51Nod 1967 路径定向 —— 欧拉回路
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1967 显然是欧拉回路问题,度数为奇数的点之间连边,跑欧拉回路就可以 ...
随机推荐
- LyX使用中的一些问题
编译开始产生的检查错误 试用LyX2.3,在2.15中能编译通过的文档,竟然提示错误 The user preamble of your document contains glyphs that a ...
- (后台)java 读取excel 文件 Unable to recognize OLE stream 错误
原因:不支出读取 excel 2007 文件(*.xlsx).只支持 excel 2003 (*.xls). 光修改文件后缀不行,需要文件另存(或者导出)为 .xls Excel 1997-2004 ...
- 四元数与欧拉角(RPY角)的相互转换
RPY角与Z-Y-X欧拉角 描述坐标系{B}相对于参考坐标系{A}的姿态有两种方式.第一种是绕固定(参考)坐标轴旋转:假设开始两个坐标系重合,先将{B}绕{A}的X轴旋转$\gamma$,然后绕{A} ...
- [20171106]配置客户端连接注意.txt
[20171106]配置客户端连接注意.txt --//在配置客户端连接时一般建议使用Net Manager工具,windows下调用执行Net Manager.--//linux下执行 netmgr ...
- [20170824]11G备库启用DRCP连接.txt
[20170824]11G备库启用DRCP连接.txt --//参考链接:http://blog.itpub.net/267265/viewspace-2099397/blogs.oracle.com ...
- sql server递归
with cte as ( select belongsAgent from [QPProxyDB].[dbo].[BS_ProxyInfo] where ProxyID = @ProxyID uni ...
- 使用 PsPing & PaPing 进行 TCP 端口连通性测试
PsPing & PaPing 介绍 通常,我们测试数据包能否通过 IP 协议到达特定主机时,都习惯使用 ping 命令.工作时 ping 向目标主机发送一个 IMCP Echo 请求的数据包 ...
- trap命令的实战用法
trap命令: trap命令是专用于捕捉信号的.比如像ctrl+c发送给终端的中断信号等等.在捕捉到信号之后,可以进行一系列的操作. 用法:trap 'COMMAND' INT COMMAND表示t ...
- C++基础算法学习——汉洛塔问题
汉诺塔问题古代有一个梵塔,塔内有三个座A.B.C,A座上有64个盘子,盘子大小不等,大的在下,小的在上(如图).有一个和尚想把这64个盘子从A座移到C座,但每次只能允许移动一个盘子,并且在移动过程中, ...
- Lua与C交互之基础操作(1)
@(语言) Lua是一个嵌入式的语言,可以Lua可以作为程序库用来扩展应用的功能,也可以注册有其他语言实现的函数,这些函数可能由C语言(或其他语言)实现,可以增加一些不容易由Lua实现的功能.这就是L ...