标签:
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
10
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 }
HDU 3698 Let the light guide us
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4743386.html