HDU 3698 Let the light guide us
Let the light guide us
Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 369864-bit integer IO format: %I64d Java class name: Main
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?
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.
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
Source
解题:dp[i][j]表示第i行第j列放灯
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 105; 4 const int M = 5005; 5 const int INF = 0x3f3f3f3f; 6 struct node { 7 int minv,lazy; 8 } tree[M<<2]; 9 int T[N][M],F[N][M],dp[N][M]; 10 void build(int L,int R,int v) { 11 tree[v].lazy = INF; 12 tree[v].minv = INF; 13 if(L == R) return; 14 int mid = (L + R)>>1; 15 build(L,mid,v<<1); 16 build(mid+1,R,v<<1|1); 17 } 18 inline void pushdown(int v) { 19 if(tree[v].lazy < INF) { 20 tree[v<<1].lazy = min(tree[v<<1].lazy,tree[v].lazy); 21 tree[v<<1].minv = min(tree[v<<1].minv,tree[v<<1].lazy); 22 tree[v<<1|1].lazy = min(tree[v<<1|1].lazy,tree[v].lazy); 23 tree[v<<1|1].minv = min(tree[v<<1|1].minv,tree[v<<1|1].lazy); 24 tree[v].lazy = INF; 25 } 26 } 27 inline void pushup(int v) { 28 tree[v].minv = min(tree[v<<1].minv,tree[v<<1|1].minv); 29 } 30 void update(int L,int R,int lt,int rt,int val,int v) { 31 if(lt <= L && rt >= R) { 32 tree[v].lazy = min(tree[v].lazy,val); 33 tree[v].minv = min(tree[v].lazy,tree[v].minv); 34 return; 35 } 36 pushdown(v); 37 int mid = (L + R)>>1; 38 if(lt <= mid) update(L,mid,lt,rt,val,v<<1); 39 if(rt > mid) update(mid+1,R,lt,rt,val,v<<1|1); 40 pushup(v); 41 } 42 int query(int L,int R,int lt,int rt,int v) { 43 if(lt <= L && rt >= R) return tree[v].minv; 44 pushdown(v); 45 int mid = (L + R)>>1,ret = INF; 46 if(lt <= mid) ret = query(L,mid,lt,rt,v<<1); 47 if(rt > mid) ret = min(ret,query(mid+1,R,lt,rt,v<<1|1)); 48 pushup(v); 49 return ret; 50 } 51 int main() { 52 int n,m; 53 while(scanf("%d%d",&n,&m),n||m) { 54 for(int i = 1; i <= n; ++i) 55 for(int j = 1; j <= m; ++j) 56 scanf("%d",T[i] + j); 57 for(int i = 1; i <= n; ++i) 58 for(int j = 1; j <= m; ++j) 59 scanf("%d",F[i] + j); 60 for(int i = 1; i <= m; ++i) dp[1][i] = T[1][i]; 61 for(int i = 2; i <= n; ++i) { 62 build(1,m,1); 63 for(int j = 1; j <= m; ++j) 64 update(1,m,max(1,j - F[i-1][j]),min(j + F[i-1][j],m),dp[i-1][j],1); 65 for(int j = 1; j <= m; ++j) { 66 int tmp = query(1,m,max(1,j - F[i][j]),min(m,j + F[i][j]),1); 67 dp[i][j] = min(INF,tmp + T[i][j]); 68 } 69 } 70 int ret = INF; 71 for(int i = 1; i <= m; ++i) 72 ret = min(ret,dp[n][i]); 73 printf("%d\n",ret); 74 } 75 return 0; 76 }
夜空中最亮的星,照亮我前行