首页 > 其他分享 >【CF843D】Dynamic Shortest Path(Dijkstra)

【CF843D】Dynamic Shortest Path(Dijkstra)

时间:2023-05-17 17:31:55浏览次数:31  
标签:ch int register Dynamic read Dijkstra CF843D dis define


Description

一张带权有向图,q q 次操作:
1. vv 询问1 1 到vv的最短路
2. c l1 l2…lc c   l 1   l 2 … l c 将边li l i 的权值增加1 1


Solution

在权值较小时,Dijkstra是可以做到线性的。
我们开值域个队列,从小到大处理,将当前松弛得到的一个新值插入对应的队列即可。
对于这个题,我们先预处理出最短路,将边权变成−dis[v[i]]+dis[u]+w[i]−dis[v[i]]+dis[u]+w[i]即可。


Code

/************************************************
 * Au: Hany01
 * Date: Aug 22nd, 2018
 * Prob: CF843D

 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<LL, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f3f3f3f3fll)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

namespace pb_ds {

    namespace io {
        const int MaxBuff = 1 << 16;
        const int Output = 1 << 22;
        char B[MaxBuff], *S = B, *T = B;
        #define getc() ((S == T) && (T = (S = B) + fread(B, 1, MaxBuff, stdin), S == T) ? 0 : *S++)
        char Out[Output], *iter = Out;
        inline void flush() {
            fwrite(Out, 1, iter - Out, stdout);
            iter = Out;
        }
    }

    template<class Type> inline Type read() {
        using namespace io;
        register char ch; register Type ans = 0; register bool neg = 0;
        while(ch = getc(), (ch < '0' || ch > '9') && ch != '-')     ;
        ch == '-' ? neg = 1 : ans = ch - '0';
        while(ch = getc(), '0' <= ch && ch <= '9') ans = ans * 10 + ch - '0';
        return neg ? -ans : ans;
    }

    template<class Type> inline void write(register Type x, register char ch = '\n') {
        using namespace io;
        if(!x) *iter++ = '0';
        else {
            if(x < 0) *iter++ = '-', x = -x;
            static int s[100]; register int t = 0;
            while(x) s[++t] = x % 10, x /= 10;
            while(t) *iter++ = '0' + s[t--];
        }
        *iter++ = ch;
    }
}
using namespace pb_ds;

const int maxn = 1e5 + 5;

int n, m, qs, e, v[maxn], beg[maxn], nex[maxn], w[maxn], f[maxn];
LL  dis[maxn];
queue<int> q[maxn];

inline void add(int uu, int vv, int ww) { v[++ e] = vv, w[e] = ww, nex[e] = beg[uu], beg[uu] = e; }

inline void Dijkstra() {
    static priority_queue<PII, vector<PII>, greater<PII> > q;
    static int vis[maxn];
    Set(dis, 63), q.push(mp(0, 1)), dis[1] = 0;
    while (!q.empty()) {
        register int u = q.top().y; q.pop();
        if (vis[u]) continue; else vis[u] = 1;
        for (register int i = beg[u]; i; i = nex[i])
            if (chkmin(dis[v[i]], dis[u] + w[i]))
                q.push(mp(dis[v[i]], v[i]));
    }
}

int main()
{
#ifdef hany01
    freopen("cf843d.in", "r", stdin);
    freopen("cf843d.out", "w", stdout);
#endif

    static int ty, x, uu, vv, ww, lmt;

    n = read<int>(), m = read<int>(), qs = read<int>();
    For(i, 1, m) uu = read<int>(), vv = read<int>(), ww = read<int>(), add(uu, vv, ww);
    Dijkstra();

    while (qs --) {
        ty = read<int>(), x = read<int>();
        if (ty == 1) {
            if (dis[x] < INF) printf("%lld\n", dis[x]); else puts("-1");
        } else {
            For(i, 1, x) ++ w[read<int>()];
            q[0].push(1), Set(f, 63), f[1] = 0, lmt = 0;
            for (int i = 0; i <= lmt; ++ i) {
                while (!q[i].empty()) {
                    register int u = q[i].front();
                    LL t; q[i].pop();
                    if (f[u] < i) continue;
                    for (register int j = beg[u]; j; j = nex[j]) {
                        if (f[v[j]] > (t = (LL)f[u] - dis[v[j]] + dis[u] + w[j])) {
                            f[v[j]] = t;
                            if (t <= min(x, n - 1)) q[t].push(v[j]), chkmax(lmt, (int)t);
                        }
                    }
                }
            }
            For(i, 2, n) dis[i] = min(INF, dis[i] + f[i]);
        }
    }

    return 0;
}

标签:ch,int,register,Dynamic,read,Dijkstra,CF843D,dis,define
From: https://blog.51cto.com/u_16117582/6292704

相关文章

  • Dynamic Process Creation Using API [JBPM 5.1]
    [url]http://atulkotwale.blogspot.com/2012/01/dynamic-process-creation-using-api-jbpm.html[/url]HiFriends,Asyouguysallknowjbpmhascameupwithnewversion.Ithassomebeautifulfeatures.InthispostIwillshowyouhowcanwe......
  • import CSV with X++ for Dynamics 365 FO
    ///<summary>///importcolorcode///</summary>classCFSImportColorCode{    ///<summary>    ///main    ///</summary>    ///<paramname=“_args”>_args</param>   publicstaticvoidmain(Args_args)   { ......
  • BGP-capability dynamic
     setprotocolsbgp<asn>neighbor<address|interface>address-family<ipv4-unicast|ipv6-unicast>soft-reconfigurationinboundChangesinBGPpoliciesrequiretheBGPsessiontobecleared.Clearinghasalargenegativeimpactonnetwork......
  • ibatis-dynamic的用法
    去除第一个prepend="and"中的字符(这里为and),从而可以帮助你实现一些很实用的功能。具体情况如下:1.使用dynamic1.1xmlselect*fromPerson表    <dynamicprepend="where"><isNotNullproperty="name"prepend="and">......
  • DYNAMICS-AWARE UNSUPERVISED DISCOVERY OF SKILLS
    发表时间:2020(ICLR2020)文章要点:这篇文章提出了一个无监督的model-based的学习算法Dynamics-AwareDiscoveryofSkills(DADS),可以同时发现可预测的行为以及学习他们的dynamics。然后对于新任务,可以直接用zero-shotplanning的方法选择最优动作。这个文章的点就是学习skill的方式......
  • 1163 Dijkstra Sequence + 层序遍历 + 链式前向星
    PAT题目链接:https://pintia.cn/problem-sets/994805342720868352/exam/problems/1478635670373253120这题踩了太多坑,本来没什么内容,硬是断断续续查了三天的bug:第一天:循环的时候内部判断逻辑不要写在for循环里,否则本该continue的逻辑,硬生生变成了break。我真是脑袋瓜秀逗了才会......
  • const_cast,static_cast,dynamic_cast,reinterpret_cast的区别(转)
    原文:https://www.cnblogs.com/fancy-xt/p/5339177.htmlC++继承了C中的隐式和显式转换的方式。但这种转换并不是安全和严格的,加上C++本身对象模型的复杂性,C++增加了四个显示转换的关键字。(C++是强类型语言)经过编码测试,小结如下:const_cast:仅用于去掉完全同类型的const,volatile......
  • SAP动态安全库存(Dynamic Safety stock)配置及计算逻辑说明测试
    概念及计算逻辑:动态安全库存(DynamicSafetystock):它根据平均的日需求(Averagedailyrequirements)数量,来确定未来几个时期的安全库存水平(数量等于若干个平均日需求):最小库存、目标库存、最大库存。若小于最小库存,产生补货请求至目标库存;若大于最大库存,系统将提示例外信息。若同时设......
  • Deep Dynamics Models for Learning Dexterous Manipulation
    发表时间:2019(CoRL2019)文章要点:文章提出了一个onlineplanningwithdeepdynamicsmodels(PDDM)的算法来学习Dexterousmulti-fingeredhands,大概意思就是学习拟人的灵活的手指操控技巧。大概思路就是结合uncertainty-awareneuralnetworkmodels和gradient-freetrajecto......
  • 我刚才用了dynamic_cast 你给我普及一下C++ 中这几种类型转换吧
    我刚才用了dynamic_cast你给我普及一下C++中这几种类型转换吧在C++中,有几种类型转换的方式,包括:隐式转换在一些情况下,编译器会自动进行类型转换。比如将整型变量赋值给浮点型变量,编译器就会自动将整型变量转换为浮点型变量。但是在大多数情况下,使用隐式转换可能会引起一些问......