Hdustm8 let the light guide us DP + line segment tree Optimization

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3698

Let the light guide us Time Limit: 5000/2000 MS (Java/others) memory limit: 62768/32768 K (Java/Others)
Total submission (s): 821 accepted submission (s): 285


Problem descriptionplain of despair was once an existing ent 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 classes ages nearby are haunted by ghosts.

In order to stop those ghosts as soon as possible, panda the Archmage and Facer the great effect ect 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 !"

Wocould you mind helping them?
 
Inputthere are multiple test cases.

Each test case starts with a line ining 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 & lt; = fij & lt; = 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.
 
Outputfor each test case, output a line with a single integer, which is the minimum time cost to finish all magic towers.
 
Sample Input
3 59 5 3 8 78 2 6 8 91 9 7 8 60 1 0 1 21 0 2 1 10 2 1 0 20 0
 
Sample output
10
 
Source2010 Asia Fuzhou Regional Contest


Select one row, and the upper and lower rows must meet the requirements. | j-k | ≤ f (I, j) + f (I + 1, k). Ask the smallest cell and value.

Analysis: the obvious DP, DP [I] [J] indicates to select the J value in the I line. However, the transfer complexity requires n * m, and it will definitely time out.

We noticed | j-k | <= f (I, j) + f (I-1, k), then for the K of the I-1 row we updated [k-F (I-1, k), K + f (I-1, k)], for the I-row Query [J-f (I, j), J + f (I, j)], in this way, the requirements are met.

Therefore, use the line segment tree to maintain the minimum value of the query interval and update the interval value. In this case, the complexity is N * m * log (m )..

/** * @author neko01 *///#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <cstring>#include <string.h>#include <iostream>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <set>#include <map>using namespace std;typedef long long LL;#define min3(a,b,c) min(a,min(b,c))#define max3(a,b,c) max(a,max(b,c))#define pb push_back#define mp(a,b) make_pair(a,b)#define clr(a) memset(a,0,sizeof a)#define clr1(a) memset(a,-1,sizeof a)#define dbg(a) printf("%d\n",a)typedef pair<int,int> pp;const double eps=1e-8;const double pi=acos(-1.0);const int INF=0x7fffffff;const LL inf=(((LL)1)<<61)+5;const int N=105;const int M=5005;int a[N][M];int f[N][M];int dp[N][M];struct node{    int l,r;    int Min;    int col;}tree[M*4];void build(int x,int l,int r){    tree[x].l=l,tree[x].r=r;    tree[x].Min=INF;    tree[x].col=INF;    if(l==r) return;    int mid=(l+r)>>1;    build(x<<1,l,mid);    build(x<<1|1,mid+1,r);}inline void push_down(int x){    if(tree[x].col!=INF)    {        tree[x<<1].col=min(tree[x].col,tree[x<<1].col);        tree[x<<1|1].col=min(tree[x<<1|1].col,tree[x].col);        tree[x<<1].Min=min(tree[x].col,tree[x<<1].Min);        tree[x<<1|1].Min=min(tree[x].col,tree[x<<1|1].Min);        tree[x].col=INF;    }}void update(int x,int l,int r,int val){    if(tree[x].l==l&&tree[x].r==r)    {        tree[x].Min=min(tree[x].Min,val);        tree[x].col=min(tree[x].col,val);        return;    }    push_down(x);    int mid=(tree[x].l+tree[x].r)>>1;    if(r<=mid) update(x<<1,l,r,val);    else if(l>mid) update(x<<1|1,l,r,val);    else    {        update(x<<1,l,mid,val);        update(x<<1|1,mid+1,r,val);    }    tree[x].Min=min(tree[x<<1].Min,tree[x<<1|1].Min);}int query(int x,int l,int r){    if(tree[x].l==l&&tree[x].r==r)        return tree[x].Min;    push_down(x);    int mid=(tree[x].l+tree[x].r)>>1;    if(r<=mid) return query(x<<1,l,r);    else if(l>mid) return query(x<<1|1,l,r);    else return min(query(x<<1,l,mid),query(x<<1|1,mid+1,r));}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        if(n==0&&m==0) break;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                scanf("%d",&a[i][j]);                if(i==1) dp[1][j]=a[i][j];            }        }        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)                scanf("%d",&f[i][j]);        for(int i=2;i<=n;i++)        {            build(1,1,m);            for(int j=1;j<=m;j++)            {                int l=max(1,j-f[i-1][j]);                int r=min(m,j+f[i-1][j]);                update(1,l,r,dp[i-1][j]);            }            for(int j=1;j<=m;j++)            {                int l=max(1,j-f[i][j]);                int r=min(m,j+f[i][j]);                dp[i][j]=query(1,l,r)+a[i][j];            }        }        int ans=INF;        for(int i=1;i<=m;i++)            ans=min(ans,dp[n][i]);        printf("%d\n",ans);    }    return 0;}




Hdustm8 let the light guide us DP + line segment tree Optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.