## Thursday, March 17, 2011

### UVa_10037_Bridge.cpp

poj2573, uva10037, spoj06734(not passed),

## Problem D: Bridge

n people wish to cross a bridge at night. A group of at most two people may cross at any time, and each group must have a flashlight. Only one flashlight is available among the n people, so some sort of shuttle arrangement must be arranged in order to return the flashlight so that more people may cross. Each person has a different crossing speed; the speed of a group is determined by the speed of the slower member. Your job is to determine a strategy that gets all n people across the bridge in the minimum time.

### Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The first line of input contains n, followed by n lines giving the crossing times for each of the people. There are not more than 1000 people and nobody takes more than 100 seconds to cross the bridge.

### Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

The first line of output must contain the total number of seconds required for all n people to cross the bridge. The following lines give a strategy for achieving this time. Each line contains either one or two integers, indicating which person or people form the next group to cross. (Each person is indicated by the crossing time specified in the input. Although many people may have the same crossing time the ambiguity is of no consequence.) Note that the crossings alternate directions, as it is necessary to return the flashlight so that more may cross. If more than one strategy yields the minimal time, any one will do.

```1

4
1
2
5
10```

```17
1 2
1
5 10
2
1 2```

## Source Code:

//Fri May 21 15:11:23 CDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

long long crossriver(vector<int> v, int T)
{
if (T == 1)
{
return v;
}
if (T == 2)
{
return v;
}
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);
}
else
{
return v + 2 * v + v[T - 1] + crossriver(v, T - 2);
}
}

void display(vector<int> v, int T)
{
if (T == 1)
{
cout << v << endl;
return;
}
if (T == 2)
{
cout << v << " " << v << endl;
return;
}
if (T == 3)
{
cout << v << " " << v << endl;
cout << v << endl;
cout << v << " " << v << endl;
return;
}
if (v + 3 * v + v[T - 1] > 2 * v + v + v[T - 2] + v[T - 1])
{
cout << v << " " << v[T - 1] << endl;
cout << v << endl;
cout << v << " " << v[T - 2] << endl;
cout << v << endl;
display(v, T - 2);
}
else
{
cout << v << " " << v << endl;
cout << v << endl;
cout << v[T - 2] << " " << v[T - 1] << endl;
cout << v << endl;
display(v, T - 2);
}
}

int main(int argc, const char* argv[])
{
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
int N;
cin >> N;
for (int k = 0; k < N; k++)
{
if (k > 0) cout << endl;
int T;
cin >> T;
vector<int> v;
for (int i = 0; i < T; i++)
{
int number;
cin >> number;
v.push_back(number);
}
cout << crossriver(v, T) << endl;
display(v, T);
}
//fclose(stdin);
//fclose(stdout);
return 0;
}