【CF843D】Dynamic Shortest Path(Dijkstra)

6 篇文章 0 订阅
3 篇文章 0 订阅

Description

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


Solution

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


Code

/************************************************
 * Au: Hany01
 * Date: Aug 22nd, 2018
 * Prob: CF843D
 * Email: hany01dxx@gmail.com & hany01@foxmail.com
 * 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值