题解:P15814 [JOI 2014 Final] 蜜袋鼯 / Sugar Glider

· · 题解

以下忽略 w\le u 的边 (u,v,w),并设 w 为边权,h 为当前高度,t 为目前耗时。

观察三种移动方式 h 和 t 的变化:

设势能 \phi=h+t,那么上升时 \phi 变大,下降或横跳时 \phi 不变,又因最终高度确定,求最终的 t 最小即最终的 \phi 最小。所以:问题等价于要从 1 以最小的上升次数到达 n。

因为横跳需要 h\ge w,跳之后 h>0 的话可以等到跳完再上升,所以每次横跳前当且仅当 h<w 时上升一定不劣。

所以第一次 h=0 之后,每次横跳前上升到 w,横跳用时 w,所以共用时 2w,建反图、边权乘 2 再以 n 为源跑最短路,可以得到在每个点 h=0 时出发的最少耗时。X=0 的部分分也有提示。

第一次 h=0 之前不上升,则 \phi 不变,初始时 \phi=h+t=X+0=X,则 h=\phi-t=X-t。dis_u 为到达 u 的最短时间跑最短路,可以知道不上升地到达每个点且恰好 h=0 的最短时间。

:::success[AC 代码]{open}

#include <bits/stdc++.h>
#define fi first
#define se second
#define mid ((l+r)>>1)
#define bmid ((l+r+1)>>1)
#define pb push_back
#define eb emplace_back
using namespace std;
using ll= long long;
#ifndef ONLINE_JUDGE
template <typename tp>
void _debug(const tp& t) {cerr<<t<<'\n';}
template <typename tp,typename... args>
void _debug(const tp& t, const args&... rest) {cerr<<t<<' ';_debug(rest...);}
#define debug(...) _debug(#__VA_ARGS__ " =", __VA_ARGS__)
#else
#define debug(...) 0
#endif
#define int ll
const int N=100005,H=N<<2,inf=5000000000000000000ll,mod=1000000007;
int n,m,x,h[N],d1[N],d2[N];
priority_queue<pair<int,int> > pq;
void dij(const int s,int dis[],vector<pair<int,int> > gr[]) {
    fill(dis,dis+n+1,inf);
    dis[s]=0,pq.emplace(0,s);
    while(pq.size()) {
        const int u=pq.top().se,d=-pq.top().fi; pq.pop();
        if(dis[u]!=d) continue;
        for(auto& i: gr[u]) {
            const int v=i.fi,w=i.se;
            if(dis[v]<=d+w) continue;
            dis[v]=d+w,pq.emplace(-d-w,v);
        }
    }
}
vector<pair<int,int> > rg[N],g[N];
signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cin>>n>>m>>x;
    for(int i=1;i<=n;i++)
        cin>>h[i];
    for(int a,b,t;m--;) {
        cin>>a>>b>>t;
        if(h[a]>=t) g[a].eb(b,t),rg[b].eb(a,t<<1);
        if(h[b]>=t) g[b].eb(a,t),rg[a].eb(b,t<<1);
    }
    dij(n,d2,rg);
    fill(d1+2,d1+n+1,inf);
    pq.emplace(0,1);
    int ans=inf;
    while(pq.size()) {
        const int u=pq.top().se,d=-pq.top().fi; pq.pop();
        if(d1[u]!=d) continue;
        const int hu=x-d;
        if(u==n) ans=min(ans,d+h[n]-hu);
        if(hu<=0) {
            ans=min(ans,d-hu+d2[u]+h[n]);
            continue;
        }
        for(auto& i: g[u]) {
            const int v=i.fi,w=i.se;
            const int nh=min(hu-w,h[v]);
            const int nd=d+hu-nh;
            if(d1[v]<=nd) continue;
            d1[v]=nd,pq.emplace(-nd,v);
        }
    }
    if(ans==inf) ans=-1;
    cout<<ans;
    return 0;
}

:::