跳到正文

题解

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

_SLIDE_约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年5月5日
收录
2026年11月18日
更新
2026年11月18日
篇幅
约 5 分钟
获赞
15
题目
P15814

版本

  1. v1当前

以下忽略 w≤uw\le u 的边 (u,v,w)(u,v,w),并设 ww 为边权,hh 为当前高度,tt 为目前耗时。

观察三种移动方式 hh 和 tt 的变化:

  • 如果是上升 11,那么 h←h+1,t←t+1h\larr h+1,t\larr t+1。
  • 如果是下降 11,那么 h←h−1,t←t+1h\larr h-1,t\larr t+1。
  • 如果是横跳,边权为 ww,那么 h←h−w,t←t+wh\larr h-w,t\larr t+w。

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

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

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

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

AC 代码
C++
#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;
}
原文
存档评论6 条抓取于