跳到正文

题解

题解 P6302 【[NOI2019]回家路线 加强版】

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

文章信息

收录
2025年12月12日
更新
2026年4月26日
篇幅
约 5 分钟
题目
P6302

版本

  1. v1当前

第一反应是设dp[u][i]dp[u][i]表示ii时刻到uu点上最小烦躁值

但是其实ii只能为几个固定值,因为边有一定的限制作用

更准确的来说,我们只要知道我们当前刚走完哪条边,我们就能知道当前的时间同时也能知道当前在哪个点(废话)

所以干脆将状态设为dp[i]dp[i]表示当前刚刚走完第ii条边最小的烦躁值

转移为dp[i]=min⁡qj≤pi,yj=xi(dp[j]+A(pi−qj)2+B(pi−qj)+C)dp[i]=\min\limits_{q_j\leq p_i,y_j=x_i}(dp[j]+A(p_i-q_j)^2+B(p_i-q_j)+C)

长得就很能斜率优化(

设fi=dp[i]+A∗qi2−B∗qif_i=dp[i]+A*q_i^2-B*q_i

那么对于jj比kk优需满足(qj>qk)(q_j>q_k)

fj−fkqj−qk<2∗A∗pi\dfrac {f_j-f_k} {q_j-q_k}<2*A*p_i

如果单纯做这个无限制的DP,维护一个下凸包即可

先不谈斜率优化,因为我们也不能只维护一个凸包

因为有转移的限制qj≤pi,yj=xiq_j\leq p_i,y_j=x_i

我们分别考虑怎么处理这两个限制

对于后一个限制,我们考虑维护多个凸包,对于每个点维护一个凸包保存每一个终点是该点的决策

转移时直接从起点处凸包获得最优转移,然后考虑前一个限制

我们只要多加一层时间循环,按照时间顺序将每一个转移加入凸包即可(此处以qq为准,因为斜率优化要保证横坐标单调)

复杂度O(m+T)O(m+T)

千万不要跟我一样,直接拿vector的大小直接减去int,一定要强制转换为int再做

C++
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<set>
#include<queue>
#include<stack>
#define M 1000010
#define N 100010
#define inf 2e18
#define LL long long
#define ull unsigned long long
#define int long long
const int mod=998244353;
using namespace std;
inline int read(){
	int f=1,x=0;char s=getchar();
	while (!isdigit(s)){if(s=='-')f=-1;s=getchar();}
	while (isdigit(s)){x=(x<<1)+(x<<3)+(s^48),s=getchar();}
	return x*f;
}
int n,m,A,B,C,cnt,tmax,head[N],ans=inf,dp[M];
struct edge{
	int from,to,st,ed;
}e[M];
vector<int>st[N],res[N];queue<int>que[N];
inline void add_edge(int u,int v,int p,int q){
	e[++cnt].from=u,e[cnt].to=v,e[cnt].st=p,e[cnt].ed=q;
}
inline int f(int x){
	return dp[x]+A*e[x].ed*e[x].ed-B*e[x].ed;
}
signed main(void){
	n=read(),m=read(),A=read(),B=read(),C=read();
	for (int i=1;i<=m;i++){
		int u=read(),v=read(),p=read(),q=read();
		add_edge(u,v,p,q),tmax=max(tmax,q);
		res[p].push_back(i);
	}st[1].push_back(0);
	for (int i=0;i<=tmax;i++){
		while (!que[i].empty()){
			int pos=que[i].front(),p=e[que[i].front()].to;que[i].pop();
			while ((int)st[p].size()-1>head[p]&&(f(st[p][(int)st[p].size()-1])-f(st[p][(int)st[p].size()-2]))*(e[pos].ed-e[st[p][(int)st[p].size()-1]].ed)>=(f(pos)-f(st[p][(int)st[p].size()-1]))*(e[st[p][(int)st[p].size()-1]].ed-e[st[p][(int)st[p].size()-2]].ed))
				st[p].pop_back();
			st[p].push_back(pos);
		}
		for (int j=0;j<res[i].size();j++){
			int pos=res[i][j],p=e[pos].from;
			if ((st[p].size())<=head[p]) continue;
			while ((int)(st[p].size())-1>head[p]&&(f(st[p][head[p]+1]))-f(st[p][head[p]])<=2*A*e[pos].st*(e[st[p][head[p]+1]].ed-e[st[p][head[p]]].ed)) head[p]++;
			dp[pos]=dp[st[p][head[p]]]+A*(e[pos].st-e[st[p][head[p]]].ed)*(e[pos].st-e[st[p][head[p]]].ed)+B*(e[pos].st-e[st[p][head[p]]].ed)+C;
			que[e[pos].ed].push(pos);if (e[pos].to==n) ans=min(ans,dp[pos]+e[pos].ed);
		}
	}printf("%lld",ans);
	return 0;
}
原文