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 显然是欧拉回路问题,度数为奇数的点之间连边,跑欧拉回路就可以 ...
随机推荐
- js replace替换 忽略大小写问题
实现就是控制台的内容“abc”,但是后台返回的是“ABC”,这个时候在前台遍历,需要将后台返回的在控制台标红. 当然控制台可以是 abc Abc等大小写混合,以下代码都可替换. var flagnew ...
- Scrapy爬虫入门
1.安装Scrapy 打开Anaconda Prompt,执行:pip install Scrapy执行安装! 注意:要是安装过程中抛出: error: Microsoft Visual C++ 14 ...
- TensorFlow实现Softmax回归(模型存储与加载)
# -*- coding: utf-8 -*- """ Created on Thu Oct 18 18:02:26 2018 @author: zhen "& ...
- 【HANA系列】SAP HANA XS的JavaScript安全事项
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA XS使用Jav ...
- SQL去除空格、截取数据的方法:trim、substring
1.如device表中的identity字段正常的字段长度是32位,但是某些不正常的数据,后面多出空格,需要去掉后面的空格,可执行以下命令: ; ; 2.使用substring函数截取某字段的的其中一 ...
- Django之model模块创建表完整过程
Django中,与数据库相关的模块是model模块,它提供了一种简单易操作的API方式与数据库交互,它是通过ORM映射的方式来操作数据库,一个类对应数据库一张表,一个类属性,对应该表的一个字段,一个实 ...
- django —— MVT模型
转载----
- 启动Myeclipse报错“Failed to create the Java Virtual Machine”的解决办法
我安装的是Myeclipse 10.7.1.装上好久没用,今天启动突然报错:Failed to create the Java Virtual Machine. 检查Myeclipse安装好使用时好的 ...
- Hibernate Tools生成注释
原文:http://www.blogjava.net/pauliz/archive/2009/11/13/302162.html 有同学需要修改后的Hibernate Tools整个tool我就不上传 ...
- flex布局下el-table横向滚动条失效
如下图,是一种常见的页面结构,我们可以有很多方法实现,inline-block,float,flex等等 但是,最近项目中遇到一个怪事,左边是侧边栏导航,右边是一个数据展示table,el-table ...