[CF843D]Dynamic Shortest Path
[CF843D]Dynamic Shortest Path
题目大意:
给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边。共\(q(q\le2000)\)次操作,操作包含以下两种:
- \(1\:v\)——查询从\(1\)到\(v\)的最短路。
- \(2\:c\:l_1\:l_2\:\ldots\:l_c\)——将边\(l_1,l_2,\ldots,l_c\)增加\(1\)的权值。
思路:
首先使用Dijkstra算法求出原图的单源最短路径\(dis[i]\)。对于所有的操作\(2\),考虑增加边权后对答案的影响。不难发现每次修改边权后\(dis[i]\)都会增加一定量或保持不变。不妨将每次每个点的增加量记作\(add[i]\),考虑增加边权后计算\(add[i]\)的值。
类比Dijkstra算法的“松弛”操作,对于一个结点\(x\),若\(add[x]\ne0\),我们可以用\(x\)来松弛别的结点。枚举\(x\)的下一个结点\(y\),若此时用\(x\)作为最短路中的上一任结点,则最短路长度需要增加\(dis[x]+w(x,y)+add[x]-dis[y]\)。而\(add[y]\)则需要对所有这样的值取\(\min\)。这样完成所有的松弛操作后,\(dis'[i]=dis[i]+add[i]\)。而这可以用BFS实现,其中当\(add[i]>c\)时则没有“松弛”的必要,可以进行剪枝。
配对堆优化Dijkstra复杂度\(\mathcal O(n\log n+m)\),单次BFS更新最短路\(\mathcal O(q(n+m))\),总时间复杂度\(\mathcal O(n\log n+m+q(n+m))\)。
细节:
注意边权可能为\(0\),因此Dijkstra中被松弛的结点可能会跑到堆顶,不能松弛完再删除堆顶元素。本题时间限制较紧,实现时注意优化常数。
源代码:
#include<queue>
#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
#include<functional>
#include<forward_list>
#include<ext/pb_ds/priority_queue.hpp>
using int64=long long;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
constexpr int N=1e5+1;
int n,w[N],add[N];
int64 dis[N];
using Edge=std::pair<int,int>;
std::forward_list<Edge> e[N];
using Vertex=std::pair<int64,int>;
__gnu_pbds::priority_queue<Vertex,std::greater<Vertex>> q;
__gnu_pbds::priority_queue<Vertex,std::greater<Vertex>>::point_iterator p[N];
inline void dijkstra() {
for(register int i=1;i<=n;i++) {
p[i]=q.push({dis[i]=i==1?0:LLONG_MAX,i});
}
while(!q.empty()&&q.top().first!=LLONG_MAX) {
const int x=q.top().second;
q.pop();
for(register auto &j:e[x]) {
const int &y=j.first,&w=::w[j.second];
if(dis[x]+w<dis[y]) {
q.modify(p[y],{dis[y]=dis[x]+w,y});
}
}
}
q.clear();
}
std::queue<int> v[N];
int main() {
n=getint();
const int m=getint(),q=getint();
for(register int i=1;i<=m;i++) {
const int u=getint(),v=getint();
w[i]=getint();
e[u].emplace_front(std::make_pair(v,i));
}
dijkstra();
for(register int i=1;i<=n;i++) {
if(dis[i]==LLONG_MAX) dis[i]=-1;
}
for(register int i=0;i<q;i++) {
if(getint()==1) {
printf("%lld\n",dis[getint()]);
} else {
const int c=getint();
for(register int i=0;i<c;i++) w[getint()]++;
std::fill(&add[1],&add[n]+1,c+1);
v[add[1]=0].emplace(1);
for(register int i=0;i<=c;i++) {
for(;!v[i].empty();v[i].pop()) {
const int &x=v[i].front();
if(add[x]!=i) continue;
for(register auto &j:e[x]) {
const int &y=j.first,&w=::w[j.second];
const int64 d=dis[x]+w+add[x]-dis[y];
if(d<add[y]) v[add[y]=d].emplace(y);
}
}
}
for(register int i=1;i<=n;i++) {
if(add[i]!=c+1) dis[i]+=add[i];
}
}
}
return 0;
}
[CF843D]Dynamic Shortest Path的更多相关文章
- CF843D Dynamic Shortest Path spfa+剪枝
考试的T3,拿暴力+剪枝卡过去了. 没想到 CF 上也能过 ~ code: #include <bits/stdc++.h> #define N 100004 #define LL lon ...
- Dynamic Shortest Path CodeForces - 843D (动态最短路)
大意: n结点有向有权图, m个操作, 增加若干边的权重或询问源点为1的单源最短路. 本题一个特殊点在于每次只增加边权, 并且边权增加值很小, 询问量也很小. 我们可以用johnson的思想, 转化为 ...
- cf 843 D Dynamic Shortest Path [最短路+bfs]
题面: 传送门 思路: 真·动态最短路 但是因为每次只加1 所以可以每一次修改操作的时候使用距离分层的bfs,在O(n)的时间内解决修改 这里要用到一个小技巧: 把每条边(u,v)的边权表示为dis[ ...
- Method for finding shortest path to destination in traffic network using Dijkstra algorithm or Floyd-warshall algorithm
A method is presented for finding a shortest path from a starting place to a destination place in a ...
- [LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...
- 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码
00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
随机推荐
- 阅读关于DuReader:百度大规模的中文机器阅读理解数据集
很久之前就得到了百度机器阅读理解关于数据集的这篇文章,今天才进行总结!.... 论文地址:https://arxiv.org/abs/1711.05073 自然语言处理是人工智能皇冠上的明珠,而机器阅 ...
- 【转】debian下的update-rc.d的使用
在Linux系统下,一个Services的启动.停止以及重启通常是通过/etc/init.d目录下的脚本来控制的.然而,在启动或改变运行级别时, 是在/etc/rcX.d中来搜索脚本.其中X是运行级别 ...
- 在linux内核中获得比jiffies精度更高的时间值【转】
转自:http://blog.chinaunix.net/uid-20672257-id-2831219.html 内核一般通过jiffies值来获取当前时间.尽管该数值表示的是自上次系统启动到当前的 ...
- laravel 中的入口文件报错
1.此次是由于加载的配置文件的编码错误导致的.例:conf.php
- CF868F Yet Another Minimization Problem
题目描述: 给定一个序列,要把它分成k个子序列.每个子序列的费用是其中相同元素的对数.求所有子序列的费用之和的最小值. 输入格式:第一行输入n(序列长度)和k(需分子序列段数).下一行有n个数,序列的 ...
- linux命令(10):ps命令
1.查看mysql进程数: ps -ef | grep "mysql" | grep -v "grep" | wc –l 2.监控CPU状态:ps –au 3. ...
- hihocoder 1135 : Magic Box
#1135 : Magic Box 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 The circus clown Sunny has a magic box. Whe ...
- [你必须知道的.NET]第二十八回:说说Name这回事儿
发布日期:2009.3.18 作者:Anytao © 2009 Anytao.com ,原创作品,转贴请注明作者和出处. 1 缘起 老赵在谈表达式树的缓存(2):由表达式树生成字符串中提到,在描述Ty ...
- .net core 2.0学习记录(三):内置IOC与DI的使用
本篇的话介绍下IOC和ID的含义以及如何使用.Net Core中的DI. 一.我是这么理解IOC和DI的: IOC:没有用IOC之前是直接new实例来赋值,使用IOC之后是通过在运行的时候根据配置来实 ...
- windows安装elasticsearch和elasticsearch-head插件
1.去官网下载最新软件 选择zip包,https://www.elastic.co/downloads/elasticsearch 2.下载node 必须 > 6.0已上 3.解压elastic ...