POJ

来源:互联网 发布:淘宝佛珠刀具价格大全 编辑:程序博客网 时间:2024/06/14 18:05

Bridge over a rough river

Description

A group of N travelers (1 ≤ N ≤ 50) has approached an old and shabby bridge and wishes to cross the river as soon as possible. However, there can be no more than two persons on the bridge at a time. Besides it's necessary to light the way with a torch for safe crossing but the group has only one torch.

Each traveler needs ti seconds to cross the river on the bridge; i=1, ... , N (ti are integers from 1 to 100). If two travelers are crossing together their crossing time is the time of the slowest traveler.

The task is to determine minimal crossing time for the whole group.

Input

The input consists of two lines: the first line contains the value of N and the second one contains the values of ti (separated by one or several spaces).

Output

The output contains one line with the result.

Sample Input

46 7 6 5

Sample Output

29



题意:每次可以两个人过桥,过桥的时间为走的最慢的那个,然后让一个人回来,求最短的过桥时间。


解题思路:很有意思的一道题目,不知道是用了贪心的思想还是动态规划。我认为一开始是贪心的思考,然后找到最佳决策,然后再用dp的思想去实现,前面的i个人的最少时间,对后面肯定没有影响。首先对时间进行排序,一开始肯定是想让走得最快的人跟过去,这样回来的时候就可以省下很多时间,但是这样子会有一个问题,就是这边可能会有两个走得很慢的人,这样就会多花一倍的时间,实际上应该是判断是让这边走得最慢的两个过去,然后让对面走得最快的回来,还是让这边走得最快的和一个最慢的过去快,用一个min函数判断就好了。



#include <iostream>#include <stdio.h>#include <stdlib.h>#include <vector>#include <map>#include <string.h>#include <algorithm>#include <queue>#define INF 1<<29using namespace std;int N;int dp[100];int a[100];int main(){    while(~scanf("%d",&N)){        for(int i=1;i<=N;i++)                scanf("%d",&a[i]);        //排完序后a[1]为走的最快的        sort(a+1,a+N+1);        dp[1]=a[1];        dp[2]=a[2];        for(int i=3;i<=N;i++)             dp[i]=min(dp[i-1] + a[1] + a[i],dp[i-2] + a[1] + a[i] + 2*a[2]);        //前者的意思是当这边还有一个人的时候,肯定是让对面走得最快的回来        //后者的意思是当这边还有两个人(其中一个肯定是a[1])的时候,肯定是让对面走得最快(这个时候是a[2])的回来,然后让a[1]和a[i]过去,a[1]回来,再a[1]a[2]过去        printf("%d\n",dp[N]);    }    return 0;}







原创粉丝点击