UVA10037 Bridge【贪心】

n people wish to cross a bridge at night. A group of at most two people may cross at any time, andeach group must have a flashlight. Only one flashlight is available among the n people, so some sort ofshuttle 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 theslower member. Your job is to determine a strategy that gets all n people across the bridge in theminimum time.

Input

The input begins with a single positive integer on a line by itself indicating the number of the casesfollowing, each of them as described below. This line is followed by a blank line, and there is also ablank 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 caseswill be separated by a blank line.

  The first line of output must contain the total number of seconds required for all n people to crossthe bridge. The following lines give a strategy for achieving this time. Each line contains either one ortwo integers, indicating which person or people form the next group to cross. (Each person is indicatedby the crossing time specified in the input. Although many people may have the same crossing timethe 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


问题链接UVA10037 Bridge

问题简述:(略)

问题分析

  过桥和过河有异曲同工之妙,实际上可以说是一回事。过桥需要带手电筒,需要送回手电筒,同时只能两人过桥。过河只能坐船,需要送回船,,每条船只能坐两人

  这两个问题,同样用贪心法来解。有人过得快,有人过得慢,只能由过得快的人去带过得慢的人,才能最节省时间。

  过河的情况可以分为以下几种:

  1.一个人过,那过桥的时间就是那个人的过时间;

  2.两个人过,那过的时间就是慢的那个人的过时间;

  3.三个人过,开始时间最短的两个人过,然后时间最短的那个人回去接时间最长的那个人,若三人过时间为a、b和c则过河时间是a+b+c;

  4.四人或四人以上过时,就由时间最短的两个人送时间最长的两个人(反正这两人都要过,就先过)过;送两人过桥后,人数就减少两人;假设四人过河时间分别是a、b、c和d,并且a<=b<=c<=d,这时又分为两种情况过河:

  一是最快带最慢两人,即过情况是ac、a、ad和a,先把c和d先送过;

  二是最快和次快的带最慢和次慢,即过桥的情况是ab、a、cd和b,,先把c和d先送过

  这两种情况所花的时间有所不同,尽量选时间短的方案;

  5.按照以上的方案,优先处理第4步,直到所有人都过了桥。

程序说明

  这个问题与POJ2573 ZOJ1877 Bridge【贪心】是同一个问题,只是输入输出格式不同。

题记:(略)

参考链接POJ1700 Crossing River【贪心】


AC的C++语言程序如下:

/* UVA10037 Bridge */

#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

const int N = 1000;
int a[N];

int main()
{
    int t, n;

    scanf("%d", & t);
    for(int i=1; i<=t ; i++) {
        int ans = 0, m;

        if(i != 1)
            printf("\n");

        scanf("%d", &n);
        for(int i=0; i<n; i++)
            scanf("%d", &a[i]);

        sort(a, a + n);

        // 计算过河需要的时间并且输出
        m = n;
        while(m >= 4) {
            ans += min(a[0] * 2 + a[m - 1] + a[m - 2], a[0] + a[1] * 2 + a[m - 1]);
            m -= 2;
        }
        if(m == 3)
            ans += a[0] + a[1] + a[2];
        else if(m == 2)
            ans += a[1];
        else if(m == 1)
            ans += a[0];
        printf("%d\n", ans);

        // 输出过河过程的具体时间
        while(n >= 4) {
            if(a[0] * 2 + a[n - 1] + a[n - 2] < a[0] + a[1] * 2 + a[n - 1])
                printf("%d %d\n%d\n%d %d\n%d\n", a[0], a[n-1], a[0], a[0], a[n-2], a[0]);
            else
                printf("%d %d\n%d\n%d %d\n%d\n", a[0], a[1], a[0], a[n-2], a[n-1], a[1]);
            n -= 2;
        }
        if(n == 3)
            printf("%d %d\n%d\n%d %d\n", a[0], a[1], a[0], a[0], a[2]);
        else if(n == 2)
            printf("%d %d\n", a[0], a[1]);
        else if(n == 1)
            printf("%d\n", a[0]);
    }

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值