码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-1700 Crossing River

时间:2017-08-08 13:50:39      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:print   job   ros   ber   代码   contain   esc   range   algorithm   

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-=2;
再次循环。直到n<4;

代码:
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6 int a[1200];
 7 int main(){
 8     int T;
 9     cin>>T;
10     while(T--){
11         int n;
12         cin>>n;
13         for(int i=1;i<=n;i++){
14             scanf("%d",a+i);
15         }
16         sort(a+1,a+1+n);
17         int sum=0;
18         while(n){
19             if(n==1){
20                 sum+=a[1];
21                 break;
22             }
23             else if(n==2){
24                 sum+=a[2];
25                 break;
26             }
27             else if(n==3){
28                 sum+=a[1]+a[2]+a[3];
29                 break;
30             }
31             else{
32                 sum+=min(a[2]+a[1]+a[n]+a[2],a[n]+a[1]+a[n-1]+a[1]);
33                 n-=2;
34             }
35         }
36         cout<<sum<<endl;
37     }
38     return 0;
39 }

 

POJ-1700 Crossing River

标签:print   job   ros   ber   代码   contain   esc   range   algorithm   

原文地址:http://www.cnblogs.com/ISGuXing/p/7306077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!