POJ1700Crossing River(过河问题)

来源:互联网 发布:excel画出数据形状 编辑:程序博客网 时间:2024/06/10 17:38
Crossing River
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 16551 Accepted: 6283

Description

A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 seconds to cross.

Output

For each test case, print a line containing the total number of seconds required for all the N people to cross the river.

Sample Input

141 2 5 10

Sample Output

17

题意:n个人过河,一次最多过两人,时间取决于最慢的,求全部渡过河的所用最小时间。

思路:

通过总结分析可得当n为1时,用时a[1],当n为2时,取决于二者最大时间,当n为3时,最小时间为三人用时总和。

当n大于等于4时有两种渡河方案:1、最快和次快先过,最快回,然后当前最慢和当前次慢的过,次快回。time=a[2]+a[n]+a[1]+a[2].2、始终最快和当前最慢的过河,最快回。在取时间时,要从两者中选最小。time=a[n]+a[1]+a[n-1]+a[1].

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[1001];int min(int a,int b){    if(a>b)        return b;    else        return a;}int digui(int n){    if(n==1)        return a[1];    if(n==2)        return a[2];    if(n==3)        return a[1]+a[2]+a[3];    int sum=0,i;    for(i=2;i<=n;i++)    {        sum+=a[i];    }    return min(sum+a[1]*(n-2),a[1]+2*a[2]+a[n]+digui(n-2));}//最后一个return通过列举连续的例子比较好得出结论。int main(){    int t,n,i,j,temp;    scanf("%d",&t);    while(t--)    {        int min_time=0;        scanf("%d",&n);        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        sort(a+1,a+1+n);        min_time=digui(n);        printf("%d\n",min_time);    }    return 0;}
代码中的递归部分也可以用while循环处理 。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[1001];int min(int a,int b){    if(a>b)        return b;    else        return a;}int main(){    int t,n,i,j,temp;    scanf("%d",&t);    while(t--)    {        int time=0;        scanf("%d",&n);        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        sort(a+1,a+1+n);        time=0;        while(n>0)        {            if(n==1)            {                time+=a[1];                break;            }            else if(n==2)            {                time+=a[2];                break;            }            else if(n==3)            {                time+=a[1]+a[2]+a[3];                break;            }            else            {                time=time+min(a[n]+2*a[1]+a[n-1],2*a[2]+a[1]+a[n]);                n-=2;               // printf("%d\n",time);            }        }        printf("%d\n",time);    }    return 0;}
while循环对本题的解释更明确清晰。

原创粉丝点击