过河游戏(算法详解+例题)

需求:划船过河,至少需要一个人划船,当两个人乘船时,时间由需要较长时间过河的决定。求最短的过河时间。

分析:由题设可知。最短时间肯定是用时较快的人来回跑。此时有两种情况。

首先假设A为时间最短的人,B为时间次短的人。Y为时间最长的人,Z为时间较长的人。

其中初始位置为此岸,到达位置为彼岸

此刻我们认为A,B已在彼岸

一.第一种情况

A回到此岸--时间b

YZ回到彼岸--时间z

B回到此岸--时间b

AB回到彼岸--时间b

总时间:a + b + b + z

二.第二种情况。

1.A回到此岸--时间a

将Y送过去--时间y

A再回到此岸--a

将Z送过去 -- z

总时间:a + a + y + z

证明:首先为什么非要两个人一组?因为可以两个两个一起过,也可以一个个的过河。如果是一个个的过求和就和两个两个一起过一样了。

那么为什么每次都要先算最重的两个人,因为他们迟早都是要过河的。为什么要是最重的和次重点。两个两个一起过河的时候应该保证两个之间差最小,才能最优。我们可以想象如果最后只剩一个了,那么剩的这一个是大好,还是小好,显然是小好。

例题:

题目链接点击打开链接

防丢失的贴题:

问题 G: Safe Passage

时间限制: 1 Sec   内存限制: 128 MB
提交: 24   解决: 11
[ 提交][ 状态][ 讨论版]

题目描述


A group of friends snuck away from their school campus, but now they must return from the main campus gate to their dorm while remaining undetected by the many teachers who patrol the campus. Fortunately, they have an invisibility cloak, but it is only large enough to cover two people at a time. They will take turns as individuals or pairs traveling across campus under the cloak (and by
necessity, returning the cloak to the gate if others remain). Each student has a maximum pace at which he or she is able to travel, yet if a pair of students are walking under the cloak together, they will have to travel at the pace of the slower of the two. Their goal is to have everyone back at the dorm as quickly as possible.
As an example, assume that there are four people in the group, with person A able to make the trip in 1 minute, person B able to travel in 2 minutes, person C able to travel in 7 minutes, and person D able to travel in 10 minutes. It is possible to get everyone to the dorm in 17 minutes with the following plan:
– A and B go from the gate to the dorm together (taking 2 minutes)
– A returns with the cloak to the gate (taking 1 minute)
– C and D go from the gate to the dorm together (taking 10 minutes)
– B returns with the cloak to the gate (taking 2 minutes)
– A and B go from the gate to the dorm together (taking 2 minutes)

输入

The input is a single line beginning with an integer, 2 ≤ N ≤ 15. Following that are N positive integers  that respectively represent the minimum time in which each person is able to cross the campus if alone; these times are measured in minutes, with each being at most 5 000. (It is a very large campus!)

输出

Output the minimum possible time it takes to get the entire group from the gate to the dorm.

样例输入

2 15 5

样例输出

15
附代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int a[55];
int main()
{
	//freopen("in.txt","r",stdin);
	int n;
	scanf("%d",&n);
	for(int i = 0; i < n; i ++)
    {
        scanf("%d",&a[i]);
    }
    sort(a, a + n);
    int ans = 0;
    int i;
    for(i = n - 1; i > 2; i = i - 2)
    {
        if(a[0] + a[0] + a[i] + a[i - 1] < a[0] + a[i] + a[1] + a[1])
        {
            ans += a[0] + a[0] + a[i] + a[i - 1];
        }
        else
        {
            ans += a[0] + a[i] + a[1] + a[1];
        }
    }
    if(i == 2)
    {
        ans += a[0] + a[i] + a[1];//人数为奇数,最快的人讲最后一个人护送过去,再加上第一步最快和次快的人过河
    }
    else if(i == 1)
    {
        ans += a[1];//人数为偶数,加上第一步最快和次快的人过河
    }
    else
    {
        ans += a[0];//只有一个人
    }
    printf("%d\n",ans);
	return 0;
}


为防丢失的同样代码的题http://poj.org/problem?id=3404

你可能感兴趣的:(贪心)