Let the light guide us

Time Limit: 2000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3698
64-bit integer IO format: %I64d      Java class name: Main

 
Plain of despair was once an ancient battlefield where those brave spirits had rested in peace for thousands of years. Actually no one dare step into this sacred land until the rumor that “there is a huge gold mine underneath the plain” started to spread.

Recently an accident destroyed the eternal tranquility. Some greedy fools tried using powerful bombs to find the hidden treasure. Of course they failed and such behavior enraged those spirits--the consequence is that all the human villages nearby are haunted by ghosts.

In order to stop those ghosts as soon as possible, Panda the Archmage and Facer the great architect figure out a nice plan. Since the plain can be represented as grids of N rows and M columns, the plan is that we choose ONLY ONE cell in EACH ROW to build a magic tower so that each tower can use holy light to protect the entire ROW, and finally the whole plain can be covered and all spirits can rest in peace again. It will cost different time to build up a magic tower in different cells. The target is to minimize the total time of building all N towers, one in each row.

“Ah, we might have some difficulties.” said Panda, “In order to control the towers correctly, we must guarantee that every two towers in two consecutive rows share a common magic area.”

“What?”

“Specifically, if we build a tower in cell (i,j) and another tower in cell (i+1,k), then we shall have |j-k|≤f(i,j)+f(i+1,k). Here, f(i,j) means the scale of magic flow in cell (i,j).”

“How?”

“Ur, I forgot that you cannot sense the magic power. Here is a map which shows the scale of magic flows in each cell. And remember that the constraint holds for every two consecutive rows.”

“Understood.”

“Excellent! Let’s get started!”

Would you mind helping them?

 

Input

There are multiple test cases.

Each test case starts with a line containing 2 integers N and M (2<=N<=100,1<=M<=5000), representing that the plain consists N rows and M columns.

The following N lines contain M integers each, forming a matrix T of N×M. The j-th element in row i (Tij) represents the time cost of building a magic tower in cell (i, j). (0<=Tij<=100000)

The following N lines contain M integers each, forming a matrix F of N×M. The j-th element in row i (Fij) represents the scale of magic flows in cell (i, j). (0<=Fij<=100000)

For each test case, there is always a solution satisfying the constraints.

The input ends with a test case of N=0 and M=0.

 

Output

For each test case, output a line with a single integer, which is the minimum time cost to finish all magic towers.

 

Sample Input

3 5
9 5 3 8 7
8 2 6 8 9
1 9 7 8 6
0 1 0 1 2
1 0 2 1 1
0 2 1 0 2
0 0

Sample Output

10

Source

 
解题:dp[i][j]表示第i行第j列放灯
 
 #include <bits/stdc++.h>
using namespace std;
const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
struct node {
int minv,lazy;
} tree[M<<];
int T[N][M],F[N][M],dp[N][M];
void build(int L,int R,int v) {
tree[v].lazy = INF;
tree[v].minv = INF;
if(L == R) return;
int mid = (L + R)>>;
build(L,mid,v<<);
build(mid+,R,v<<|);
}
inline void pushdown(int v) {
if(tree[v].lazy < INF) {
tree[v<<].lazy = min(tree[v<<].lazy,tree[v].lazy);
tree[v<<].minv = min(tree[v<<].minv,tree[v<<].lazy);
tree[v<<|].lazy = min(tree[v<<|].lazy,tree[v].lazy);
tree[v<<|].minv = min(tree[v<<|].minv,tree[v<<|].lazy);
tree[v].lazy = INF;
}
}
inline void pushup(int v) {
tree[v].minv = min(tree[v<<].minv,tree[v<<|].minv);
}
void update(int L,int R,int lt,int rt,int val,int v) {
if(lt <= L && rt >= R) {
tree[v].lazy = min(tree[v].lazy,val);
tree[v].minv = min(tree[v].lazy,tree[v].minv);
return;
}
pushdown(v);
int mid = (L + R)>>;
if(lt <= mid) update(L,mid,lt,rt,val,v<<);
if(rt > mid) update(mid+,R,lt,rt,val,v<<|);
pushup(v);
}
int query(int L,int R,int lt,int rt,int v) {
if(lt <= L && rt >= R) return tree[v].minv;
pushdown(v);
int mid = (L + R)>>,ret = INF;
if(lt <= mid) ret = query(L,mid,lt,rt,v<<);
if(rt > mid) ret = min(ret,query(mid+,R,lt,rt,v<<|));
pushup(v);
return ret;
}
int main() {
int n,m;
while(scanf("%d%d",&n,&m),n||m) {
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j)
scanf("%d",T[i] + j);
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j)
scanf("%d",F[i] + j);
for(int i = ; i <= m; ++i) dp[][i] = T[][i];
for(int i = ; i <= n; ++i) {
build(,m,);
for(int j = ; j <= m; ++j)
update(,m,max(,j - F[i-][j]),min(j + F[i-][j],m),dp[i-][j],);
for(int j = ; j <= m; ++j) {
int tmp = query(,m,max(,j - F[i][j]),min(m,j + F[i][j]),);
dp[i][j] = min(INF,tmp + T[i][j]);
}
}
int ret = INF;
for(int i = ; i <= m; ++i)
ret = min(ret,dp[n][i]);
printf("%d\n",ret);
}
return ;
}

