大意: n结点有向有权图, m个操作, 增加若干边的权重或询问源点为1的单源最短路.

本题一个特殊点在于每次只增加边权, 并且边权增加值很小, 询问量也很小. 我们可以用johnson的思想, 转化为差值最短路, 这样边权就在n-1以内, 可以直接暴力跑桶优化dijkstra.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<ll,int> pli; const int N = 1e5+10;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m, q;
struct _ {int to,w;} E[N];
vector<int> g[N];
int vis[N], d2[N];
ll d[N];
priority_queue<pli,vector<pli>, greater<pli> > Q;
queue<int> q2[N]; void Dij() {
memset(d,0x3f,sizeof d);
Q.push(pli(d[1]=0,1));
while (Q.size()) {
int u = Q.top().y; Q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (auto &&id:g[u]) {
auto &e = E[id];
ll dd = d[u]+e.w;
if (dd<d[e.to]) Q.push(pli(d[e.to]=dd,e.to));
}
}
}
void Dij2(int k) {
REP(i,1,k) {int t; scanf("%d", &t), ++E[t].w;}
memset(d2,0x3f,sizeof d2);
q2[0].push(1), d2[1] = 0;
int mx = 0;
REP(i,0,mx) while (q2[i].size()) {
int u = q2[i].front(); q2[i].pop();
if (d2[u]<i) continue;
for (auto &&id:g[u]) {
auto &e = E[id];
int dd = d2[u]+(e.w+d[u]-d[e.to]);
if (dd<d2[e.to]) {
d2[e.to] = dd;
if (dd<=min(n-1,k)) {
q2[dd].push(e.to);
mx = max(mx, dd);
}
}
}
}
REP(i,1,n) d[i]=min(INF,d[i]+d2[i]);
} int main() {
scanf("%d%d%d", &n, &m, &q);
REP(i,1,m) {
int u;
scanf("%d%d%d", &u, &E[i].to, &E[i].w);
g[u].pb(i);
}
Dij();
while (q--) {
int op, x;
scanf("%d%d", &op, &x);
if (op==1) printf("%lld\n", d[x]<INF?d[x]:-1);
else Dij2(x);
}
}

Dynamic Shortest Path CodeForces - 843D (动态最短路)的更多相关文章

  1. [CF843D]Dynamic Shortest Path

    [CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...

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

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

  3. Shortest Path Codeforces - 59E || 洛谷P1811 最短路_NOI导刊2011提高(01)

    https://codeforces.com/contest/59/problem/E 原来以为不会..看了题解发现貌似自己其实是会的? 就是拆点最短路..拆成n^2个点,每个点用(i,j)表示,表示 ...

  4. HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C.还有M ...

  5. HDU-4725 The Shortest Path in Nya Graph 最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...

  6. CF843D Dynamic Shortest Path spfa+剪枝

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

  7. HDU 4725 The Shortest Path in Nya Graph (最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

    人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...

  9. Codeforces Beta Round #3 A. Shortest path of the king 水题

    A. Shortest path of the king 题目连接: http://www.codeforces.com/contest/3/problem/A Description The kin ...

随机推荐

  1. 【Java----创建多级文件夹】

    File类中的mkdir()和mkdirs(): mkdir():只能创建一层目录. mkdirs():可以创建多层目录 代码:path可以是带文件名称的全路径 //路径 String path = ...

  2. centos7重新调整分区大小

    As others have pointed out, XFS filesystem cannot be shrunk. So your best bet is to backup /home, re ...

  3. dfs序七个经典问题

    update-2018.07.23: 原文问题五思路描述有误,已更正. 参考自:<数据结构漫谈>-许昊然 dfs序是树在dfs先序遍历时的序列,将树形结构转化成序列问题处理. dfs有一个 ...

  4. JZ2440存储管理器--SDRAM

     为了cpu访问外部设备,ARM提供一个存储管理器部件,提供访问外部设备的所需的信号(对SDRAM.网卡.nor等设备进行初始化,以便存储器管理器配合CPU进行与外设数据通讯).   CPU通常读写一 ...

  5. Docker5之Deploy your app

    Make sure you have published the friendlyhello image you created by pushing it to a registry. We’ll ...

  6. Use Memory Layout from Target Dialog Scatter File

    参考 MDK-ARM Linker Scatter File的用法(转载) keil报错 Rebuild target 'Target 1' assembling test1.s... linking ...

  7. module.exports 与 exports

    module.exports 与 exports 注意:1 对于要导出的属性,可以简单直接挂到 exports 对象上2 对于类,为了直接使导出的内容作为类的构造器可以让调用者使用 new 操作符创建 ...

  8. 5、web站点架构模式简介及Nginx

    LB Cluster: 提升系统容量的方式: scale up:向上扩展 scale out:向外扩展 LVS工作在内核中,本身的数量不受套接字数量限制,利用LVS做调度器,优化得当的话,并发数量可以 ...

  9. 1:httpd-2.2基础

    在配置httpd主配置文件时,应该先记得备份一下: #cd /etc/httpd/conf/ #cp httpd.conf{,.bak} #vim /etc/httpd/conf/httpd.conf ...

  10. 新加坡金融科技节 | 蚂蚁金服CTO程立:面向全球开放,与合作伙伴共赢

    小蚂蚁说: 11月13日,在新加坡金融科技节上,蚂蚁金服CTO程立分别从TechFin.BASIC战略.SOFAStack全栈分布式体系以及全面开放等方面讲述蚂蚁金融科技. TechFin是一种“倒立 ...