|Time Limit: 1000MS||Memory Limit: 10000K|
|Total Submissions: 7220||Accepted: 2590|
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.
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.
For each test case, print a line containing the total number of seconds required for all the N people to cross the river.
1 4 1 2 5 10
For four people,
A <= B <= X <= Y,
1st, A and B go together, then A come back, then X and Y go together, then B come back, then A and B go together. Sum = A + 3*B + Y; (or A + 2*B + Y, if A and B will stay in the original bank)
2nd, A go to the other bank with each of them. Sum = 2*A + B + X + Y; (or A + X + Y, if A and B will stay in the original bank)
Thus, compare these two ways to determine which way to go.
Source Code://Fri May 21 13:14:15 CDT 2010
using namespace std;
long long crossriver(vector<int> v, int T)
if (T == 1)
if (T == 2)
if (T == 3)
return v + v + v;
if (v + 3 * v + v[T - 1] > 2 * v + v + v[T - 2] + v[T - 1])
return 2 * v + v[T - 2] + v[T - 1] + crossriver(v, T - 2);
return v + 2 * v + v[T - 1] + crossriver(v, T - 2);
int main(int argc, const char* argv)
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
cin >> N;
cin >> T;
for (int i = 0; i < T; i++)
cin >> number;
cout << crossriver(v, T) << endl;