HDU 3698 Let the light guide us的更多相关文章

  1. 题解 HDU 3698 Let the light guide us Dp + 线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  2. hdu 3698 Let the light guide us(线段树优化&简单DP)

    Let the light guide us Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/O ...

  3. HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)

    Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...

  4. hdu3698 Let the light guide us dp+线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  5. hdu 3698 UVA1490 Let the light guide us 线段树优化DP

    题目链接 and 题目大意 hdu3698 但是 hdu的数据比较弱,所以在这luogu提交吧UVA1490 Let the light guide us 有一个\(n*m\)的平原,要求每行选一个点 ...

  6. HDU 2857 Mirror and Light

    /* hdu 2857 Mirror and Light 计算几何 镜面反射 */ #include<stdio.h> #include<string.h> #include& ...

  7. hdu 2857:Mirror and Light(计算几何,点关于直线的对称点,求两线段交点坐标)

    Mirror and Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 4276-The Ghost Blows Light(树状背包)

    题意: n个房间,每个有一定的钱,一个房间到另一个房间花费一定的时间,给你房间连接树,求在t时间内到达房间m能得到的最大钱数(从房间1(根)出发) 分析: 该题关键是最后要到达m,没有这个条件,就是基 ...

  9. HDU 3698 DP+线段树

    给出N*M矩阵.每一个点建立灯塔有花费.每一个点的灯塔有连接范围,求每一行都建立一个灯塔的最小花费,要求每相邻两行的灯塔能够互相连接.满足 |j-k|≤f(i,j)+f(i+1,k) DP思路,dp[ ...

随机推荐

  1. 关于使用chrome插件改动全部的站点的响应responseHeaders头的注意

    1 眼下我掌握的调试技巧非常不方便,如今使用的是浏览器动作,每次都须要点击那个popup页面弹出,然后右键->查看元素,才干显示它的调试面板.一点击某些位置它又没有了; 2 改动响应报头的值时, ...

  2. [深入理解Android卷一全文-第七章]深入理解Audio系统

    由于<深入理解Android 卷一>和<深入理解Android卷二>不再出版,而知识的传播不应该由于纸质媒介的问题而中断,所以我将在CSDN博客中全文转发这两本书的全部内容. ...

  3. 【cl】字符串

    使用单引号(') 你可以用单引号指示字符串,就如同‘How are you’这样.所有的空白,即空格跟制表符都照原样保留 使用双引号(“) 在双引号中的字符串与单引号中的字符串的使用完全相同,例如“W ...

  4. 【剑指offer】Q19:二叉树的镜像

    def MirroRecursively(root): # root is None or just one node, return root if None == root or None == ...

  5. JAVA进阶-泛型

    >泛型:泛型指代了參数的类型化类型,一般被用在接口.类.方法中 >作用:用来确定參数的范围,在书写代码的时候提前检查代码的错误性 >泛型的声明,下面给出类声明,依此类推: class ...

  6. LNMP 架构 上传文件

    修改PHP上传文件大小限制的方法   修改PHP上传文件大小限制的方法1. 一般的文件上传,除非文件很小.就像一个5M的文件,很可能要超过一分钟才能上传完.但在php中,默认的该页最久执行时间为 30 ...

  7. CodeForces 651C

    Description Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg s ...

  8. rehat7.X下postgresql 11编译安装

    文档目录结构: 一.准备 操作系统版本:rehat7.6 Postgresql:11.2 软件安装目录:/pgsql11/basedir 数据文件存放目录:/pgsql11data/ 11.2的下载地 ...

  9. golang iris html/temple

    在使用golang的模板语法的过程中遇见自动转义问题(或者以我的理解下发的富文本html代码不是template.html类型,而是string类型),需要强制转型 func unescaped(x ...

  10. mysql的递归(使用函数)

    getChildList: BEGIN #声明两个局部变量 ); ); #初始化局部变量 SET sTemp = ''; #调用cast函数将int转换为char SET sTempChd = roo ...