UVa10037 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 allnpeople 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 containsn, followed bynlines 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 allnpeople 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.

Sample Input

1

4
1
2
5
10

Sample Output

17
1 2
1
5 10
2
1 2

思路:

当n=1时,只有一个人,直接过去

当n=2时,两个一起过。

当n=3时,设A,B,C三人,A 速度最快,那么方案为:

A B

A

A C

当n>3时, 所有人按速度从小到大排列,第一,二个为A,B,最后两个为X,Y,那么为了让X,Y过去,有两种方案:

方案一:

A B

A

X Y

B

总时间为A+2*B+Y

方案二:

A X

A

A Y

A

总时间为2*A+X+Y,

如果A+2*B+Y < 2*A+X+Y,那么选择第一种方案,否则第二种


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int s[1005];

int cmp(int a,int b){
    return a < b;
}

int main(){
//freopen("test.in","rt",stdin);
    int t,n,ans,e,i;
    cin >>t;
    while(t--) {
        cin >>n;
        memset(s,0,sizeof(s));
        for(i = 0;i < n;i++) {
            cin >>s[i];
        }
        sort(s,s+n,cmp);
        ans = 0,e = n%2 + 1;;
        if(n == 1) {
            ans = s[0];
        }
        else if(n == 2) {
            ans = s[1];
        }
        else{
            for(i = n-1;i > e;i -= 2) {
                ans += s[0] + s[i] + min(s[1]*2,s[i-1]+s[0]);
            }
            ans += (n%2)?s[0]+s[1]+s[2]:s[1];
        }
        cout <<ans <<endl;

        if(n == 1) {
            cout <<s[0] <<endl;
        }
        else if(n == 2) {
            cout <<s[0] <<" " <<s[1] <<endl;
        }
        else if(n == 3) {
            cout <<s[0] <<" " <<s[1] <<endl;
            cout <<s[0] <<endl;
            cout <<s[0] <<" " <<s[2] <<endl;
        }
        else {
            for(i = n-1;i > e;i -= 2) {
                if (s[1] * 2 < s[i-1] + s[0]) {
                    cout <<s[0] <<" " <<s[1] <<endl;
                    cout <<s[0] <<endl;
                    cout <<s[i-1] <<" " <<s[i] <<endl;
                    cout <<s[1] <<endl;
                }
                else {
                    cout <<s[0] <<" " <<s[i-1] <<endl;
                    cout <<s[0] <<endl;
                    cout <<s[0] <<" " <<s[i] <<endl;
                    cout <<s[0] <<endl;
                }
            }
            if(n%2) {
                cout <<s[0] <<" " <<s[1] <<endl;
                cout <<s[0] <<endl;
                cout <<s[0] <<" " <<s[2] <<endl;
            }
            else {
                cout <<s[0] <<" " <<s[1] <<endl;
            }
        }
        if(t) {
            cout <<endl;
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值