Solution: luoguP1070 road game (DP)

 

Topic description

Xiaoxin is playing a simple computer game.

There is a circular road in the game, there are n robot factories on the road, and two adjacent robot factories are connected by a short road. Xiaoxin starts from a robot factory and numbers the n robot factories as 1~n in clockwise order. Because the road is circular, the nth robot factory and the first robot factory are connected by a road. together. Xiaoxin will also number the n sections of roads connecting the robot factories as 1~n, and stipulate that the i-th road connects the i-th robot factory and the i+1-th robot factory (1≤i≤n-1), and the n-th road A section of road connects the nth robot factory with the first robot factory.

During the game, in each unit time, some gold coins will appear on each section of the road, and the amount of gold coins will change with time, that is, the amount of gold coins that appear on the same section of the road in different unit times may be different. Xiaoxin needs the help of a robot to collect the gold coins on the road. The required robot must be purchased with some gold coins in the robot factory. Once the robot is purchased, it will walk along the circular road in a clockwise direction, walking once per unit time, that is, from the robot factory where it is currently located. The next robot factory next door, and collect all the gold coins on the road to Xiaoxin. For example, Xiaoxin buys a robot in robot factory i (1≤i≤n), and this robot will buy a robot from robot factory i. Start, walk clockwise on the road, the first walk will pass through road i to reach robot factory i+1 (if i=n, ​​the robot will reach the first robot factory), and put all the robots on road i Gold coins are collected for Xiaoxin. In the game, two or more robots cannot exist on the circular road at the same time, and each robot can walk on the circular road at most p times. When Xiaoxin buys a robot, he needs to set the number of walking times for the robot. The number of walking times can be any integer between 1 and p. When the robot on the road has walked the specified number of times, it will disappear automatically. Xiaoxin must immediately buy a new robot in any robot factory and set a new number of walking times for the new robot.

Here are some additional notes about the game:

  1. The game starts when Xiaoxin buys a robot for the first time.

  2. Buying the robot and setting the number of walks for the robot is done in an instant and does not take time.

  3. Purchasing a robot and walking the robot are two separate processes. The robot cannot be purchased while the robot is walking. The robot can only walk after purchasing the robot and setting the number of robot walks.

  4. The cost of purchasing a robot in the same robot factory is the same, but the cost of purchasing a robot in different robot factories is not necessarily the same.

  5. The gold coins spent to buy robots will be deducted from the gold coins collected by Xiaoxin at the end of the game, so Xiaoxin does not have to worry about the lack of gold coins and the inability to purchase robots during the game, which will cause the game to fail. Because of this, after the game is over, the amount of gold coins collected may be negative.

Now we know the number of gold coins that appear per unit of time on each road and the cost of purchasing robots in each robot factory. Please tell Xiaoxin that after m units of time, after deducting the cost of purchasing robots, Xiaoxin has the most How many coins can be collected.

Input and output format

Input format:

 

The first line contains 3 positive integers, n, m, p. The meaning is as described in the title.

The next n lines, each line contains m positive integers, separated by a space between each two integers, where the i-th line describes

Describes the number of gold coins in each unit time on No. i road (1≤the number of gold coins≤100), that is, the jth (1≤j≤m) number in the i-th row represents the i-th road in the jth unit time The number of coins that appear on the .

In the last line, there are n integers, separated by a space between each two integers, where the ith number represents the amount of gold coins that need to be spent to purchase robots in robot factory i (1≤gold coins≤100).

 

Output format:

 

A total of one line, including 1 integer, indicating that within m units of time, the purchase of the robot will be deducted

After spending gold coins, how many gold coins can Xiaoxin collect at most.

 

Input and output example

Input example #1: 
2 3 2
1 2 3
2 3 4
1 2
Sample output #1:
5

illustrate

【data range】

For 40% of the data, 2≤n≤40, 1≤m≤40.

For 90% of the data, 2≤n≤200, 1≤m≤200.

For 100% data, 2≤n≤1000, 1≤m≤1000, 1≤p≤m.

answer:

In fact, this problem can be solved in O(n^3)......

Since the robot can be bought in any factory, after step i, we only need to consider the number of gold coins obtained at this time, and do not care about its specific location.

So there is such an idea, let f[i] represent the maximum number of gold coins that can be obtained after taking i steps, and consider continuing to operate on this basis. Enumerate each factory, calculate the gold coins that can be obtained after taking k steps from here, and update f[i+k].

State transition equation:

 

                s=(j+k-1)%n;if(s==0) s=n; ctx+=c[s][i+k]; f[i+k]=maxn(f[i+k],f[i]+ctx-rbt[j]);   

       (i is how many steps have been taken, j is the jth factory, k is the number of steps to be taken, s is the position after taking k steps, ctx is the accumulated gold coins on the road, c[i][j] is Gold coins at time j on the i-th road, rbt[i] is the cost of buying robots in the i-th factory)

 

#include<bits/stdc++.h>
#define MAXN 1005
#define INF 1e9
using namespace std;
int n,m,p,ctx=0;
int f[MAXN],c[MAXN][MAXN],rbt[MAXN];
int maxn(int a,int b)
{
    return a>b?a:b;
}
void DP()
{
    for(int i=0;i<=m;i++)
        for(int j=1;j<=n;j++)
            {
                ctx=0;
                for(int k=1;k<=(m-i>p?p:m-i);k++)
                    {
                        int s=(j+k-1)%n;
                        if(s==0) s=n;
                        ctx+=c[s][i+k];
                        f[i+k]=maxn(f[i+k],f[i]+ctx-rbt[j]);
                    }
            }
}
intmain ()
{
    scanf("%d%d%d",&n,&m,&p);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&c[i][j]);      
    for(int i=1;i<=n;i++)
        scanf("%d",&rbt[i]);
    for(int i=1;i<=m;i++)
        f[i]=-INF;
    f[0]=0;        
    DP();
    printf("%d\n",f[m]);                
    return 0;
}
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325047748&siteId=291194637