luogu P5468 [NOI2019] route home (gradient optimization, DP)

Topic links: (luogu) https://www.luogu.org/problemnew/show/P5468

Solution: explosion destroyed long long life

I was too dishes, the simple question exam that actually did not expect positive solution ......

Set \ (dp [i] \) represents the final step is to sit \ (i \) car, spent a total of irritability value on the waiting (not including the final time) is \ (f [i] \) .

This shift is then easy to find a DAG. (I actually think that there is a ring in the examination room, so you can abandon ......)

Transfer equation \ (dp [i] = \ min_ {j | y [j] = x [i]} dp [j] + A (x_i-x_j) ^ 2 + B (x_i-x_j) + C \)

Then this thing is obviously the slope can be optimized for each enumeration chronologically \ (i \) , for a \ (i \) we started according to \ (x [i] \) to obtain \ (dp [i] \) for a \ (i \) end we \ (dp [i] \) to update \ (the y-[i] \) . obviously, this thing can then optimize the slope, then for each point \ (i \) to maintain the convex hull.

Must be careful not burst long long! I \ (\ inf \) open to the \ (10 ^ {11} \) , it is necessary to ensure not put \ (\ inf \) added to the queue, or the time to compare the slope optimized multiplying two will burst ll.

Code

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iostream>
#include<vector>
#include<algorithm>
#define llong long long
using namespace std;

inline int read()
{
    int x=0; bool f=1; char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
    for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+(c^'0');
    if(f) return x;
    return -x;
}

const int N = 1e5;
const int M = 2e5;
const int C = 1000;
const llong INF = 100000000000ll;
struct Point
{
    llong x,y;
    Point() {}
    Point(llong _x,llong _y) {x = _x,y = _y;}
};
struct Element
{
    int u,v; llong x,y;
} a[M+3];
vector<int> sid[C+3],tid[C+3];
vector<int> que[M+3];
int hd[M+3];
llong dp[M+3];
int n,m;
llong arga,argb,argc;

llong calcy(llong x) {return dp[x]+arga*a[x].y*a[x].y-argb*a[x].y;}
llong calcx(llong x) {return 2ll*arga*a[x].y;}
int cmp_slope(int x,int y,int z)
{
    llong xx = calcx(x),xy = calcy(x),yx = calcx(y),yy = calcy(y),zx = calcx(z),zy = calcy(z);
    return (yy-xy)*(zx-yx)>(zy-yy)*(yx-xx) ? 1 : -1;
}
llong calcdp(int x,llong y) {return dp[x]+arga*(y-a[x].y)*(y-a[x].y)+argb*(y-a[x].y)+argc;}

int main()
{
    scanf("%d%d%lld%lld%lld",&n,&m,&arga,&argb,&argc); int mx = 0;
    for(int i=1; i<=m; i++)
    {
        scanf("%d%d%lld%lld",&a[i].u,&a[i].v,&a[i].x,&a[i].y);
        sid[a[i].x].push_back(i); mx = max(mx,(int)a[i].y);
    }
    que[1].push_back(0);
    llong ans = INF;
    dp[1] = 0ll; for(int i=2; i<=m; i++) dp[i] = INF;
    for(int i=0; i<=mx; i++)
    {
        for(int j=0; j<tid[i].size(); j++)
        {
            int x = tid[i][j],v = a[x].v; //x: 边的编号 v: 终点的编号 
            while(hd[v]+1<que[v].size() && cmp_slope(que[v][que[v].size()-2],que[v][que[v].size()-1],x)>=0) {que[v].pop_back();}
            que[v].push_back(x);
        }
        for(int j=0; j<sid[i].size(); j++)
        {
            int x = sid[i][j],u = a[x].u; //x: 边的编号 u: 起点的编号
            if(que[u].size()==0) continue; //注意特判! 
            while(hd[u]+1<que[u].size() && calcdp(que[u][hd[u]],i)>=calcdp(que[u][hd[u]+1],i)) {hd[u]++;}
            dp[x] = calcdp(que[u][hd[u]],i);
            if(a[x].v==n) {ans = min(ans,dp[x]+a[x].y);}
            tid[a[x].y].push_back(x); //如果读入时把所有y全都放进去,那么会导致队列中出现inf而爆long long.
        }
    }
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/suncongbo/p/11203763.html