[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的更多相关文章

  1. CF843D Dynamic Shortest Path spfa+剪枝

    考试的T3,拿暴力+剪枝卡过去了. 没想到 CF 上也能过 ~ code: #include <bits/stdc++.h> #define N 100004 #define LL lon ...

  2. Dynamic Shortest Path CodeForces - 843D (动态最短路)

    大意: n结点有向有权图, m个操作, 增加若干边的权重或询问源点为1的单源最短路. 本题一个特殊点在于每次只增加边权, 并且边权增加值很小, 询问量也很小. 我们可以用johnson的思想, 转化为 ...

  3. cf 843 D Dynamic Shortest Path [最短路+bfs]

    题面: 传送门 思路: 真·动态最短路 但是因为每次只加1 所以可以每一次修改操作的时候使用距离分层的bfs,在O(n)的时间内解决修改 这里要用到一个小技巧: 把每条边(u,v)的边权表示为dis[ ...

  4. 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 ...

  5. [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 ...

  6. 干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码

    00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cos ...

  7. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  9. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

随机推荐

  1. js_setCookie,getCookie和checkcookie函数

    随便说说: cookie和sessionStrong,localStrong在web应用中都有一种存储的功能,也就是说可以把一些数据记录在浏览器.cookie和后两者的主要区别 是cookie是和后端 ...

  2. Python模块学习 - pyinotify

    pyinotify介绍 pyinotify模块用来监测文件系统的变化,依赖于Linux内核的inotify功能,inotify是一个事件驱动的通知器,其通知接口从内核空间到用户空间通过三个系统调用.p ...

  3. Java基础 变量和数据类型及相关操作

    Java基本语法: 1):Java语言严格区分大小写,好比main和Main是完全不同的概念. 2):一个Java源文件里可以定义多个Java类,但其中最多只能有一个类被定义成public类.若源文件 ...

  4. python基础===进程,线程,协程的区别(转)

    本文转自:http://blog.csdn.net/hairetz/article/details/16119911 进程拥有自己独立的堆和栈,既不共享堆,亦不共享栈,进程由操作系统调度. 线程拥有自 ...

  5. CentOS在ssh下远程重装系统

    CentOS在ssh下远程重装系统 http://www.zxsdw.com/index.php/archives/913/ 国外VPS服务器一般都有控制面板,有很多种系统可自行安装,但国内有些IDC ...

  6. Centos7 IP地址配置方法

    1.编辑 ifcfg-eth0 文件 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改如下内容 BOOTPROTO=”static” #dhcp ...

  7. MACBOOK 总是断网怎么办

    MACBOOK 连接 wifi 老是断网.焦躁不安 看图,二个方法,第一就搞定,

  8. IE7下面iframe滚动条无法用鼠标轮滚 其他浏览器可以

    1.让 IFRAME 隐藏滚动条,通常的做法就是在嵌入  IFRAME 的页面的 CSS 中指定以下规则:   html, body {overflow: hidden}   2.如果只是想隐藏横向滚 ...

  9. java中的三元运算符

    格式: 关系表达式 ? 表达式1:表达式2 public class OperatorDemo { public static void main(String[] args){ int a = 10 ...

  10. 22:django 配置详解

    django配置文件包含了你的django安装的所有配置信息,本节为大家详细讲解django的配置 基本知识 一个配置文件只是一个包含模块级别变量的的python模块,所有的配置变量都是大写的,哈哈哈 ...