POJ 1700 Crossing River

Description

A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 seconds to cross.

Output

For each test case, print a line containing the total number of seconds required for all the N people to cross the river.

Sample Input

1
4
1 2 5 10

Sample Output

17
题目意思是有N个人过河,只有一条船,每个人过河的时间都不一样,如果两个人一起过河,那么过河的时间应当按消耗时间长的算,如果这N过人都过到河的另一边,问你最少应该耗时多少?
对于过河,有几种方案比较快:
对于耗时,先从小到大排序 cost[0]<cost[1]....<cost[n-1];
方案一:
	先让耗时最小的两个过河,耗时cost[1];
	耗时最小的回来,耗时cost[0];
	耗时最大的两个过河,耗时cost[i];
	耗时第二小的回来,耗时cost[1];
方案二:
	耗时最小的和耗时最大和过河,耗时cost[i];
	耗时最小的回来,耗时cost[0];
	耗时第小的和耗时第二大的过河,耗时cost[-1];
	耗时最小的回来,耗时cost[0];
这样过河,如果最后剩3个,耗时肯定为这三个的和,如果还有二个,耗时为大的那个,如果还有一个,耗时肯定为那个喽。
 
 
LANGAUGE:C++
CODE:
#include<iostream>
#include<algorithm>

using namespace std;

#define maxn 1001

int main()
{
	int cas,cost[maxn];
	cin>>cas;
	while(cas--)
	{
		int n,i,sum=0;
		cin>>n;
		for(i=0;i<n;i++)
			cin>>cost[i];

		sort(cost,cost+n);

		for(i=n-1;i>2;i-=2)
		{
			int solve_1=cost[1]+cost[0]+cost[i]+cost[1];
			int solve_2=cost[i]+cost[0]+cost[i-1]+cost[0];
			if(solve_1>solve_2)
				sum+=solve_2;
			else sum+=solve_1;
		}
		if(i==2)sum+=cost[0]+cost[1]+cost[2];
		else if(i==1)sum+=cost[1];
		else if(i==0)sum+=cost[0];

		cout<<sum<<endl;
	}
	return 0;
}


	
	

你可能感兴趣的:(Integer,ini,input,each,Go,output)