HDU3698 区间线段树+dp

来源:互联网 发布:你不知道的js中文pdf版 编辑:程序博客网 时间:2024/05/22 20:27


Let the light guide us

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


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


题目大意:给你n*m的点,每一个点有个时间还有个魔法值。每行选取一个点,相邻两行的点必须满足坐标差小于魔法值的和。

解题思路:1.dp[i][j]=Min(dp[i-1][k]+time[i][j])|j-k|≤f(i,j)+f(i-1,k) dp方程是这个样子,但是复杂度为n*m*m。利用线段树可以优化到n*m*logm.主要问题是k的取值范围有哪些?

2.将上面的式子转化可以得到 

j - f(i, j) <= k + f(i - 1, k) j >= k

k - f(i - 1, k) <= j + f(i, j) j < k

这两个式子的意义为如果区间[j - f(i, j), j + f(i, j)]和[k - f(i - 1, k), k + f(i - 1, k)]有相交的部分,这个k可以去更新dp[i][j].注意k的范围不是这两个区间相交的范围。这种理解是错误的。利用线段树去预处理[k - f(i - 1, k), k + f(i - 1, k)]这个区间内的最小值。之后直接查找就可以了。


#include <iostream>#include <cmath>#include <algorithm>#include <cstdio>#define lson (id<<1)#define rson ((id<<1)|1)#define mid ((l+r)>>1)const int INF = 0x3f3f3f3f;const int maxm = 5010;using namespace std;struct nod {    int sum,lazy;} tree[maxm*5];int n, m, q;int t[102][5002], dp[maxm], f[102][maxm];void push_up(int id){    tree[id].sum = min(tree[lson].sum, tree[rson].sum);    return;}void build_tree(int id,int l,int r){    if (l==r) {        tree[id].sum = INF;        tree[id].lazy = INF;        return;    }    build_tree(lson,l,mid);    build_tree(rson,mid+1,r);    push_up(id);    tree[id].lazy=INF;    return;}void push_down(int id,int l,int r){    tree[lson].sum = min(tree[lson].sum, tree[id].lazy);    tree[lson].lazy = min(tree[lson].lazy, tree[id].lazy);    tree[rson].sum = min(tree[rson].sum, tree[id].lazy);    tree[rson].lazy = min(tree[rson].lazy, tree[id].lazy);    tree[id].lazy=INF;    return;}void ins(int id,int l,int r,int ql,int qr,int tt){    if (ql<=l && r<=qr) {        tree[id].sum = min(tree[id].sum, tt);        tree[id].lazy = min(tree[id].lazy, tt);        return;    }    if (tree[id].lazy) {        push_down(id,l,r);    }    if (ql<=mid) {        ins(lson,l,mid,ql,qr,tt);    }    if (mid+1<=qr) {        ins(rson,mid+1,r,ql,qr,tt);    }    push_up(id);    return;}int query(int id,int l,int r,int ql,int qr){    if (ql<=l && r<=qr) {        return tree[id].sum;    }    if (tree[id].lazy) {        push_down(id,l,r);    }    int sum=INF;    if (ql<=mid) {        sum = min(sum, query(lson,l,mid,ql,qr));    }    if (mid+1<=qr) {        sum = min(sum, query(rson,mid+1,r,ql,qr));    }    return sum;}int main(){    while(cin >> n >> m) {        if(n == 0 && m == 0) {            break;        }        for(int i = 1; i <= n; ++i) {            for(int j = 1; j <= m; ++j) {                scanf("%d", &t[i][j]);                dp[j] = t[1][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_tree(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]);                ins(1, 1, m, l, r, dp[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[j] = query(1, 1, m, l, r) + t[i][j];            }        }        int ans = INF;        for(int i = 1; i <= m; ++i) {            ans = min(dp[i], ans);        }        printf("%d\n", ans);    }}



0 0
原创粉丝点击