# UVa 10037-bridge

Source: Internet
Author: User

Main topic

In one night there are n people across the river, they have a flashlight, need to have a flashlight to cross the river, each time up to two people at the same time across the river, each time the river is equal to the speed of the slowest person's river crossing time, so that everyone across the river, spend the least amount of time?

Analysis

If only one person crosses the river, the total time to cross the river is the time when the man crosses the river. If it is two people crossing the river, then the total time for the river is slower than the person crossing the river time. If it is three people across the river, it takes time to a+b+c. When the number is equal to 4 o'clock, we let two slowest people cross the river each time, assuming that A and B represent the fastest and second fastest people, the speed of a,b,c and D is the slowest and the second slowest, the speed is c,d. The first strategy is to let A and B cross the river, a to return, C and D to cross the river, B to return, the time spent is 2*b+a+c. The second strategy is to let A and C cross the river, a returns, A and D cross the river, a returns, the time is spent 2*a+c+d. Compare the time spent on two strategies, and which time it takes to choose which strategy.

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define MAXN 1005
using namespace Std;
int a[maxn],q[maxn*2];
int m,n;
int Solve ()
{

Int total=0,i;
M=-1;
for (i=n-1; i>=3; i-=2)
{
int t1=2*a+a[i-1]+a[i];
int t2=2*a+a+a[i];
if (t1<t2)
{ br> m++;
Q[m]=a;
Q[m]=a[i];
m++;
Q[m]=a;
m++;
Q[m]=a;
Q[m]=a[i-1];
m++;
Q[m]=a;
Total+=t1;
}
Else
{
m++;
Q[m]=a;
Q[m]=a;
m++;
Q[m]=a;
m++;
Q[m]=a[i-1 ];
Q[m]=a[i];
m++;
Q[m]=a;
Total+=t2;
}

}
if (i==2)
{
TOTAL+=A+A+A;
m++;
Q[M]=A;
Q[M]=A;
m++;
Q[M]=A;
m++;
Q[M]=A;
Q[M]=A;
}
else if (i==1)
{
TOTAL+=A;
m++;
Q[M]=A;
Q[M]=A;
}
Else
{
TOTAL+=A;
m++;
Q[M]=A;
}
}
int main (void)
{
int t;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
for (int i=0; i<n; i++)
scanf ("%d", &a[i]);
Sort (a,a+n);
memset (q,0,sizeof (q));
printf ("%d\n", Solve ());
for (int i=0; i<=m; i++)
{
printf ("%d", q[i]);
if (Q[i])
printf ("%d", q[i]);
printf ("\ n");
}
if (t) printf ("\ n");
}
return 0;
}

UVa 10037-bridge

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.