最短路的最大权值

博客以优先队列优化的迪杰斯特拉算法为例,介绍了最短路更新方式,更新后 dis[x] 表示起点到 x 最短路上的最大边权值。

以优先队列优化的迪杰斯特拉算法为例
最短路中我们是这么更新的

int to = e[i].to;
if(dis[to] > dis[x] + e[i].w){
	dis[to] = dis[x] + e[i].w;
	q.push({dis[to], to});
}

现在我们只需要改成即可

int to = e[i].to;
ll z = max(e[i].w, dis[x]);
if(dis[to] > z){
	dis[to] = z;
	q.push({dis[to], to};
}

这时候 dis[x]dis[x]dis[x] 表示起点到 xxx 最短路上的最大边权值

### 关于拆点短路算法 拆点短路是一种特殊处理方式,在某些特定场景下可以有效解决问题。通常情况下,当遇到带有容量限制或者时间窗口约束等问题时,可以通过拆点的方法将其转化为标准的短路径问题。 #### 拆点的概念及其作用 在一个图中,如果某个顶点存在额外属性(比如通过该点的最大次数),那么就可以考虑对该点进行拆分操作。具体来说就是把原来的单个节点分裂成多个子节点,并设置合适的边权连接这些新产生的节点之间以及与其他原有节点间的联系[^1]。 对于含有流量限制条件下的短路径求解而言,利用网络流模型配合拆点技术能够很好地满足需求。例如在网络传输过程中可能存在带宽瓶颈的情况;此时可以把具有限流特性的中间结点分解为入度端口出度端口两部分,再依据实际业务逻辑调整二者间连通性及权重参数从而构建起适用于此类情形的新图结构以便后续计算优解方案[^2]。 #### 实现方法概述 为了实现上述思路中的转换过程,一般会遵循如下原则: - 对每一个需要被分割的目标位置创建若干副本; - 根据题目给定的信息设定好各个复制品之间的转移代价; - 将所有新建出来的实体按照一定规律重新组合起来形成一张新的拓扑关系网; - 应用常规意义上的寻径函数如 Dijkstra 或者 SPFA 来获取终的结果集。 ```python def split_node(graph, node_id, capacity): """ Split a single node into two nodes with an edge between them. :param graph: Original adjacency list representation of the graph :type graph: dict[int, list[tuple(int, int)]] :param node_id: ID of the node to be split :type node_id: int :param capacity: Capacity limit on the new edge connecting the split parts :type capacity: int :return: Modified graph after splitting specified node and adding necessary edges :rtype: dict[int, list[tuple(int, int)]] """ # Create copies for incoming (in_) and outgoing (_out) connections from/to original point in_copy = f"{node_id}_in" out_copy = f"{node_id}_out" # Add these newly created points back into our working space along with their respective weights/capacities updated_graph = {**graph} updated_graph[in_copy] = [] updated_graph[out_copy] = [] # Transfer all pre-existing links pointing towards this location over to its 'input' counterpart instead, # while also establishing direct connection between input/output versions at fixed cost defined by user for neighbor, weight in graph[node_id]: updated_graph[in_copy].append((neighbor, weight)) updated_graph[in_copy].append((out_copy, capacity)) del updated_graph[node_id] return updated_graph ``` 此代码片段展示了如何基于字典形式表示的一张无向有权重图来进行指定编号节点的拆分工作。这里假设输入数据已经过适当预处理并符合预期格式要求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值