Wednesday, September 1, 2010

poj_1700_Crossing_River.cpp

Problem Links:

poj1700,

Problem:

Crossing River
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 7220
Accepted: 2590
Description
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.
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
1
4
1 2 5 10
Sample Output
17
Source
POJ Monthly--2004.07.18
 

Solution:


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

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

No comments :