hdu3698 Let the light guide us

148 篇文章 0 订阅
36 篇文章 0 订阅

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.

首先考虑暴力的dp,dp[i][j]=min{dp[i-1][k]||j-k|<=f[i][j]+f[i-1][k]}。时间复杂度O(nm^2)。
对后面的条件进行分析可以发现,j和k其实是可以分开的。j覆盖j-f[i][j]..j+f[i][j]区域,k覆盖k-f[i-1][k]..k+f[i-1][k]区域。如果两个区间有交,那么就可以用k更新j。
那么可以对每一行进行处理,用这一行的每一个数对他所覆盖的区间进行更新,在求解时对他所覆盖的区间进行查找。这一个区间修改区间查询的操作可以用线段树来实现,每个节点维护子树中的最小值。
时间复杂度降为O(nmlogm)。

#include<cstdio>
#include<cstring>
const int oo=0x3f3f3f3f;
int lson[2][20010],rson[2][30010],val[2][30010],tag[2][30010],c[110][5010],f[110][5010],n,m,tot[2];
int min(int x,int y)
{
    return x<y?x:y;
}
int max(int x,int y)
{
    return x>y?x:y;
}
int rd()
{
    int x=0;
    char c=' ';
    while (c==' '||c=='\n') c=getchar();
    while (c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x;
}
void build(int k,int p,int ll,int rr)
{
    if (ll==rr) return;
    int mid=(ll+rr)/2;
    lson[k][p]=++tot[k];
    build(k,lson[k][p],ll,mid);
    rson[k][p]=++tot[k];
    build(k,rson[k][p],mid+1,rr);
}
void down(int k,int p)
{
    if (tag[k][p]!=oo)
    {
        val[k][p]=min(val[k][p],tag[k][p]);
        tag[k][lson[k][p]]=min(tag[k][lson[k][p]],tag[k][p]);
        tag[k][rson[k][p]]=min(tag[k][rson[k][p]],tag[k][p]);
        tag[k][p]=oo;
    }
}
int find(int k,int p,int ll,int rr,int l,int r)
{
    down(k,p);
    if (ll==l&&rr==r) return val[k][p];
    int mid=(ll+rr)/2;
    if (r<=mid) return find(k,lson[k][p],ll,mid,l,r);
    if (l>mid) return find(k,rson[k][p],mid+1,rr,l,r);
    return min(find(k,lson[k][p],ll,mid,l,mid),find(k,rson[k][p],mid+1,rr,mid+1,r));
}
void modi(int k,int p,int ll,int rr,int l,int r,int x)
{
    down(k,p);
    if (ll==l&&rr==r)
    {
        tag[k][p]=x;
        return;
    }
    val[k][p]=min(val[k][p],x);
    int mid=(ll+rr)/2;
    if (r<=mid) modi(k,lson[k][p],ll,mid,l,r,x);
    else
    {
        if (l>mid) modi(k,rson[k][p],mid+1,rr,l,r,x);
        else
        {
            modi(k,lson[k][p],ll,mid,l,mid,x);
            modi(k,rson[k][p],mid+1,rr,mid+1,r,x);
        }
    }
}
int main()
{
    int i,j,k,p,q,x,y,z;
    while (scanf("%d%d",&n,&m)&&n)
    {
        for (i=1;i<=n;i++)
          for (j=1;j<=m;j++)
            c[i][j]=rd();
        for (i=1;i<=n;i++)
          for (j=1;j<=m;j++)
            f[i][j]=rd();
        tot[0]=tot[1]=1;
        memset(lson,0,sizeof(lson));
        memset(rson,0,sizeof(rson));
        build(0,1,1,m);
        build(1,1,1,m);
        memset(val,0,sizeof(val));
        memset(tag,0x3f,sizeof(tag));
        for (i=1;i<=n;i++)
        {
            memset(val[i&1],0x3f,sizeof(val[i&1]));
            memset(tag[i&1],0x3f,sizeof(tag[i&1]));
            for (j=1;j<=m;j++)
            {
                x=find(i&1^1,1,1,m,max(1,j-f[i][j]),min(m,j+f[i][j]))+c[i][j];
                modi(i&1,1,1,m,max(1,j-f[i][j]),min(m,j+f[i][j]),x);
            }
        }
        down(n&1,1);
        printf("%d\n",val[n&1][1]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值