POJ - 1700 Crossing River

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
题意:N个人想过河,但是一条船只能坐两个人,且速度取决于慢的那个人,并且要有人把船划回来,也就是一次只能有一个人过河

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include<math.h>
using namespace std;
const int maxn = 2010;
typedef long long ll;
int a[maxn];
int main()
{
    int n, m;
    int t;
    cin.sync_with_stdio(false);
    cin >> n;
    while (n--)
    {
        cin >> m;
        for (int i = 1; i <= m; ++i)
            cin >> a[i];
        sort(a + 1, a + 1 + m);
        int ans = 0;
        while (m >= 4)
        {
            ans += min(a[1] + a[2] * 2 + a[m], a[1] * 2 + a[m - 1] + a[m]);
//在每次用最少花费时间的人进行运送和最慢的两个人只做一程过去再由第二快的把第一快的接回来,两者进行比较看那种花费时间少选哪种 m
-= 2; } if (m == 3)ans += a[3]+a[1] + a[2] ; if (m == 2)ans += a[2]; if (m == 1)ans += a[1]; cout << ans << endl; } return 0; }
 

猜你喜欢

转载自www.cnblogs.com/-113/p/12318588.html