Parade - UVa 1427 dp+优先队列

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.

\epsfbox{p4327.eps}

Input 

There are multiple test cases. Input ends with a line containing three zeros.


Each test case consists of n + 3 lines.

The first line contains three integers: n , m and k . (0 < n$ \le$100, 0 < m$ \le$10000, 0$ \le$k$ \le$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 mintegers 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.

Sample Input 

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

Sample Output 

27

题意:找到一条路径使得最后的得分最高。

思路:dp[i][j]表示在i行j列的最高得分,以从左边往右走为例,dp[i][j]=max(dp[i][k]+sum[i][j]-sum[i][k])所以,用dp[i][k]-sum[i][k]作为值用优先队列维护,从右往左为dp[i][j]=max(dp[i][k]-sum[i][j]+sum[i][k])。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>
using namespace std;
struct node
{
    int val,k;
    bool operator<(const node A)const
    {
        return val<A.val;
    }
};
int n,m,K,dp[110][10010],val[110][10010],cost[110][10010];
priority_queue <node> qu;
node a;
void pop(int k)
{
    while(!qu.empty())
    {
        a=qu.top();
        if(abs(a.k-k)>K)
          qu.pop();
        else
          break;
    }
}
void solve(int i)
{
    while(!qu.empty())
      qu.pop();
    int j,k;
    for(j=0;j<=m;j++)
    {
        a.val=dp[i+1][j]-val[i][j];
        a.k=cost[i][j];
        qu.push(a);
        pop(cost[i][j]);
        a=qu.top();
        dp[i][j]=max(dp[i][j],a.val+val[i][j]);
    }
    while(!qu.empty())
        qu.pop();
    for(j=m;j>=0;j--)
    {
        a.val=dp[i+1][j]+val[i][j];
        a.k=cost[i][j];
        qu.push(a);
        pop(cost[i][j]);
        a=qu.top();
        dp[i][j]=max(dp[i][j],a.val-val[i][j]);
    }
}
int main()
{
    int i,j,k,maxn;
    while(~scanf("%d%d%d",&n,&m,&K) && n+m+K>0)
    {
        n++;
        for(i=1;i<=n;i++)
           for(j=1;j<=m;j++)
           {
               scanf("%d",&val[i][j]);
               val[i][j]+=val[i][j-1];
           }
        for(i=1;i<=n;i++)
           for(j=1;j<=m;j++)
           {
               scanf("%d",&cost[i][j]);
               cost[i][j]+=cost[i][j-1];
           }
        memset(dp,0,sizeof(dp));
        for(i=n;i>=1;i--)
               solve(i);
        maxn=0;
        for(i=0;i<=m;i++)
           maxn=max(maxn,dp[1][i]);
        printf("%d\n",maxn);
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值