Robotruck - ZOJ 3031 dp

来源:互联网 发布:linux 关闭安全狗 编辑:程序博客网 时间:2024/05/16 13:03

Robotruck

Time Limit: 5 Seconds      Memory Limit: 32768 KB

This problem is about a robotic truck that distributes mail packages to several locations in a factory. The robot sits at the end of a conveyer at the mail office and waits for packages to be loaded into its cargo area. The robot has a maximum load capacity, which means that it may have to perform several round trips to complete its task. Provided that the maximum capacity is not exceeded, the robot can stop the conveyer at any time and start a round trip distributing the already collected packages. The packages must be delivered in the incoming order.

The distance of a round trip is computed in a grid by measuring the number of robot moves from the mail office, at location (0,0), to the location of delivery of the first package, the number of moves between package delivery locations, until the last package, and then the number of moves from the last location back to the mail office. The robot moves a cell at a time either horizontally or vertically in the factory plant grid. For example, consider four packages, to be delivered at the locations (1,2),(1,0), (3,1), and (3,1). By dividing these packages into two round trips of two packages each, the number of moves in the first trip is 3+2+1=6, and 4+0+4=8 in the second trip. Notice that the two last packages are delivered at the same location and thus the number of moves between them is 0.

Given a sequence of packages, compute the minimum distance the robot must travel to deliver all packages.

Input

A positive integer P in a single line followed by a sequence of P test cases. Each test case consists of a line containing one positive integer C, not greater than 1,000,000, indicating the maximum capacity of the robot, a line containing one positive integer N, not greater than 100,000, which is the number of packages to be loaded from the conveyer. Next, there are N lines containing, for each package, two non-negative integers to indicate its delivery location in the grid, and a non-negative integer to indicate its weight. The weight of the packages is always smaller than the robot's maximum load capacity. The order of the input is the order of appearance in the conveyer.

Output

One line containing one integer representing the minimum number of moves the robot must travel to deliver all the packages.

Sample Input

11041 2 31 0 33 1 43 1 4

Sample Output

14

题意:一个机器人在一个二维的平面运东西,每次能同时拿走的物品重量不能超过他的承受力,需要按照顺序去运送物品,每个单位时间走一个单位长度,为运送物品的最少时间是多少。

思路:简单的dp,dp[i]表示运送前i个物品的最短时间,dp[j]=min(dp[j],dp[i-1]+dis[j]-dis[i]+x[i]+y[i]+x[j]+y[j]);

AC代码如下:

#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;typedef long long ll;ll dp[100010],INF=1e18,ret,x[100010],y[100010],dis[100010];int weight[100010];int main(){    int t,n,m,i,j,k;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&m,&n);        for(i=1;i<=n;i++)        {            scanf("%lld%lld%d",&x[i],&y[i],&weight[i]);            weight[i]+=weight[i-1];            dis[i]=dis[i-1]+abs(x[i]-x[i-1])+abs(y[i]-y[i-1]);            dp[i]=INF;        }        for(j=1;j<=n;j++)           for(i=j;i>=1;i--)           {               if(weight[j]-weight[i-1]>m)                 break;               ret=dp[i-1]+dis[j]-dis[i]+x[i]+y[i]+x[j]+y[j];               if(ret<dp[j])                 dp[j]=ret;           }        printf("%lld\n",dp[n]);    }}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电脑wps文字打不开怎么办 电脑的wps打不开怎么办 qq锁异地登录怎么办 手机玩游游戏太热怎么办 运动秀注册不了怎么办 应用卸载不掉怎么办 aweme卸载不掉怎么办 win10系统安装失败怎么办 英雄联盟自动更新失败怎么办 英雄联盟老出错怎么办? lol下载0kb怎么办 手机md5校验失败怎么办 王者荣耀占内存怎么办 信用卡被暂停使用怎么办 宝宝爱涂鸦怎么办呢 大良老板欠薪怎么办 投影仪流明很低怎么办 户外涂鸦没有墙怎么办 word突然未响应怎么办 文档突然未响应怎么办 pr不自己播放怎么办 笔记本电脑空格键打字失灵怎么办 小孩受凉打喷嚏流鼻涕怎么办 头疼打喷嚏流鼻涕鼻塞怎么办 头疼打喷嚏想吐怎么办 感冒鼻塞头疼打喷嚏怎么办 孕期打喷嚏漏尿怎么办 孕妇打喷嚏漏尿怎么办 打喷嚏停不下来怎么办 鼻炎不停的打喷嚏怎么办 感冒了鼻子痒怎么办 怀孕鼻炎严重了怎么办 鼻炎不停打喷嚏流鼻涕怎么办 鼻子里面干痒怎么办 有鼻炎老打喷嚏怎么办 鼻子喷嚏打不停怎么办 鼻子痒想打喷嚏怎么办 鼻痒打喷嚏流鼻涕怎么办 原配找到公司了怎么办 西北游不吃羊肉怎么办 水培植物冬天该怎么办