hdu3698——Let the light guide us

来源:互联网 发布:apache 文件服务器 编辑:程序博客网 时间:2024/06/11 23:20

Let the light guide us

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


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
 

Source
2010 Asia Fuzhou Regional Contest
 

Recommend

dp方程很明显, dp[i][j]表示在第i行第j列上造灯塔所花费的最少时间
dp[i][j] = min(dp[i - 1][k] + t[i][j])

直接做的复杂度是O(n * m * m),太大,所以我们用线段树优化,
|j-k|≤f(i,j)+f(i+1,k) 类似于两圆相交,(j, k是圆心,其他的是半径)所以利用区间更新的技巧可以将线段插入到线段树上,查询相交部分的最小值即可,但是如果直接按[i - f[i][j], i +f[i][j]]来建立线段树,log的常数太大还是会超时,所以我们每次更新查询都判断下,如果i - f[i][j] < 1, 就让左端为1,右端同理。
这样复杂度成功降到O(n * m * logm)

#include <map>#include <set>#include <list>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N = 110;const int M = 5010;const int inf = 0x3f3f3f3f;int dp[N][M], t[N][M], f[N][M];struct node{int l, r;int val;int add;}tree[M << 2];void pushdown(int p){if (tree[p].add != inf){tree[p << 1].val = min(tree[p << 1].val, tree[p].add);tree[p << 1 | 1].val = min(tree[p << 1 | 1].val, tree[p].add);tree[p << 1].add = min(tree[p << 1].add, tree[p].add);tree[p << 1 | 1].add = min(tree[p << 1 | 1].add, tree[p].add);tree[p].add = inf;}}void build (int p, int l, int r){tree[p].l = l;tree[p].r = r;tree[p].val = inf;tree[p].add = inf;if (l == r){return;}int mid = (l + r) >> 1;build(p << 1, l, mid);build(p << 1 | 1, mid + 1, r);}void update(int p, int l, int r, int val){if (l <= tree[p].l && tree[p].r <= r){tree[p].val = min(tree[p].val, val);tree[p].add = min(tree[p].add, val);return;}pushdown(p);int mid = (tree[p].l + tree[p].r) >> 1;if (r <= mid){update(p << 1, l, r, val);}else if (l > mid){update(p << 1 | 1, l, r, val);}else{update(p << 1, l, mid, val);update(p << 1 | 1, mid + 1, r, val);}tree[p].val = min(tree[p << 1].val, tree[p << 1 | 1].val);}int query(int p, int l, int r){if (tree[p].l >= l && r >= tree[p].r){return tree[p].val;}pushdown(p);int mid = (tree[p].l + tree[p].r) >> 1;if (r <= mid){return query(p << 1, l, r);}else if (l > mid){return query(p << 1 | 1, l, r);}else{return min(query(p << 1, l, mid), query(p << 1 | 1, mid + 1, r));}}int main(){int n, m;while(~scanf("%d%d", &n, &m)){if (!n && !m){break;}int l, r;for (int i = 1; i <= n; ++i){for (int j = 1; j <= m; ++j){scanf("%d", &t[i][j]);}}for (int i = 1; i <= n; ++i){for (int j = 1; j <= m; ++j){scanf("%d", &f[i][j]);}}for (int i = 1; i <= m; ++i){dp[1][i] = t[1][i];}int ans = inf;for (int i = 2; i <= n; ++i){build(1, 1, m);for (int j = 1; j <= m; ++j){l = max(1, j - f[i - 1][j]);r = min(m, j + f[i - 1][j]);update(1, l, r, dp[i - 1][j]);}for (int j = 1; j <= m; ++j){l = max(1, j - f[i][j]);r = min(m, j + f[i][j]);dp[i][j] = query(1, l, r) + t[i][j];// printf("dp[%d][%d] = %d\n", i, j, dp[i][j]);}}for (int i = 1; i <= m; ++i){ans = min(ans, dp[n][i]);}printf("%d\n", ans);}return 0;}


0 0
原创粉丝点击