P3399 Silk Road

Topic background

Zhang Qian in 138 BC, had been passing through numerous mission to the Western Regions. Strengthen friendly exchanges between the Han Dynasty and the Western countries. Since then, teams of camel caravan in this business a long road to travel, they crossed the mountains, with advanced technology to China to Central Asia, West Asia and Europe, where the spices, a good horse pass into our country. When people stare desolate Smoke in the desert, all caused reverie of past trade, cultural prosperity ......

Title Description (topic Link: https://www.luogu.org/problem/P3399 )

Small hamster with goods, to rest from China, start and end point of the Silk Road, including a total of N + 1 city, 0 is the starting point of the city of Chang'an, N number is the end of the city of Baghdad. It requires no more than M days must reach the finish line. The time of day can be continuous from one city to the next city. I-1 from city to city i is the distance Di.

As we all know, the continuous hurry is very hard, so little hamster may be in the city, you can have the following options:

  • Mobile: marching down a city

  • Rest: stay in the original city does not move

Desert changeable weather, when the weather is very bad, the forward will encounter many difficulties. We put the first day j M (1 <= j <= M) days of bad weather the value denoted by Cj. I-1 is moved from the city when the city i to j-marching day, takes the fatigue Di * Cj.

However, there is still little hamster right to choose, you can avoid relatively bad weather, rest is not going to consume fatigue value. Now he wants to know how much fatigue the minimum value of the entire trip to consumption.

Input Format

The first line of two integers N, M

A continuous integer N lines Dj

M lines each successive integer Cj

Output Format

An integer representing the minimum fatigue

Sample input and output

Input # 1
3 5
10
25
15
50
30
15
40
30
Output # 1
1125

Description / Tips

__ this question time 1s, memory limit 128M, because new evaluation machine speed is closer to NOIP evaluation machine speed, constant attention to the impact caused by the problem. __

1st day of rest

Day 2 0-> 1 fatigue value 10 × 30 = 300.

Day 31-> 2 fatigue value 25 × 15 = 375.

Day 4 Rest

Day 52-> 3 fatigue value of 15 × 30 = 450.

1 ≦ N ≦ M ≦ 1000

1 ≦ Di, Ci ≦ 1000

Analysis: Obviously the problem is dp, and the number of days the title fatigue has two parameters, the introduction of an array f, f [i] [j] indicates the j-th days to reach minimum fatigue city i

Can be obtained a state equation: f [i] [j] = min (f [i] [j-1] + 0, f [i-1] [j-1] + C [j] * D [i]);

There is Initialization: f [i] [i] = f [i-1] [j-1] + c [i] * d [i];

In summary f [n] [m] is the answer

c ++ code is as follows:

#include <bits / STDC ++ H.>
 the using  namespace STD;
 int D [ 1000 + . 5 ], C [ 1000 + . 5 ]; // D: distance, C: weather 
int F [ 1000 + . 5 ] [ 1000 + . 5 ] ; // the j i-th day in the fatigue cities minimum                        
int main () 
{ 
    int n-, m; // n-city, m day 
    CIN >> >> n- m;
     // fatigue Di * Cj , i-1 represents movement from city to city i is the j-th day headed 
    for ( int i = . 1 ; i <= n-; i ++) >> CIND[i];
    for(int i=1;i<=m;i++)cin>>C[i];
    
    //f[i][j]=min(f[i][j-1]+0,f[i-1][j-1]+C[j]*D[i]);
    for(int i=1;i<=n;i++){
        f[i][i]=f[i-1][i-1]+C[i]*D[i];//连续赶路不休息 
        for(int j=i+1;j<=m;j++){
        f[i][j]=min(f[i][j-1]+0,f[i-1][j-1]+C[j]*D[i]);    
        }
        
    } 
    cout<<f[n][m];
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/cstdio1/p/11330961.html