caioj 1082 动态规划入门(非常规DP6:火车票)

f[i]表示从起点到第i个车站的最小费用

f[i] = min(f[j] + dist(i, j)), j < i

动规中设置起点为0,其他为正无穷

(貌似不用开long long也可以)

#include<cstdio>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

typedef long long ll;
const int MAXN = 112;
ll a[MAXN], l[4], c[4], f[MAXN];
int n, s, e;

ll dist(ll n, ll m)
{
	ll x = a[m] - a[n];
	if(0 < x && x <= l[1]) return c[1];
	if(l[1] < x && x <= l[2]) return c[2];
	if(l[2] < x && x <= l[3]) return c[3];
	return 2e9;
}

int main()
{
	REP(i, 1, 4) scanf("%lld", &l[i]);
	REP(i, 1, 4) scanf("%lld", &c[i]);
	scanf("%d%d%d", &n, &s, &e);
	if(s > e) swap(s, e);
	REP(i, 2, n + 1) scanf("%d", &a[i]), f[i] = 2e9;
	
	f[s] = 0;
	REP(i, s, e + 1)
		REP(j, s, i)
			f[i] = min(f[i], f[j] + dist(j, i));
	printf("%lld\n", f[e]);

	return 0;
}

 

posted @ 2018-08-26 09:50  Sugewud  阅读(268)  评论(0编辑  收藏  举报