POJ1700: Crossing River (cross-River problem), poj1700crossing

Source: Internet
Author: User

POJ1700: Crossing River (cross-River problem), poj1700crossing
POJ1700Question link: http://poj.org/problem? Id = 1700Time Limit:1000 MSMemory Limit:10000KB64bit IO Format:% I64d & % I64u

Description

A group of N people wishes to go into ss 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 started ss.

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

Question:

This is a problem of cross-river boat ride. There are two strategies in total.
① The fastest and fastest times are over, and the fastest time is back. The slowest and slow times are over, the second is fast, and the fastest and fast times are over, t1 = a [1] + a [0] + a [n-1] + a [1] + a [1]. ② The fastest and slowest time goes by, and the fastest time goes by; the fastest time goes by, the fastest time goes by, and the fastest time goes, t2 = a [n-1] + a [0] + a [1] + a [0] + a [N-2]. Select a strategy with less execution in both, Judge t1 and t2 size, only need to judge 2a [1] is greater than a [0] + a [N-2], in this way, the slowest and slow delivery across the river, the remaining N-2 individual cycle processing. Note: When n = 1, n = 2, and n = 3, add them directly.

# Include <iostream>
# Include <algorithm>
Using namespace std;
Int n, a [1006];
Int main ()
{
Int t, I;
Cin> t ;;
While (t --)
{
Int f = 0; // each time f is set to 0
Cin> n;
For (I = 1; I <= n; I ++)
Cin> a [I];
Sort (a, a + n + 1 );
While (n)
{
If (n = 1)
{
F + = a [1];
Break;
}
If (n = 2)
{
F + = a [2];
Break;
}
If (n = 3)
{
F + = a [1] + a [2] + a [3];
Break;
}
If (n> 3)
{
If (2 * a [2]> (a [1] + a [n-1])
F + = 2 * a [1] + a [n] + a [n-1];
Else
F + = 2 * a [2] + a [1] + a [n];
N = n-2; // attention Loop
}
}
Cout <f <endl;
}
Return 0;
}

Related Article

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.