Let the light guide us

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others)
Total Submission(s): 677    Accepted Submission(s): 226

Problem Description
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
 
Recommend
chenyongfu
 

题意:

要在N*M(n<=100.m<=5000)的矩形区域的每行的一个位置建灯塔。而在第i行的j列建塔要花费时间ti[i][j].建塔还必须满足一个条件。

如果本行在j列建塔。下行在k列建塔。那么必须满足|j-k|<=f[i][j]+f[i+1][k]。

f[i][j]由题目给出。

问每行建完塔花费的最小时间。

思路:

很容易想到一个动规方程。dp[i][j]=min(dp[i][j],dp[i-1][k]+ti[i][j])。dp[i][j]代表前面i-1行建好塔。第i行在j列建塔的最小花费。

可问题又来了。这个k怎么确定。。。。。

如果暴力枚举的话时间复杂度为O(n*m*m)。目测最好n,m取最大的时候大于9秒吧。这还是单组数据。。。

所以不得不找其它办法优化下。

这个估计就要点思维了。我也是看了别人的转化才恍然大悟的。

我们去掉绝对值可以得到两个方程。

j-f[i][j]<=k+f[i+1][k]。 j>=k。

k-f[i+1][k]<=j+f[i][j]。 j<k。

可以发现k的取值范围即为区间[j-f[i][j],j+f[i][j]]和[k-f[i+1][k],k+f[i+1][k]]相交的部分。

所以思路就清晰了。要求dp[i][j]只需要知道上一行和[j-f[i][j],j+f[i][j]]相交部分的最小值就可以了。

而这个值可以用线段树维护。时间复杂度降到了。O(n*m*log(m))这下就没问题了。

详细见代码:

#include<algorithm>
#include<iostream>
#include<sstream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=150;
const int maxm=5010;
int ti[maxn][maxm],f[maxn][maxm],minv[maxm<<2],lazy[maxm<<2];
int dp[maxm],n,m;
void btree(int L,int R,int k)
{
int ls,rs,mid; minv[k]=lazy[k]=INF;
if(L==R)
return ;
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
btree(L,mid,ls);
btree(mid+1,R,rs);
}
void pushdown(int k,int ls,int rs)
{
minv[ls]=min(minv[ls],lazy[k]);
minv[rs]=min(minv[rs],lazy[k]);
lazy[ls]=min(lazy[ls],lazy[k]);
lazy[rs]=min(lazy[rs],lazy[k]);
lazy[k]=INF;
}
void update(int L,int R,int l,int r,int k,int v)
{
int ls,rs,mid;
if(L==l&&R==r)
{
minv[k]=min(minv[k],v);
lazy[k]=min(lazy[k],v);
return;
}
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
if(lazy[k]!=INF)
pushdown(k,ls,rs);
if(l>mid)
update(mid+1,R,l,r,rs,v);
else if(r<=mid)
update(L,mid,l,r,ls,v);
else
{
update(L,mid,l,mid,ls,v);
update(mid+1,R,mid+1,r,rs,v);
}
minv[k]=min(minv[ls],minv[rs]);
}
int qu(int L,int R,int l,int r,int k)
{
int ls,rs,mid;
if(L==l&&R==r)
return minv[k];
ls=k<<1;
rs=ls|1;
mid=(L+R)>>1;
if(lazy[k]!=INF)
pushdown(k,ls,rs);
if(l>mid)
return qu(mid+1,R,l,r,rs);
else if(r<=mid)
return qu(L,mid,l,r,ls);
else
return min(qu(L,mid,l,mid,ls),qu(mid+1,R,mid+1,r,rs)); }
int main()
{
int i,j,l,r,ans; while(scanf("%d%d",&n,&m),n||m)
{
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&ti[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&f[i][j]);
for(i=1;i<=m;i++)
dp[i]=ti[1][i];
for(i=2;i<=n;i++)
{
btree(1,m,1);
for(j=1;j<=m;j++)
{
l=max(j-f[i-1][j],1);
r=min(j+f[i-1][j],m);
update(1,m,l,r,1,dp[j]);
}
for(j=1;j<=m;j++)
{
l=max(j-f[i][j],1);
r=min(j+f[i][j],m);
dp[j]=qu(1,m,l,r,1)+ti[i][j];
}
}
ans=INF;
for(i=1;i<=m;i++)
ans=min(ans,dp[i]);
printf("%d\n",ans);
}
return 0;
}

