程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj1700 Crossing River的相關內容介紹

poj1700 Crossing River的相關內容介紹

編輯:C++入門知識

poj1700 Crossing River的相關內容介紹


Crossing River   Time Limit:1000MS   Memory Limit:10000K Total Submissions:12585   Accepted:4787  

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

Source

POJ Monthly--2004.07.18  
  題意:n個人一條船,所有人要過河;船每次只能載兩個人,用時按最慢的算;求所有人過河的最小時間。   分析:兩種情況;設最小的和第二小的為m1,m2,當前
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 100010

int T,n;
int a[1010];
int sum;

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        CL(a, 0);
        for(int i=0; i1; i-=2)
        {
            if(m2*2

最大的和第二大的為n1,n2;   1、m2+m2 2、m2+m2>m1+n2;m1和n1先過河,然後m1回來,m1和n2過河,m1回來;   另外還要注意奇數的情況。  
 
   

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved