UVA 10037 Bridge



Description

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
ashlight. Only one
ashlight is available among the n people, so some sort of
shuttle arrangement must be arranged in order to return the
ashlight 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 all n people 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 rst 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 cases
will be separated by a blank line.
The rst line of output must contain the total number of seconds required for all n people 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 speci ed 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
ashlight 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


题意:这个题和之前的过河问题是一样的,只是这道题需要输出过河的过程。

思路:设由A—>B (过河)   ,需要求过河时间最短。

           每一组过完河之后还要在B选派一个人把手电筒送到A,就要选速度最快的人。每次过河耗费的时候都为两个人中速度慢的人所需的时间来计算,这样我们可以有两种方案:

     (1)最快的人带最慢的人过去,最快的人将手电筒送回来

注意:但是考虑到速度最快的人和速度最慢的人一起走也是按照最慢的人时间计算的,两个最慢的人走耗费的时间也是最慢的人过河的时间,这就会有时间的浪费

     (2)最快的两个人先过去,最快的人回来送手电筒,两个最慢的人一起过河。B处最快的人将手电筒送回,两个最快的人回B。

比较(1)和(2)方案的时间,选择两者中时间较短的。


注意:还要注意一下特殊情况,每次过河都是两个人过,如果总人数为1,2,3时的情况。




#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,m,a[1005];
		scanf("%d",&n);
		m=n;
		for(int i=0;i<n;i++)
			cin>>a[i];
		sort(a,a+n);
		int sum=0;
		while(n>=4)
		{
			int x1=2*a[0]+a[n-1]+a[n-2];
			int x2=2*a[1]+a[0]+a[n-1];
			if(x1<x2) sum+=x1;
			else sum+=x2;
			n-=2;
		}
		if(n==1) sum+=a[0];
		else if(n==2) sum+=a[1];
		else if(n==3) sum+=a[2]+a[1]+a[0];

	    cout<<sum<<endl;          //输出总时间

	    while(m>=4)
		{
			int x1=a[m-1]+a[m-2]+2*a[0];
			int x2=2*a[1]+a[0]+a[m-1];
			if(x1<x2)
			{
				cout<<a[0]<<" "<<a[m-1]<<endl;
				cout<<a[0]<<endl;
				cout<<a[0]<<" "<<a[m-2]<<endl;
				cout<<a[0]<<endl;
			}
			else
			{
				cout<<a[0]<<" "<<a[1]<<endl;
				cout<<a[0]<<endl;
				cout<<a[m-2]<<" "<<a[m-1]<<endl;
				cout<<a[1]<<endl;
			}
			m-=2;
		}
		if(m==1) cout<<a[0]<<endl;
		else if(m==2) cout<<a[0]<< " " <<a[1]<<endl;
		else
		{
			cout<<a[0]<<" "<<a[2]<<endl;
			cout<<a[0]<<endl;
			cout<<a[0]<<" "<<a[1]<<endl;
		}

		if(T) cout<<endl;
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值