poj 1700 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

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

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

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

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 }
时间: 2024-10-16 18:09:35

poj 1700 Crossing River的相关文章

POJ 1700 Crossing River(贪心)

V - Crossing River Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1700 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. There

[ACM] poj 1700 Crossing River (经典过河问题)

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10212   Accepted: 3855 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 arr

poj 1700 Crossing River C++/Java

http://poj.org/problem?id=1700 题目大意: 有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河.且过河时间为两个人中速度慢的,求n个人过河的最短时间. 思路: 贪心. 对于每次过河的,有两种情况: //最快和最慢过去,然后最快回来.在和次慢过去.最快回来 int action1=a[i-1] + a[0] + a[i-2] +a[0]; //最快和次慢过去,然后最快回来,在次慢和最慢过去,次慢回来 int action2=a[1] +a[

ACM学习历程——POJ 1700 Crossing River(贪心)

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. Eac

poj 1700 Crossing River(贪心)

分析:题意 源岸数量<=3  很好判断 3:num[0]+num[1]+num[2] 2:num[1] ,1:num[0] 源岸数量>3 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main(void) { int n,t ; int i,j; int num[1001]; cin>>

POJ 1700 cross river (数学模拟)

 Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10311   Accepted: 3889 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

1700 Crossing River

题目链接: http://poj.org/problem?id=1700 1. 当1个人时: 直接过河 t[0]. 2. 当2个人时: 时间为较慢的那个 t[1]. 3. 当3个人时: 时间为 t[0]+t[1]+t[2]. 4. 当4个以上的人时, 将t[0] 和 t[1]当作搬运工. 两种方案: - 最快和次快的过去, 最快的回来, 最慢和次慢的过去, 次快的回来, 这样就将最慢和次慢的送过去了. 时间: t[0]+2*t[1]+t[i] - 最快的依次送最慢和次慢的过去再回来, 时间: 2

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 arrange

BFS POJ 2150 Crossing Prisms

题目传送门 1 /* 2 BFS:这题我的做法是先找出所有草地,然后枚举所有两兄弟烧的地点进行BFS,然后去最小值 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-3 19:23:42 7 File Name :FZU_2150.cpp 8 *************************************************/