【DP+线段树】 hdu3698 Let the light guide us

34 篇文章 0 订阅
8 篇文章 0 订阅

Let the light guide us

http://acm.hdu.edu.cn/showproblem.php?pid=3698


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

题意:在每行上选一个点,每个点都要各自对应的代价,同时相邻两行的点要满足 |j-k|≤f(i,j)+f(i+1,k)。问最小代价是多少。

题解:明显的DP题目,而且阶段也很明显,每一行是一个阶段,但是这个是现场赛的题目,明显不会那么水。的确,数据太大了(2<=N<=100,1<=M<=5000)。

所以我们要进行优化,根据转移方程dp[i][j]=min{dp[i-1][k]}+t[i][j](|j-k|≤f(i,j)+f(i+1,k)),如何快速求k就是关键了。

我们可以想到dp[i][j]的最小值一定在以j为中心的半径f(i,j)的区间内,而dp[i-1]][k]可以更新以k为中心的半径f(i-1,k)的区间内的最小值。

这个就可以用到线段树来维护。

ps:注意这个题目min不能写宏定义,写宏定义就是超时

#include<cstdio>
#include<cstring>
using namespace std;
//#define min(a,b) ((a)<(b)?(a):(b))
#define inf 1073741823
#define N 105
#define M 5005
int t[N][M];//the time cost of building a magic tower in cell(i,j).
int f[N][M];//the scale of magic flows in cell(i,j).
int dp[N][M];
int summ[M<<2],add[M<<2];
inline int min(int a,int b)
{
    return a<b?a:b;
}
void push_down(int idx)
{
    if(add[idx]!=inf)
    {
        add[idx<<1]=min(add[idx<<1],add[idx]);
        add[idx<<1|1]=min(add[idx<<1|1],add[idx]);
        summ[idx<<1]=min(summ[idx<<1],add[idx<<1]);
        summ[idx<<1|1]=min(summ[idx<<1|1],add[idx<<1|1]);
        add[idx]=inf;
    }
}
void build(int l,int r,int idx)
{
    summ[idx]=inf;
    add[idx]=inf;
    if(l==r) return;
    int mid=(l+r)>>1;
    build(l,mid,idx<<1);
    build(mid+1,r,idx<<1|1);
}
void update(int a,int b,int w,int l,int r,int idx)
{
    if(a<=l&&r<=b)
    {
        add[idx]=min(w,add[idx]);
        summ[idx]=min(add[idx],summ[idx]);
        return;
    }
    int mid=(l+r)>>1;
    push_down(idx);
    if(a<=mid) update(a,b,w,l,mid,idx<<1);
    if(mid<b)  update(a,b,w,mid+1,r,idx<<1|1);
    summ[idx]=min(summ[idx<<1],summ[idx<<1|1]);
}
int query(int a,int b,int l,int r,int idx)
{
    if(a<=l&&r<=b)
       return summ[idx];
    int mid=(l+r)>>1;
    push_down(idx);
    int res=inf;
    if(a<=mid) res=min(res,query(a,b,l,mid,idx<<1));
    if(mid<b)  res=min(res,query(a,b,mid+1,r,idx<<1|1));
    return res;
}
inline void scan(int &n)//输入加速
{
    char cc ;
    for(;cc=getchar(),cc<'0'||cc>'9';);
    n=cc-'0';
    for(;cc=getchar(),cc>='0'&&cc<='9';)
        n=n*10+cc-'0';
}
int main()
{
    int n,m;
    for(;scanf("%d%d",&n,&m)&&(n!=0||m!=0);)
    {
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j)
                scan(t[i][j]);
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j)
                scan(f[i][j]);
        for(int i=1;i<=m;++i)
            dp[1][i]=t[1][i];
        for(int i=2;i<=n;++i)
        {
            build(1,m,1);
            for(int j=1;j<=m;++j)
                update(j-f[i-1][j],j+f[i-1][j],dp[i-1][j],1,m,1);
            for(int j=1;j<=m;++j)
                dp[i][j]=query(j-f[i][j],j+f[i][j],1,m,1)+t[i][j];
        }
        int ans=inf;
        for(int i=1;i<=m;++i)
            ans=min(ans,dp[n][i]);
        printf("%d\n",ans);
    }
    return 0;
}

来源: http://blog.csdn.net/ACM_Ted


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值