首页 > ACM题库 > HDU-杭电 > hdu 2490 Parade-动态规划-[解题报告]C++
2014
01-26

hdu 2490 Parade-动态规划-[解题报告]C++

Parade

问题描述 :

Panagola, The Lord of city F likes to parade very much. He always inspects his city in his car and enjoys the welcome of his citizens. City F has a regular road system. It looks like a matrix with n+1 west-east roads and m+1 north-south roads. Of course, there are (n+1)×(m+1) road crosses in that system. The parade can start at any cross in the southernmost road and end at any cross in the northernmost road. Panagola will never travel from north to south or pass a cross more than once. Citizens will see Panagola along the sides of every west-east road. People who love Panagola will give him a warm welcome and those who hate him will throw eggs and tomatoes instead. We call a road segment connecting two adjacent crosses in a west-east road a “love-hate zone”. Obviously there are m love-hate zones in every west-east road. When passing a love-hate zone, Panagola may get happier or less happy, depending on how many people love him or hate him in that zone. So we can give every love-hate zone a “welcome value” which may be negative, zero or positive. As his secretary, you must make Panagola as happy as possible. So you have to find out the best route —– of which the sum of the welcome values is maximal. You decide where to start the parade and where to end it.

When seeing his Citizens, Panagola always waves his hands. He may get tired and need a break. So please never make Panagola travel in a same west-east road for more than k minutes. If it takes p minutes to pass a love-hate zone, we say the length of that love-hate zone is p. Of course you know every love-hate zone’s length.

The figure below illustrates the case in sample input. In this figure, a best route is marked by thicker lines.

输入:

There are multiple test cases. Input ends with a line containing three zeros.
Each test case consists of 2×n + 3 lines.

The first line contains three integers: n, m and k.(0<n<=100,0<m<=10000, 0<=k<=3000000)

The next n+1 lines stands for n + 1 west-east roads in north to south order. Each line contains m integers showing the welcome values of the road’s m love-hate zones, in west to east order.

The last n+1 lines also stands for n + 1 west-east roads in north to south order. Each line contains m integers showing the lengths (in minutes) of the road’s m love-hate zones, in west to east order.

输出:

There are multiple test cases. Input ends with a line containing three zeros.
Each test case consists of 2×n + 3 lines.

The first line contains three integers: n, m and k.(0<n<=100,0<m<=10000, 0<=k<=3000000)

The next n+1 lines stands for n + 1 west-east roads in north to south order. Each line contains m integers showing the welcome values of the road’s m love-hate zones, in west to east order.

The last n+1 lines also stands for n + 1 west-east roads in north to south order. Each line contains m integers showing the lengths (in minutes) of the road’s m love-hate zones, in west to east order.

样例输入:

2 3 2
7 8 1
4 5 6
1 2 3
1 1 1
1 1 1
1 1 1
0 0 0

样例输出:

27

Description

Panagola, The Lord of city F likes to parade very much. He always inspects his city in his car and enjoys the welcome of his citizens. City F has a regular road system. It looks like a matrix with n+1 west-east roads and m+1 north-south roads. Of course, there are (n+1)×(m+1) road crosses in that system. The parade can start at any cross in the southernmost road and end at any cross in the northernmost road. Panagola will never travel from north to south or pass a cross more than once. Citizens will see Panagola along the sides of every west-east road. People who love Panagola will give him a warm welcome and those who hate him will throw eggs and tomatoes instead. We call a road segment connecting two adjacent crosses in a west-east road a “love-hate zone”. Obviously there are m love-hate zones in every west-east road. When passing a love-hate zone, Panagola may get happier or less happy, depending on how many people love him or hate him in that zone. So we can give every love-hate zone a “welcome value” which may be negative, zero or positive. As his secretary, you must make Panagola as happy as possible. So you have to find out the best route —– of which the sum of the welcome values is maximal. You decide where to start the parade and where to end it. 

When seeing his Citizens, Panagola always waves his hands. He may get tired and need a break. So please never make Panagola travel in a same west-east road for more than k minutes. If it takes p minutes to pass a love-hate zone, we say the length of that love-hate zone is p. Of course you know every love-hate zone’s length. 

The figure below illustrates the case in sample input. In this figure, a best route is marked by thicker lines. 

 

Input

There are multiple test cases. Input ends with a line containing three zeros.  Each test case consists of 2×n + 3 lines. 
The first line contains three integers: n, m and k.(0<n<=100,0<m<=10000, 0<=k<=3000000) 
The next n+1 lines stands for n + 1 west-east roads in north to south order. Each line contains m integers showing the welcome values of the road’s m love-hate zones, in west to east order. 
The last n+1 lines also stands for n + 1 west-east roads in north to south order. Each line contains m integers showing the lengths (in minutes) of the road’s m love-hate zones, in west to east order. 
 

