【DP】过桥

D e s c r i p t i o n Description

T r a i n Train o f of T h o u g h t Thought

D P DP
通过测试别的数据,我们可以发现,来回奔波的只可能是速度最快的两个人,然后动态转移方程就是 m i n ( ) min(速度最快的人运送,速度第二快的人运送)

C o d e Code

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int n,t[1005],f[1005];//f[i]表示运送i个人要花费的最少时间
int main()
{
	scanf("%d",&n);
	for (int i=1; i<=n; ++i)
	 scanf("%d",&t[i]);
	sort(t+1,t+n+1);//排序后便于计算
	f[1]=t[1];
	f[2]=t[2];
	for(int i=3; i<=n; ++i)
		f[i]=min(f[i-1]+t[i]+t[1],f[i-2]+t[i]+t[2]*2+t[1]);
	printf("%d",f[n]);
}

猜你喜欢

转载自blog.csdn.net/LTH060226/article/details/89025760