HDU 2059 龟兔赛跑

#include <stdio.h>

int main(){
	long L;
	long N,C,T;
	long VR,VT1,VT2;
	long p[102];
	long len;
	int i,j;
	double time,min,tmp[102];

	while(scanf("%ld",&L) != EOF){		
		scanf("%ld %ld %ld",&N,&C,&T);
		scanf("%ld %ld %ld",&VR,&VT1,&VT2);
		p[0] = tmp[0]=0;
		for(i = 1;i <= N;i++) 
			scanf("%ld",&p[i]);		
		p[N+1] = L;
		
		for(i = 1;i <= N+1;i++){			
			min = 0xffffff;
			for(j = 0; j < i; j++){
				len = p[i]-p[j];
				time = len > C ? (len-C+0.0)/VT2 + 1.0*C/VT1 : 1.0*len/VT1;
				time+=tmp[j];
				if(j) time+=T;
				if(min > time) min = time;
			}
			tmp[i] = min;
		}
		if(tmp[N+1] < 1.0*L/VR)
			printf("What a pity rabbit!\n");
		else
			printf("Good job,rabbit!\n");

	}
	return 0;
}


采用动态规划。如果有N个加油站,则算上起点和终点,全程可分为N+1个区间,共有N+2个端点。用数组tmp[]保存每个端点的最优值。乌龟到i端点的最短时间tmp[i]是所有经过此前端点j(j=0,1,...i)的值和本区间(p[i]-p[j])所耗时间和的最小值.

你可能感兴趣的:(C++,c,C#,J#)