Output

For each test case, output the sum of welcome values of the best route. The answer can be fit in a 32 bits integer.

 

 题目大意:有一个n*m的矩阵,只能沿着边走,只能往左、往右或往上走,在同一行只能沿一个方向走(走了左边就不能返回走右边了)。打横的边都有一个权值(可能为负数)和一个长度,每行走过的长度不能超过k,打竖的边没有权值和长度。先要从最下面的任意一个点开始,走到最上面的任意一个点,问最大权值和为多少(答案不超过2^32-1,虽然题目不是这么说的)。

思路:一看就是动态规划,每一行只和上一行的状态有关。因为习惯从小到大循环我们从上往下走,反正都一样。设dp[i][j]为走到第 i 行第 j 个点的最大权值(已往左往右走完),那么dp[i][j] = max(dp[i-1][x] + sum(welcome[i][y])),distance(x, y) ≤ k,y in [x, i]。其中distance和sum(welcome[i][y])可以预处理出来(如sum[i]代表1~i的和,distance(i, j) = sum[j] – sum[i],i ≤ j),平均到处理每个dp[i][j]身上时间复杂度为O(1)。但是这样计算dp数组,时间复杂度高达O(nm²)。

现假设我们从左到右走,那么dp[i][j] = max(dp[i - 1][x] – sum_welcome[x] + sum_welcome[y]) = dp[i][j] = max(dp[i - 1][x] – sum_welcome[x]) + sum_welcome[y],那么对每一个j,所用的dp[i - 1][x] – sum_welcome[x]都是一样的,这里很容易能想到单调队列优化(如果你知道单调队列的话)。每次把队列末尾小于dp[i - 1][j] – sum_welcome[j]弹出,把队头distance(i, x) > k的弹出,队头就是最佳的dp[i - 1][x] – sum_welcome[x]。优化完时间复杂度为O(nm),已经是读入数据的复杂度了。(这里不介绍单调队列)

PS:可恶这题居然不让人在线非要我把整个矩阵一起读进来……

 

代码(1078MS,可恶啊C++又比G++快一倍):

#include <cstdio>
 #include <cstring>
 #include <iostream>
 #include <algorithm>
 using namespace std;
 
 const int MAXN = 110;
 const int MAXM = 10010;
 
 int wel[MAXN][MAXM], len[MAXN][MAXM];
 int sum_w[MAXM], sum_l[MAXM];
 int a[MAXM], b[MAXM], head, tail;
 int dp[2][MAXM];
 int n, m, k, cur;
 
 inline void insert(int x, int y) {
     while(head != tail && a[tail - 1] < x) --tail;
     a[tail] = x; b[tail] = y; ++tail;
 }
 
 void solve() {
     memset(dp, 0, sizeof(dp));
     cur = 0;
     for(int i = 0; i < n; ++i) {
         cur ^= 1;
         memset(dp[cur], 0, sizeof(dp[cur]));
 
         sum_w[0] = sum_l[0] = 0;
         for(int j = 1; j <= m; ++j) sum_w[j] = sum_w[j - 1] + wel[i][j];
         for(int j = 1; j <= m; ++j) sum_l[j] = sum_l[j - 1] + len[i][j];
         head = tail = 0;
         for(int j = 0; j <= m; ++j) {
             insert(dp[cur ^ 1][j] - sum_w[j], sum_l[j]);
             while(k < sum_l[j] - b[head]) ++head;
             dp[cur][j] = max(dp[cur][j], a[head] + sum_w[j]);
         }
 
         sum_w[m] = sum_l[m] = 0;
         for(int j = m; j > 0; --j) sum_w[j - 1] = sum_w[j] + wel[i][j];
         for(int j = m; j > 0; --j) sum_l[j - 1] = sum_l[j] + len[i][j];
         head = tail = 0;
         for(int j = m; j >= 0; --j) {
             insert(dp[cur ^ 1][j] - sum_w[j], sum_l[j]);
             while(k < sum_l[j] - b[head]) ++head;
             dp[cur][j] = max(dp[cur][j], a[head] + sum_w[j]);
         }
     }
 }
 
 int main() {
     while(scanf("%d%d%d", &n, &m, &k) != EOF) {
         if(n == 0 && m == 0 && k == 0) break;
         ++n;
         for(int i = 0; i < n; ++i)
             for(int j = 1; j <= m; ++j) scanf("%d", &wel[i][j]);
         for(int i = 0; i < n; ++i)
             for(int j = 1; j <= m; ++j) scanf("%d", &len[i][j]);
         solve();
         int ans = 0;
         for(int i = 0; i <= m; ++i) ans = max(ans, dp[cur][i]);
         printf("%d\n", ans);
     }
 }

 

解题转自:http://www.cnblogs.com/oyking/archive/2013/08/15/3260006.html


  1. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])