大意: 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. 倍数|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    思路:从l枚举到r肯定超时,这时我们要转变思路!题目让我们求一个区间内的d的倍数,只需要求出r/d - l/d就是区间内d倍数的个数. 代码: #include <iostream> us ...

  2. samtools can not find libbz2.so.1.0

    Error: samtoolssamtools: error while loading shared libraries: libbz2.so.1.0: cannot open shared obj ...

  3. c# Database类的使用

    参考资料http://ansonlh.iteye.com/blog/1689009 搜索:c# DatabaseFactory类

  4. 【sql server】索引详解

    索引可以理解为一种特殊的目录结构. sql server提供两种索引形式: 聚集索引和非聚集索引. 怎么理解这两种形式. 拿我们常用的字典举例来说, 一个字典好比数据库中的一个表.那么当我们想从字典中 ...

  5. kubernetes 实战4_命令_Configure Pods and Containers

    Configure Service Accounts for Pods A service account provides an identity for processes that run in ...

  6. C++ getline判断空行

    C++中getline用于逐行读取字符, 格式 getline(字符串,字符数) 将该行“字符数”个的字符读入“字符串” 如何判断所读是否为空行呢? strlen(字符串)==0就是空行

  7. CentOS7 使用firewalld打开关闭防火墙以及端口

    1.firewalld的基本使用 启动 systemctl start firewalld 关闭 systemctl stop firewalld 查看状态 systemctl status fire ...

  8. django表单的Widgets

    不要将Widget与表单的fields字段混淆.表单字段负责验证输入并直接在模板中使用.而Widget负责渲染网页上HTML表单的输入元素和提取提交的原始数据.widget是字段的一个内在属性,用于定 ...

  9. confirm("确定要删除吗?") _详解

    具体代码如下: html代码: <div class="deletes">删除</div> js代码: <script type="text ...

  10. typeScript入门基础 (2): 数据 类型, 函数定义

    1.   number , boolean, string, null, undefind  枚举, any, array数组, 元祖, 2...枚举,需要的注意点,  下一个图,就是为什么yello ...