P6302 [NOI2019] 回家路线 加强版
题目传送门
本题为P5468的加强版,重点看数据范围:
当然还有三个参数:
注意到此时 long long。同时还发现
这里还是把斜率优化
设
时间复杂度
发现这个方程有平方项,考虑展开:
把这个方程看成一次函数标准形式
用单调队列维护凸壳即可,每条边进出一次队列,时间复杂度
代码(使用了较多 long long):
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
typedef long double ld;
inline ll read(){
ll f=1,x=0;
char c=getchar();
while(c<'0'||c>'9') {
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return f*x;
}
inline void write(ll x){
if(x<0)x=-x,putchar('-');
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline ll max(ll x,ll y){
return x>=y?x:y;
}
inline ll min(ll x,ll y){
return x<=y?x:y;
}
const int N=1e5+10,M=1e6+10,T=4e4+10;
const ll oo=1e18;//正无限
int n,m;
ll A,B,C,x[M],y[M],p[M],q[M];
struct point{
ll x,y;
int id/*对应边的编号*/;
};
ll head[N];
vector<point> hull[N];
vector<int> d[T];
queue<int> res[T];
ll maxT/*即前面分析中的t*/,f[M],ans=oo;
inline ld get_slope(point a,point b){
ll xx=a.x-b.x,yy=a.y-b.y;
if(xx==0){
if(yy>0)
return 1.0*oo;
else
return -oo*1.0;
}
return (1.0*yy)/(1.0*xx);
}
inline void insert(int id){
int pos=y[id];
point new_one=(point){q[id],f[id]+A*q[id]*q[id]-B*q[id],id};
while(hull[pos].size()-head[pos]>=2){
int siz=hull[pos].size();
if(get_slope(hull[pos][siz-1],hull[pos][siz-2])<get_slope(hull[pos][siz-2],new_one))
break;
hull[pos].pop_back();
}
hull[pos].push_back(new_one);
}
inline void _delete(ld k,int pos){
while(hull[pos].size()-head[pos]>=2){
if(k<get_slope(hull[pos][head[pos]],hull[pos][head[pos]+1]))
break;
head[pos]++;
}
}
int main(){
n=read(),m=read(),A=read(),B=read(),C=read();
for(int i=1;i<=m;i++){
x[i]=read(),y[i]=read(),p[i]=read(),q[i]=read();
if(maxT<q[i])
maxT=q[i];
}
for(int i=1;i<=m;i++)
d[p[i]].push_back(i);
hull[1].push_back((point){0,0,0});
for(int t=0;t<=maxT;t++){
while(!res[t].empty()){
insert(res[t].front());
res[t].pop();
}
ld k;
for(int _=0,i,j,pos;_<d[t].size();_++){
i=d[t][_],pos=x[i];
if(head[pos]==hull[pos].size())//队列为空
continue;
k=2.0*A*p[i];
_delete(k,pos);
j=hull[pos][head[pos]].id;
f[i]=f[j]+A*(p[i]-q[j])*(p[i]-q[j])+B*(p[i]-q[j])+C;
res[q[i]].push(i);
if(y[i]==1ll*n)//走到终点
ans=min(ans,f[i]+q[i]);
}
}
return 0;
}