POJ 1700 Crossing River

Source: Internet
Author: User

Crossing River
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12585 Accepted: 4787

Description

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

Input

The first line of the input contains a single integer t (1 <= t <=), the number of test cases. Then T cases follow. The first line of all case contains N, and the second line contains n integers giving the time for each people to cross t He river. Each case was preceded by a blank line. There won ' t is more than, people and nobody takes more than, 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


The code is not difficult, the idea must be clear. Copy the idea of the great God on the Internet:

First, everyone's speed from small to large sort, first consider the simple situation, a person, their past can be. Two people, take the longest time, three people, the best is three people spend the sum of time (each let the fastest that the single boat back).

Four people or more than four, you need to consider different decisions to the slowest and slower people crossing the river. There are two kinds of decisions.

1. Always let the fastest individual boating. The fastest and slowest to cross the river, and then the fastest to draw back, the fastest and the slow crossing together, the fastest to come back. Time used is s[n]+s[1] + s[n-1] +s[1]

2. Let the fastest and the fastest cross the river first, the fastest row back, then the slowest and the slow crossing, let the next quick row back. The time used for this is s[2]+s[1]+s[n]+s[2]

Take the shortest time in both decisions.


Test instructions: N person, only one boat, everyone has a rowing speed, ask to let n individuals across the river, a crossing including two people go first, a person come back. When you take the most time between two people, the time of one person is what he uses. The shortest amount of time a person can use to finish a river.

Attached code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <stack>5#include <queue>6#include <map>7#include <Set>8#include <vector>9#include <cmath>Ten#include <algorithm> One using namespacestd; A Const DoubleEPS = 1e-6; - Const DoublePI = ACOs (-1.0); - Const intINF =0x3f3f3f3f; the Const intMOD =1000000007; - #definell Long Long - #defineCL (b) memset (A,b,sizeof (a)) - #defineMAXN 100010 +  - intT,n; + inta[1010]; A intsum; at intMinintAintb) - { -     returnA>b?b:a; - } - intMain () - { inscanf"%d",&T); -      while(t--) to     { +scanf"%d",&n); -CL (A,0); the          for(intI=1; i<=n; i++) *scanf"%d",&a[i]); $Sort (A +1, a+n+1);Panax Notoginsengsum=0; -          while(n) the         { +             if(n = =1) A             { thesum+=a[1]; +                  Break; -             } $             Else if(n==2) $             { -sum+=a[2]; -                  Break; the             } -             Else if(n==3)Wuyi             { thesum+=a[3]+a[1]+a[2]; -                  Break; Wu             } -             Else About             { $Sum+=min (a[2]*2+a[1]+a[n],a[1]*2+a[n]+a[n-1]); -n-=2; -             } -         } Aprintf"%d\n", sum); +     } the     return 0; -}

POJ 1700 Crossing River

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.