UVALive 3983 Robotruck

来源:互联网 发布:网络摄像头修改ip 编辑:程序博客网 时间:2024/06/05 01:17

题目链接:http://acm.hust.edu.cn/vjudge/problem/13674


题意:有n个垃圾,给出每个的坐标和重量(xi,yi,wi),一个机器人的最大载重为C,求把所有垃圾按照顺序放进垃圾桶(0,0)走的最短距离。两点的距离为坐标差的绝对值之和。


思路:dp[i]表示把前i个垃圾扔进垃圾桶走过的最短距离。枚举上一次扔进垃圾桶的垃圾j,那么[j+1,i]就作为下一次收集一起扔进垃圾桶且∑wi <= C。

dp[i] = dp[j] + 从原点回到j+1点的距离 + 从j+1点按顺序走到i点的距离 + 从i点回到原点垃圾桶的距离。

dis(i,j)表示i点直接到j点的距离  path(i)表示从原点按顺序走到i点的距离。

dp[i] = min( dp[j] + dis(0,j+1) + path(i)-path(j+1) + dis(0,i)  )  = dis(0,i) + path(i) + min( dp[j] + dis(0,j+1) -path(j+1) ) 可以用单调队列优化


#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <sstream>#include <queue>#include <utility>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod 100000007const int maxn = 100009;LL sum[maxn];LL cost[maxn];LL f[maxn];int q[maxn];LL V[maxn];int head,tail;int n,c;struct node{    int x,y,v;}p[maxn];int dis(int a,int b){    return abs(p[a].x-p[b].x) + abs(p[a].y-p[b].y);}void init(){    cin>>c>>n;    p[0].x = p[0].y = p[0].v = 0;    sum[0] = cost[0] = 0;    rep(i,1,n)    {        scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].v);        sum[i] = sum[i-1] + dis(i-1,i);        cost[i] = cost[i-1] + p[i].v;    }}void solve(){    head = 0;    tail = 1;    q[0] = 0;    V[0] = dis(0,1) - sum[1];    rep(i,1,n)    {        while( head < tail && cost[i] - cost[ q[head] ] > c ) head++;        f[i] = dis(0,i) + sum[i] + V[head];        LL temp = f[i] + dis(0,i+1) - sum[i+1];        while( head < tail && V[tail-1] > temp ) tail--;        q[tail] = i;        V[tail] = temp;        tail++;    }    cout<<f[n]<<endl;}int main(){    int T;    cin>>T;    while(T--)    {        init();        solve();        if ( T ) puts("");    }    return 0;}



0 0
原创粉丝点击