文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Pku 1700

Pku 1700

时间:2011-04-10  来源:LuckyAnnika

题目连接:http://poj.org/problem?id=1700

                    Crossing River
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7333 Accepted: 2632

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 具体思路: 总体而言是贪心的思路 定义:iN表示人数      iTime[]表示每一个人过河所需要的时间    iMinTime表示过河需要的最少时间 首先将iTime[]从小到大排序 然后再来考虑iN的值的情况:   很容易考虑的是iN值为1、2、3的情况     1.  iN = 1  iMinTime = iTime[0]     2.  iN = 2  iMinTime = iTime[1]     3.  iN = 3  iMinTime = iTime[0] + iTime[1] + iTime[2]   再考虑 iN >= 4 的情况,有两种过河的方法:     1.最快和次快一起过  最慢和次慢一起过         iMinTime = iTime[0] + 2*iTime[1] + iTime[iN -1] (可以自己试着推一推)     2.最快和最慢一起过 然后最快的返回 和次慢一起过        iMinTime  = 2*iTime[0] + iTime[iN -2] + iTime[iN -1]
此题是经典的过桥问题,推荐大家看一篇文章:http://blogold.chinaunix.net/u/8780/showart.php?id=178361

具体代码实现:
   
#include <algorithm>   
#include <iostream>             
#include <cstdio>     

using namespace std;

const int MAX_PEOPLENUM = 1005;
int iTime[MAX_PEOPLENUM];

int solved(int iN)
{
        int iTime1,iTime2;

        if (iN == 1)
                return iTime[0];
        else if (iN == 2)
                return iTime[1];
        else if (iN == 3)
                return iTime[0] + iTime[1] + iTime[2];
        else
        {
                iTime1 = iTime[0] + 2*iTime[1] + iTime[iN-1];
                iTime2 = 2*iTime[0] + iTime[iN-2] + iTime[iN-1];
                if (iTime1 < iTime2)
                        return (iTime[0] + 2*iTime[1] + iTime[iN-1] + solved(iN-2));
                else
                        return (2*iTime[0] + iTime[iN-2] + iTime[iN-1] + solved(iN-2));
        }
}

int main()
{
        int i;
        int iCase,iN;

        while (1 == scanf("%d",&iCase))
        {
                while (iCase--)
                {
                        scanf("%d",&iN);
                        for (i=0; i<iN; i++)
                                scanf("%d",&iTime[i]);

                        sort(iTime,iTime+iN);
                        
                        printf("%d\n",solved(iN));
                }
        }
        return 0;
}
相关阅读 更多 +
排行榜 更多 +
边境检察最后区域手机版下载

边境检察最后区域手机版下载

角色扮演 下载
酋长你别跑手游下载

酋长你别跑手游下载

休闲益智 下载
心动漫画app下载官方版

心动漫画app下载官方版

浏览阅读 下载