您的位置:首页 > 其它

UVA 10037 - Bridge

2013-05-31 12:33 721 查看

Problem D: 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 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 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 cases will be separated by a blank line.

The first 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 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个人过河,按速度从小到大排列。
有两种最优策略供选择:①最快和次快过去,最快回;最慢和次慢过去,
次快回,t=s[1]+s[0]+s[n-1]+s[1]。②最快和最慢过去,最快回;最快和次快过去,
最快回,t=s[n-1]+s[0]+s[n-2]+s[0]。选择两者中用时较少的一个策略执行。
如此便将最慢和次慢送过河,对剩下n-2个人循环处理。注意当n=1、n=2、n=3时
直接相加处理即可。
#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include <cstring>
#include <queue>
using namespace std;
#define eps 1e-20
#define MAXN 1050
#define outstars cout << "*********" << endl;
int a[MAXN];
int x[MAXN] , y[MAXN] , z[MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(x , 0 , sizeof(x));
memset(y , 0 , sizeof(y));
memset(z , 0 ,  sizeof(z));
int n;
scanf("%d",&n);
for(int i = 0 ; i < n ; i++)
{
scanf("%d",&a[i]);
}
sort(a , a + n);
int ans = 0;
int num = 0;
//outstars
while(n > 3)
{
if(a[1] * 2 > a[0] + a[n - 2])
{
ans += a[0] + a[n - 2] + a[n - 1] + a[0];
x[num] = a[0] ;
y[num] = a[n - 1];
z[num] = a[0];
num++;
x[num] = a[0] ;
y[num] = a[n - 2];
z[num] = a[0];
num++;

}
else
{
ans += a[0] + a[1] + a[n - 1] + a[1];
x[num] = a[0] ;
y[num] = a[1];
z[num] = a[0];
num++;
x[num] = a[n - 2] ;
y[num] = a[n - 1];
z[num] = a[1];
num++;
}
n -= 2;
}

if(n == 3)
{
ans += a[0] + a[1] + a[2];
x[num] = a[0];
y[num] = a[2];
z[num] = a[0];
num++;
}
else if(n == 2)
{
ans += a[1];
}
else if(n == 1)
{
ans += a[0];
}
printf("%d\n",ans);
for(int i = 0 ; i < num ; i++)
{
printf("%d %d\n",x[i] ,y[i]);
printf("%d\n",z[i]);
}
if(n != 1)printf("%d %d\n",a[0] , a[1]);
else printf("%d\n",a[0]);
if(T != 0)printf("\n");
}

return 0;
}
/*

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM 水题 思维