# Crossing the river (Nanyang oj47) (Greedy)

Source: Internet
Author: User

Crossing the river (Nanyang oj47) (Greedy)
Cross-river problem time limit: 1000 MS | memory limit: 65535 KB difficulty: 5

Description

In the dark night, N travelers came to a narrow bridge without guardrails. If you do not use a flashlight, you will not dare to cross the bridge in any way. Unfortunately, N people only carry one flashlight, while the bridge is narrow enough to let two people pass simultaneously. If they bridge the bridge separately, the time required by N people is known. If the two bridge the bridge at the same time, the time required is the time required for a person to take action slowly. The problem is how to design a scheme to allow the N people to bridge the bridge as soon as possible.

Input
The first line is an integer T (1 <=t <= 20) indicating the number of groups of test data.
The first line of each group of test data is an integer N (1 <= N <= 1000), indicating that N people are crossing the river.
The second row of each group of test data is N integer Si, which indicates that it takes time for this person to cross the river. (0 Output
Output the minimum time needed to cross the river
Sample Input
`141 2 5 10`
Sample output
`17`
Source

POJ

```/* Train of Thought: There are two time-saving solutions to transport the slowest two people (b1, b2) to the opposite side: 1: Transport b2 across the river with the fastest one k1, a1 returns, then b2 is transported across the river, and a1 is returned. Time used: 2 * a [k1] + a [b1] + a [b2]. 2: Transport k1 and k2 of the fastest two to cross the river. k1 returns, b1 and b2 of the slowest two to cross the river, and b2 returns. Time used: a [k1] + 2 * a [k2] + a [b2]. Select the shortest time option to cross the river! */# Include

# Include

# Define deusing namespace std; int a [1010]; int fun (int n) {if (n <= 2) return a [n]; else if (n = 3) return a [1] + a [2] + a [3]; elsereturn fun (n-2) + min (a [n] + a [n-1] + 2 * a [1], a [1] + a [2] * 2 + a [n]);} int main () {int I, test, n; scanf ("% d", & test); while (test --) {memset (a, 0, sizeof (a); scanf ("% d", & n); for (I = 1; I <= n; I ++) scanf ("% d ", & a [I]); sort (a + 1, a + n + 1); printf ("% d \ n", fun (n);} return 0 ;}

```

Related Keywords:
Related Article

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.