Problem Description
There are N towns on a line running east-west. The towns are numbered 1 through N, in order from west to east. Each point on the line has a one-dimensional coordinate, and a point that is farther east has a greater coordinate value. The coordinate of town i is Xi.
You are now at town 1, and you want to visit all the other towns. You have two ways to travel:
Walk on the line. Your fatigue level increases by A each time you travel a distance of 1, regardless of direction.
Teleport to any location of your choice. Your fatigue level increases by B, regardless of the distance covered.
Find the minimum possible total increase of your fatigue level when you visit all the towns in these two ways.
Constraints
- All input values are integers.
- 2≤N≤105
- 1≤Xi≤109
- For all i(1≤i≤N−1), Xi<Xi+1.
- 1≤A≤109
- 1≤B≤109
Input
The input is given from Standard Input in the following format:
N A B
X1 X2 … XNOutput
Print the minimum possible total increase of your fatigue level when you visit all the towns.
Example
Sample Input 1
4 2 5
1 2 5 7Sample Output 1
11
From town 1, walk a distance of 1 to town 2, then teleport to town 3, then walk a distance of 2 to town 4. The total increase of your fatigue level in this case is 2×1+5+2×2=11, which is the minimum possible value.Sample Input 2
7 1 100
40 43 45 105 108 115 124Sample Output 2
84
From town 1, walk all the way to town 7. The total increase of your fatigue level in this case is 84, which is the minimum possible value.Sample Input 3
7 1 2
24 35 40 68 72 99 103Sample Output 3
12
Visit all the towns in any order by teleporting six times. The total increase of your fatigue level in this case is 12, which is the minimum possible value.
题意:一行有 n 个点,每个点的坐标为 xi,已知每次从一个点走到另一个相邻的点花费为两点的距离乘以 A,从一个点传送到其他任意点花费为 B,问从 1 号点开始访问完所有点的最小花费
思路:贪心+线性dp
由于每次可以从一个点传送到其他任意点,那么每次只要考虑从第 i 个点到第 i+1 个点的最小花费即可
假设前 i 个点的最小花费为 dp[i],那么从第 i 个点到第 i+1 个点有两种行走方式:
- 行走:选择行走时,当前的花费为 (x[i]-x[i-1])*a,那么前 i 个点的花费就是 dp[i]=dp[i-1]+(x[i]-x[i-1])*a
- 传送:选择传送时,当前花费为 b,那么前 i 个点的花费就是 dp[i]=dp[i-1]+b
因此,取两种方式的最小值即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
LL x[N];
LL dp[N];
int main() {
LL n,a,b;
scanf("%lld%lld%lld",&n,&a,&b);
for(int i=1;i<=n;i++)
scanf("%lld",&x[i]);
dp[1]=0;
for(int i=2;i<=n;i++){
LL walk=dp[i-1]+(x[i]-x[i-1])*a;
LL teleport=dp[i-1]+b;
dp[i]=min(walk,teleport);
}
printf("%lld\n",dp[n]);
return 0;
}