第一反应是设表示时刻到点上最小烦躁值
但是其实只能为几个固定值,因为边有一定的限制作用
更准确的来说,我们只要知道我们当前刚走完哪条边,我们就能知道当前的时间同时也能知道当前在哪个点(废话)
所以干脆将状态设为表示当前刚刚走完第条边最小的烦躁值
转移为
长得就很能斜率优化(
设
那么对于比优需满足
如果单纯做这个无限制的DP,维护一个下凸包即可
先不谈斜率优化,因为我们也不能只维护一个凸包
因为有转移的限制
我们分别考虑怎么处理这两个限制
对于后一个限制,我们考虑维护多个凸包,对于每个点维护一个凸包保存每一个终点是该点的决策
转移时直接从起点处凸包获得最优转移,然后考虑前一个限制
我们只要多加一层时间循环,按照时间顺序将每一个转移加入凸包即可(此处以为准,因为斜率优化要保证横坐标单调)
复杂度
千万不要跟我一样,直接拿vector的大小直接减去int,一定要强制转换为int再做
#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;
}