UVA - 1336 Fixing the Great Wall

problem

Robot repair the Great Wall, to three integers, n represents the gap (the gap seen point), v (robot speed of action), x (now the location of the robot), x does not coincide with any point defects, for n points, each point there are three integers, x the position of the representative point defect, the cost of repair representatives C, delta increases with time on behalf of the cost, the total cost of a c + delta * t

Entry

Three integer parameters n, v, x, n sets of back point, x, c, delta, no greater than minimum cost 1 e 9 1e^9

Export

Repair costs, cut output integer

Thinking

Repair point must be connected, because the repair does take time, if it is the middle point did not have time to repair is certainly not the optimal solution, the state is dp [i] [j] [k], [i, j] represents the fix [ij] is a region (points in ascending order according to the number of x), k of size 2, k = 0 representative of the robot stops at the extreme left point i, k = 1 stops at the extreme right representatives

important point

How accumulated by time spent? For each point, it increases with time spent is all unpatched point increase this time and spent (such as a robot to a point in time multiplied by all the unpatched points and delta, which is the first robot repair points to spend at point [1] .c + t * (sum (delta))), so that, each time to the next point, all want to start counting from 0

State transition equation (sheet method):
next repair point there are two possibilities, one is left repair point, the right one is repaired
left patch point:
f ( i , j , 0 ) = m i n ( f ( i + 1 , j , 0 ) + c o s t 1 , f ( i + 1 , j , 1 ) + c o s t 2 ) f(i,j,0)=min(f(i+1,j,0)+cost1,f(i+1,j,1)+cost2) repair the left of point i-1, there are two cases, one is movable in the robot i at left, and repair and co Cost1, the robot moves to the left one is the point j, the mobile repair and co cost2
repair the right of the point:
f ( i , j , 1 ) = m i n ( f ( i , j 1 , 0 ) + c o s t 1 , f ( i , j 1 , 1 ) + c o s t 2 ) f(i,j,1)=min(f(i,j-1,0)+cost1,f(i,j-1,1)+cost2) repair the right Similarly

bug: a forgotten start points in added cost in [k] .c

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=1005,Inf=1500000000;
struct Point{
    int x,c,delta;
    Point(int x=0,int c=0,int delta=0):x(x),c(c),delta(delta) {}
    bool operator < (const Point &rhs) const {
        return x<rhs.x;
    }
};
Point points[maxn];
//dp[i][j][k]  [i,j]代表经历过了[i,j]这些点,k=0代表机器人在i,k=1代表机器人在j
int n,v,x,xi,ci,deltai,delta[2][maxn];
double dp[maxn][maxn][2];
//update state
void update(int i,int j,int k,int i2,int j2,int k2){
    if(dp[i2][j2][k2]>1e9) return;
    double diff=0;
    if(!k2 && !k) diff=fabs((points[i2].x-points[i].x)*1.0/v);
    else if(!k2 && k) diff=fabs((points[i2].x-points[j].x)*1.0/v);
    else if(k2 && !k) diff=fabs((points[j2].x-points[i].x)*1.0/v);
    else if(k2 && k) diff=fabs((points[j2].x-points[j].x)*1.0/v);
    dp[i][j][k]=min(dp[i][j][k],dp[i2][j2][k2]+points[(k==0)?i:j].c+diff*(delta[0][i2-1]+delta[1][j2+1]));
}

int main(void){
    while(cin>>n>>v>>x && n){
        for(int i=1;i<=n;++i){
            scanf("%d%d%d",&points[i].x,&points[i].c,&points[i].delta);
        }
        sort(points+1,points+n+1);
        memset(delta,0,sizeof(delta));
        for(int i=1;i<=n;++i) dp[i][i][0]=dp[i][i][1]=Inf;
        for(int i=1;i<=n;++i) delta[0][i]=delta[0][i-1]+points[i].delta;
        for(int i=n;i>=1;--i) delta[1][i]=delta[1][i+1]+points[i].delta;

        int k=int(lower_bound(points+1,points+1+n,Point(x,0,0))-points);
        if(k>=1 && k<=n) dp[k][k][0]=dp[k][k][1]=points[k].c+(points[k].x-x)*1.0/v*(delta[0][k]+delta[1][k+1]);
        if(k-1>=1 && k-1<=n)
            dp[k-1][k-1][0]=dp[k-1][k-1][1]=points[k-1].c+(x-points[k-1].x)*1.0/v*(delta[0][k-1]+delta[1][k]);
        for(int d=1;d<n;++d){
            for(int i=1;i<=n;++i){
                if(i+d>n) break;
                int j=i+d;
                dp[i][j][0]=dp[i][j][1]=Inf;
                update(i,j,0,i+1,j,0);
                update(i,j,0,i+1,j,1);
                update(i,j,1,i,j-1,0);
                update(i,j,1,i,j-1,1);
            }
        }
        int ans=int(min(dp[1][n][0],dp[1][n][1]));
        printf("%d\n",ans);
    }
}
发布了15 篇原创文章 · 获赞 0 · 访问量 176

Guess you like

Origin blog.csdn.net/zpf1998/article/details/103913199