方程很简单

p[i][j] = min{dp[i-1][k] + |pos[i-1][k] - pos[i][k]|} + v[i][j];

循环求值肯定TLE,主要是绝对值不方便。好吧,我真的BI了狗了,竟然想不到怎么样去绝对值。F,只要从左到右dp一次,再从右往左dp一次,不就好了吗?????F。当然了,必须是选按位置排序。开始用单调队列,后来发现根本没必要了。

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm> using namespace std;
struct Node{
int pos,val;
};
const int inf=1<<30;
Node map[55][1050];
bool cmp(Node a,Node b){
if(a.pos<b.pos) return true;
return false;
}
int que[1050];
int dp[55][1050]; int main(){
int T,n,m,x;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&m,&n,&x);
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
scanf("%d",&map[i][j].pos);
}
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
scanf("%d",&map[i][j].val);
}
sort(map[i]+1,map[i]+1+n,cmp);
}
for(int i=1;i<=n;i++){
dp[1][i]=abs(map[1][i].pos-x)+map[1][i].val;
}
int k,mm;
for(int i=2;i<=m;i++){
mm=inf;
k=1;
for(int j=1;j<=n;j++){
for(k;k<=n;k++){
if(map[i-1][k].pos>map[i][j].pos) break;
mm=min(mm,dp[i-1][k]-map[i-1][k].pos);
}
dp[i][j]=mm+map[i][j].pos+map[i][j].val;
}
k=n;
mm=inf;
for(int j=n;j>=1;j--){
for(k;k>=1;k--){
if(map[i-1][k].pos<map[i][j].pos) break;
mm=min(mm,dp[i-1][k]+map[i-1][k].pos);
}
dp[i][j]=min(dp[i][j],mm-map[i][j].pos+map[i][j].val);
}
}
int ans=inf;
for(int i=1;i<=n;i++)
ans=min(dp[m][i],ans);
printf("%d\n",ans);
}
return 0;
}

  

HDU 4362的更多相关文章

  1. HDU 4362 Dragon Ball 贪心DP

    Dragon Ball Problem Description   Sean has got a Treasure map which shows when and where the dragon ...

  2. HDU 4362 Dragon Ball 线段树

    #include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...

  3. 清北学堂2018DP&图论精讲班 DP部分学习笔记

    Day 1 上午 讲的挺基础的--不过还是有些地方不太明白 例1 给定一个数n,求将n划分成若干个正整数的方案数. 例2 数字三角形 例7 最长不下降子序列 以上太过于基础,不做深入讨论 例3 给定一 ...

  4. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  6. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  7. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  9. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

随机推荐

  1. .net framework 3.5 安装报错 0x800F0954问题

    windows Server 2019 .net framework 3.5 安装报错 0x800F0954问题 .net framework 3.5的安装教程:但是安装出现0x800F0954这个错 ...

  2. Laravel5.1学习笔记18 数据库4 数据填充

    简介 编写数据填充类 使用模型工厂类 调用额外填充类 执行填充 #简介 Laravel includes a simple method of seeding your database with t ...

  3. hibernate.cfg.xml配置

    hibernate.hbm2ddl.auto 配置: create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这 ...

  4. jQuery——切换toggle

    toggle()-----显示隐藏 toggleClass-------添加类或者删除类 <!DOCTYPE html> <html lang="en"> ...

  5. 【译】x86程序员手册09-第3章程序指令集

    注:觉得本章内容与理解操作系统不直接相关,所以本章并未看完,也就没有翻译完,放在这里中是为了保证手册的完整.有兴趣的人可以去原址查看. https://pdos.csail.mit.edu/6.828 ...

  6. Ubuntu 关闭guest用户

    Ubuntu 关闭guest用户 ca0gu0@ub:~$ cat /etc/lightdm/lightdm.conf [SeatDefaults]autologin-user=falseallow- ...

  7. js的replace, 高亮

    ";console.log(str.replace(/\,/g, "")); //输出 123 ";console.log(str);//输出123 " ...

  8. Java中内部类详解—匿名内部类

    什么是内部类? 将一个类A定义在另一个类B里面,里面的那个类A就称为内部类,B则称为外部类.   成员内部类 定义在类中方法外的类. 定义格式: class 外部类 { class 内部类{ } } ...

  9. linux强制踢出已登录的用户及本地用户

    方法一: pkill -kill -t pts/0 方法二: fuser -k /dev/pts/0 你也可以给他发送关闭信息然后关闭 echo "你被管理员踢出了" > / ...

  10. CAD增加一个有形的线型(网页版)

    主要用到函数说明: _DMxDrawX::AddTextStyle1 向数据库中增加一个文字样式.详细说明如下: 参数 说明 BSTR pszName 文字样式名称 BSTR pszFileName ...