[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 ...
随机推荐
- solaris如何启动ssh服务
先查看一下ssh服务状态:# svcs或# svcs | grep sshonline Aug_07 svc:/network/ssh:default 如需要关闭ssh服务(关闭完可以 svcs | ...
- css控制文字换行
1.word-wrap 设置为break-word时,文本中的长单词或url可以换行 <p style="width:100px;word-wrap:break-word;border ...
- ie8下trim失效
1.ie8下使用trim失效 trim可以除去字符串两侧的空白字符,但ie8并不支持 2.解决方案 String.prototype.trim = function () { return this ...
- python进行机器学习(二)之特征选择
毫无疑问,解决一个问题最重要的是恰当选取特征.甚至创造特征的能力,这叫做特征选取和特征工程.对于特征选取工作,我个人认为分为两个方面: 1)利用python中已有的算法进行特征选取. 2)人为分析各个 ...
- Python模块学习 - Fileinput
Fileinput模块 fileinput是python提供的标准库,使用fileinput模块可以依次读取命令行参数中给出的多个文件.也就是说,它可以遍历 sys.argv[1:],并按行读取列表中 ...
- 移动端测试===Android内存泄露和GC机制(转)
本文转自:https://www.testwo.com/article/1153 1.前言 Hello,小伙伴们,相信大家在项目测试中都遇到过内存泄露问题,小编也着实爬过很多坑.比如小编所测项目,更换 ...
- UNIX v6
UNIX v6 http://download.csdn.net/download/u013896535/9106775 https://github.com/chromium/mini_chromi ...
- MSCL超级工具类(C#),开发人员必备,开发利器
MSCL超强工具类库 是基于C#开发的超强工具类集合,涵盖了日常B/S或C/S开发的诸多方面,包含上百个常用封装类(数据库操作类全面支持Mysql.Access.Oracle.Sqlserver.Sq ...
- sicily 1012. Stacking Cylinders & 1206. Stacking Cylinders
Time Limit: 1sec Memory Limit:32MB Description Cylinders (e.g. oil drums) (of radius 1 foot) are ...
- Author name disambiguation using a graph model with node splitting and merging based on bibliographic information
Author name disambiguation using a graph model with node splitting and merging based on bibliographi ...