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

poj 1700 Crossing River

时间:2016-03-06 17:05:16      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

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


代码不难,思路必须清晰。复制一下网上大神的思路:

先把每个人的速度从小到大排序,先考虑简单的情况,一个人,自己过去就可以了。两个人,取时间最长的那个,三个人,最优的是三个人所用时间之和(每让最快的那个单独划船回来)。

四个人或四个以上,就要考虑不同的决策把最慢和次慢的人渡河。有两种决策。

1.始终让最快的单独划船。  最快的和最慢的一起渡河,然后最快的划回来,最快和次慢的一起渡河,最快的再回来。所用时间为 s[n]+s[1] + s[n-1] +s[1]

2.让最快和次快的先过河,最快的划回来,接着最慢和次慢的过河,让次快的划回来。  这样所用时间为 s[2]+s[1]+s[n]+s[2]

两种决策中取时间最短的。


题意:N个人,只有一条船,每个人都有一个划船速度,要求让N个人过河,一次渡河包括两个人先过去,一个人再回来。每次时间取两个人之间用时最多的,一个人的时候的时间就是他自己所用的。求让N个人过完河所用的最短时间。

 

附上代码:

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <stack>
 5 #include <queue>
 6 #include <map>
 7 #include <set>
 8 #include <vector>
 9 #include <cmath>
10 #include <algorithm>
11 using namespace std;
12 const double eps = 1e-6;
13 const double pi = acos(-1.0);
14 const int INF = 0x3f3f3f3f;
15 const int MOD = 1000000007;
16 #define ll long long
17 #define CL(a,b) memset(a,b,sizeof(a))
18 #define MAXN 100010
19 
20 int T,n;
21 int a[1010];
22 int sum;
23 int min(int a,int b)
24 {
25     return a>b?b:a;
26 }
27 int main()
28 {
29     scanf("%d",&T);
30     while(T--)
31     {
32         scanf("%d",&n);
33         CL(a, 0);
34         for(int i=1; i<=n; i++)
35             scanf("%d",&a[i]);
36         sort(a+1, a+n+1);
37         sum=0;
38         while(n)
39         {
40             if(n == 1)
41             {
42                 sum+=a[1];
43                 break;
44             }
45             else if(n==2)
46             {
47                 sum+=a[2];
48                 break;
49             }
50             else if(n==3)
51             {
52                 sum+=a[3]+a[1]+a[2];
53                 break;
54             }
55             else
56             {
57                 sum+=min(a[2]*2+a[1]+a[n],a[1]*2+a[n]+a[n-1]);
58                 n-=2;
59             }
60         }
61         printf("%d\n",sum);
62     }
63     return 0;
64 }

 

poj 1700 Crossing River

标签:

原文地址:http://www.cnblogs.com/pshw/p/5247803.html

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