[Johnson + barrel Maintenance Dij] Codeforces #843D. Dynamic Shortest path__ graph theory-

Source: Internet
Author: User
Tags log log

This is the idea of the Johnson Johnson algorithm, which is to first go to the shortest circuit brush out dis (i) dis (i) and then transform the diagram. The power of the Edge (X,y) (x,y) is changed to W (x,y) +dis (x) −dis (y) W (x,y) +dis (x)-dis (y).
After this, the edge right is not negative, the new map to do the shortest line of things and the original image is completely equivalent, the new map from 1 1 to v V of the length of a path is the corresponding length of the original and Dis (v) dis (v) difference. Delta Delta deltas in dis dis may be useful in some questions related to weights and sizes.
This problem, we first rebuilt the map, when 1 to n δdis \delta dis are 0 0, when the weight of K K Edge plus 1 1, it is clear that the shortest possible length of the maximum increase min (k,n−1) min (k,n-1).
The shortest length has an upper bound, we can force the use of drums to maintain Dij dij, that is, the bucket instead of the priority queue, the shortest value of each point removed is not dropped log log, each o (n+m) O (n+m), the total complexity O (n+m) q) O ((n+m) q).

#include <cstdio> #include <queue> #include <algorithm> using namespace std;
typedef long Long LL;
const int maxn=100005,maxe=100005,maxd=100005;
int n,m,q;
LL Dis[maxn],f[maxn],inf=1e16;
int Fir[maxn],nxt[maxe],son[maxe],w[maxe],tot; void Add (int x,int y,int z) {son[++tot]=y; w[tot]=z; nxt[tot]=fir[x]; fir[x]=tot;} struct data{int x;
    LL D;
    Data (int t1=1,ll t2=1) {x=t1;d=t2;}
BOOL operator < (const data &b) const{return d<b.d;}};
Priority_queue <data> Heap; 
    inline void Dij () {for (int i=1;i<=n;i++) dis[i]=inf; dis[1]=0;
    Heap.push (data (1,dis[1])); while (! Heap.empty ()) {int x=heap.top (). x; 
        Heap.pop (); for (int j=fir[x];j;j=nxt[j]) if (Dis[x]+w[j]<dis[son[j]]) dis[son[j]]=dis[x]+w[j], Heap.push (Data (son[j],dis[so
    N[J]));
}} queue<int> Bk[maxd];
    int main () {freopen ("cf843d.in", "R", stdin);
    Freopen ("Cf843d.out", "w", stdout);
    scanf ("%d%d%d", &n,&m,&q); for (int i=1;i<=m;i++) {int x,y,z; scanf ("%d%d%d", &x,&y,&z); 
    Add (x,y,z);
    } dij ();
        while (q--) {int _type,k,x; scanf ("%d%d", &_type,&k); if (_type==1) printf ("%i64d\n", dis[k]<inf?dis[k]:-1);
            else{for (int i=1;i<=k;i++) scanf ("%d", &x), w[x]++; for (int i=1;i<=n;i++) F[i]=inf; f[1]=0;
            Bk[0].push (1); for (int i=0,_max=0;i<=_max;i++) while (! Bk[i].empty ()) {int X=bk[i].front ();
                Bk[i].pop ();
                if (f[x]<i) continue;
                    for (int j=fir[x];j;j=nxt[j]) {int t=f[x]+ (w[j]+dis[x]-dis[son[j]]);
                        if (T<f[son[j]]) {f[son[j]]=t;
                    if (T<=min (k,n-1)) Bk[t].push (Son[j]), _max=max (_max,t);   
        for (int i=1;i<=n;i++) dis[i]=min (Inf,dis[i]+f[i]);
} return 0;

 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.