UVa10037 Bridge

来源:互联网 发布:空燃比传感器数据 编辑:程序博客网 时间:2024/05/20 19:46



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;}



0 0
原创粉丝点击