Dynamic Shortest Path CodeForces - 843D (动态最短路)
大意: 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 (动态最短路)的更多相关文章
- [CF843D]Dynamic Shortest Path
[CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...
- cf 843 D Dynamic Shortest Path [最短路+bfs]
题面: 传送门 思路: 真·动态最短路 但是因为每次只加1 所以可以每一次修改操作的时候使用距离分层的bfs,在O(n)的时间内解决修改 这里要用到一个小技巧: 把每条边(u,v)的边权表示为dis[ ...
- Shortest Path Codeforces - 59E || 洛谷P1811 最短路_NOI导刊2011提高(01)
https://codeforces.com/contest/59/problem/E 原来以为不会..看了题解发现貌似自己其实是会的? 就是拆点最短路..拆成n^2个点,每个点用(i,j)表示,表示 ...
- 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 ...
- HDU-4725 The Shortest Path in Nya Graph 最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...
- CF843D Dynamic Shortest Path spfa+剪枝
考试的T3,拿暴力+剪枝卡过去了. 没想到 CF 上也能过 ~ code: #include <bits/stdc++.h> #define N 100004 #define LL lon ...
- 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 ...
- ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]
人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...
- 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 ...
随机推荐
- OAuth2.0原理与实现
弄懂了原理流程,才可以搭建出来.更重要的是,可以根据原理流程自定义搭建,甚至可以完全自己实现一套,最后运行效果和原理和这个对得上就成功了,不要总期待标准答案! 首先参考两篇博客: 阮一峰的博客以及张开 ...
- 【无法使用yum安装软件】使用yum命令安装软件提示No package numactl.x86_64 available.
在安装mysql时需要安装numactl.x86_64 使用yum -y install numactl.x86_64时报错 [root@sdp6 mysql]# yum -y install num ...
- 论文笔记:Diffusion-Convolutional Neural Networks (传播-卷积神经网络)
Diffusion-Convolutional Neural Networks (传播-卷积神经网络)2018-04-09 21:59:02 1. Abstract: 我们提出传播-卷积神经网络(DC ...
- SalGAN: Visual saliency prediction with generative adversarial networks
SalGAN: Visual saliency prediction with generative adversarial networks 2017-03-17 摘要:本文引入了对抗网络的对抗训练 ...
- shiro中authc和user的权限区别
前者(authc)是认证过,后者(user)是登录过,如果开启了rememberMe功能的话,后者(user)也是可以通过的,而前者(authc)通过不了.故我们用authc来校验一些关键操作,比如购 ...
- ToString()格式和用法大全,C#实现保留两位小数的方法
C,货币,2.5.ToString("C"),¥2.50.D,十进制数,25.ToString("D5"),00025.E,科学型,25000.ToString ...
- 使用Python制作第一个爬虫程序
用到的开发环境 IDE:pycharm python version :2.7 掌握的知识: Pycharm 还能更改Python的版本 代码如下:(重点就是 正则表达式的学习) # !/u ...
- Latex: 减少图与文字之间的空白间隙
参考: Remove space after figure and before text Latex: 减少图与文字之间的空白间隙 论文中图与文字之间的空白间隙过大,导致排版不大美观.解决方法是在\ ...
- IntelliJ IDEA java selenium
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...
- Pandas中关于accessor的骚操作
来自:Python那些事 pandas中accessor功能很强大,可以将它理解为一种属性接口,通过它获得额外的方法. 下面用代码和实例理解一下: import pandas as pd pd.Ser ...