hdu 3698 Let the light guide us(线段树优化&简单DP)的更多相关文章

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

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

  2. 题解 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 ...

  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. HDU 3698 Let the light guide us

    Let the light guide us Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. ...

  5. BZOJ 3790 神奇项链(回文自动机+线段树优化DP)

    我们预处理出来以i为结尾的最长回文后缀(回文自动机的构建过程中就可以求出)然后就是一个区间覆盖,因为我懒得写贪心,就写了线段树优化的DP. #include<iostream> #incl ...

  6. 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 ...

  7. hdu 4117 GRE Words (ac自动机 线段树 dp)

    参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...

  8. HDU多校第三场 Hdu6606 Distribution of books 线段树优化DP

    Hdu6606 Distribution of books 题意 把一段连续的数字分成k段,不能有空段且段和段之间不能有间隔,但是可以舍去一部分后缀数字,求\(min(max((\sum ai ))\ ...

  9. Weak Pair---hud5877大连网选(线段树优化+dfs)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5877  题意:给你一颗树,有n个节点,每个节点都有一个权值v[i]:现在求有多少对(u,v ...

随机推荐

  1. 【原】iOS 获取当前和 前后n天的日期

    原文:http://www.cnblogs.com/A--G/p/4759810.html 一.获取当前日期需要调用一个简单的方法:[NSDate date];//获取现在的日期 -(NSString ...

  2. linuxC编程实战 my_server.c例子问题总结

    今天看linux C 编程实战的my_server例子时,敲到这段代码,对其父子进程关闭socket 进行close调用产生疑问 如图中标注的三个close socket,思考子进程通信结束 关闭自己 ...

  3. php配置虚拟主机的配置步骤(hosts、httpd.conf、vhosts.conf)1.配置本地的dns文件2.配置apache的主配置文件3.配置Apache的虚拟主机

    1.域名解析(DNS) 找到C:\Windows\System32\drivers\etc目录下的hosts文件,在里面进行添加对应的内容

  4. 设置session的生命周期(php)

    PHP中,Session变量保存在服务器端(默认以文件格式保存),而Session ID以cookie形式保存在客户端. 销毁session的方法有2种 第一种是通过程序 session_destor ...

  5. github 分支 合并

    Git如何进行分支管理?      1.创建分支      创建分支很简单:git branch <分支名>      2.切换分支      git checkout <分支名&g ...

  6. Java认证:JavaRunnable线程编写接口代码

    Java认证:JavaRunnable线程编写接口代码.JavaRunnable线程如何才能更好的适应目前的编程环境呢?下面我们就看看如何才能更好的进行相关环境.希望下面的文章对大家有所帮助.Java ...

  7. [r]Ubuntu Linux系统下apt-get命令详解

    Ubuntu Linux系统下apt-get命令详解(via|via) 常用的APT命令参数: apt-cache search package 搜索包 apt-cache show package ...

  8. 研究不定数量参数的函数并实现一个printf函数

    一.前提知识 1.如何传递参数(主函数) a.函数的参数是通过栈传递,而且是从右到左依次入栈 b.即使是char型变量,在传递参数时,也是占用两个字节,因为push操作是两个字节为单位的. c.sho ...

  9. 【Java】Java Socket编程(1)基本的术语和概念

    计算机程序能够相互联网,相互通讯,这使一切都成为可能,这也是当今互联网存在的基础.那么程序是如何通过网络相互通信的呢?这就是我记录这系列的笔记的原因.Java语言从一开始就是为了互联网而设计的,它为实 ...

  10. ios入门之c语言篇——基本函数——5——素数判断

    参数返回值解析: 参数: m:int,需要判断的值: 返回值: 0:非素数 1:素数 函数解析: 注意:函数没有对输入进行判断,请自己屏蔽非法输入 int prime(int m) { int tem ...