博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces 545E - Paths and Trees(最短路树)
阅读量:4339 次
发布时间:2019-06-07

本文共 1116 字,大约阅读时间需要 3 分钟。

题目链接

【题意】

给定一个n个结点m条边的无向图,并给出源点s,让你找出图中权值最小的最短路树,并输出这个权值

【思路】

对dijkstra算法稍作修改即可,在松弛操作的时候,在保留最短路的前提下,保正上一条边的权值是最小的,类似一种贪心的思想,最短路径确定的话,如果上一条边的权值最小,那么其余部分肯定会有更多的公共路径可以利用,最短路树的权值也就更小了。

#include
using namespace std;typedef long long ll;typedef ll type;const ll inf=1e18;const int maxn=300050;struct Edge{ int from,to,id; type dist; Edge(int f,int t,type d,int i):from(f),to(t),dist(d),id(i){}};struct HeapNode{ type d; int u; HeapNode(type dd,int uu):d(dd),u(uu){} bool operator<(const HeapNode& rhs)const{ return d>rhs.d; }};struct Dijkstra{ int n,m; vector
edges; vector
g[maxn]; bool done[maxn]; type d[maxn]; int p[maxn]; void init(int n){ this->n=n; for(int i=0;i
que; for(int i=0;i
d[u]+e.dist){ d[e.to]=d[u]+e.dist; p[e.to]=g[u][i]; que.push(HeapNode(d[e.to],e.to)); } else if(d[e.to]==d[u]+e.dist && e.dist
ans; for(int i=0;i

转载于:https://www.cnblogs.com/wafish/p/10465315.html

你可能感兴趣的文章
GA代码中的细节
查看>>
leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法
查看>>
join用法
查看>>
log4cxx入门第一篇--一个小例子
查看>>
jq中的事件委托:closest,parent,parents,delegate
查看>>
ArcGIS服务发布过程以及Unable to connect to publishing tools service 解决方案
查看>>
【转】数据挖掘从入门到进阶
查看>>
持续集成--Jenkins--2
查看>>
关于STM32F405的GPIO中断问题
查看>>
(FFOS Gecko & Gaia) OTA - 结束篇
查看>>
Linux服务器时间相关命令记录
查看>>
SVN配置使用
查看>>
.net基础复习之一
查看>>
网页的缩放,适配以及移动的适配!
查看>>
[SCM]源码管理 - Perforce命令行
查看>>
简单的csh实例
查看>>
jenkins2 pipeline插件的10个最佳实践
查看>>
yii2关闭(开启)csrf的验证
查看>>
mysql 隔离性与隔离级别
查看>>
css选择器
查看>>