POJ 1700 cross river (数学模拟)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1700 cross river (数学模拟)相关的知识,希望对你有一定的参考价值。


                                                                                                                          Crossing River
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10311   Accepted: 3889

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

1
4
1 2 5 10

Sample Output

17


思路:(假如 1-n个人时间time[n]。递增排列)
  • 仅仅有一个人的时候:sum=time[1];
  • 二个人的时候:       sum=time[1]+time[2]
  • 三的人的时候:       sum=time[1]+time[2]+time[3]
  • 重点!当大于四个人的时候。为了追求时间最短化。仅仅有两种运送方式:(1) 最快。次快去-->最快回--->最慢。次慢去-->次快   time[2]+time[1]+time[n]+time[n-1]+time[2]
  •                                                                                                          (2) 最快,最慢去-->最快回-->最快。次快去-->最快回     time[n]+time[1]+time[n-1]+time[1]

#include<cstdio>
#include<algorithm>

#define maxn 100001
using namespace std;
int time[maxn];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,sum=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",time+i);

        sort(time+1,time+n+1);

        while(n)
        {
            if(n==1)
            {
                sum+=time[1];
                n=0;
            }
            else if(n==2)
            {
                sum+=time[2];
                n=0;
            }
            else if(n==3)
            {
                sum+=time[1]+time[2]+time[3];
                n=0;
            }
            else
            {
                if(time[2]*2>=time[1]+time[n-1])
                    sum+=2*time[1]+time[n]+time[n-1];
                else
                    sum+=2*time[2]+time[1]+time[n];
                n-=2;
            }
        }

        printf("%d\n",sum);
    }

    return 0;
}


以上是关于POJ 1700 cross river (数学模拟)的主要内容,如果未能解决你的问题,请参考以下文章

poj-1700 crossing river

POJ-1700 Crossing River

poj1700 Crossing River

POJ1700----Crossing River

Crossing River POJ过河问题

Uva12230Crossing Rivers 数学