P6302 [NOI2019] 回家路线 加强版

· · 题解

题目传送门

本题为P5468的加强版,重点看数据范围:

2 \leq n \leq 10^5,1 \leq m\leq 10^6,0 \leq p_i \le q_i \leq 4 \times 10^4

当然还有三个参数:

0 \leq A \leq 10, 0 \leq B,C \leq 10^7

注意到此时 B 和 C 已经超过了 10^7,需要开 long long。同时还发现 m \leq 10^6,这意味着 \text{P5468} 正解(即 O(m) 的斜率优化 \text{DP})改一改就可以通过本题。

这里还是把斜率优化 \text{DP} 的思路简单说一下:

设 f_i 表示走完第 i 条边最小的代价,则有暴力转移:

f_i=\min \{ f_j+A \times (p_i-q_j)^2 + B \times (p_i-q_j)+C \}

时间复杂度 O(mt),其中 t=\max_{i=1}^{m}q_i,需要优化。

发现这个方程有平方项,考虑展开:

f_j+A \times q_j^2-B \times q_j=2\times A \times p_i \times q_j -f_i-A \times p_i^2-B \times p_i-C

把这个方程看成一次函数标准形式 y=kx+b(k \neq 0),其中:

y=f_j+A \times q_j^2 - B \times q_j k=2 \times A \times p_i x=q_j b=-f_i-A \times p_i^2-B \times p_i-C

用单调队列维护凸壳即可,每条边进出一次队列,时间复杂度 O(m),可以通过本题。

代码(使用了较多 \text{STL},可以自己卡一卡常数,注意开 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;
}