poj 3404 Bridge over a rough river(过桥问题)

编程入门 行业动态 更新时间:2024-10-28 17:27:15

poj 3404 Bridge over a rough river(<a href=https://www.elefans.com/category/jswz/34/1677179.html style=过桥问题)"/>

poj 3404 Bridge over a rough river(过桥问题)

题目链接:=3404


Bridge over a rough river
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4024 Accepted: 1644

Description

A group of N travelers (1 ≤ N ≤ 50) has approached an old and shabby bridge and wishes to cross the river as soon as possible. However, there can be no more than two persons on the bridge at a time. Besides it's necessary to light the way with a torch for safe crossing but the group has only one torch.

Each traveler needs ti seconds to cross the river on the bridge; i=1, ... , N (ti are integers from 1 to 100). If two travelers are crossing together their crossing time is the time of the slowest traveler.

The task is to determine minimal crossing time for the whole group.

Input

The input consists of two lines: the first line contains the value of N and the second one contains the values of ti (separated by one or several spaces).

Output

The output contains one line with the result.

Sample Input

4
6 7 6 5

Sample Output

29

Source

Northeastern Europe 2001, Western Subregion


 思路:

      n==1时,直接输出答案

      n==2时,输出最大值

      n==3时,输出三者和

      n>=4时,两种策略,均转化成4人时的情形求最优解。设4人过河速度为A<B<C<D,那么有两种策略:

      先AB过,A回,CD过,B回,即temp=A+2*B+D

      先AD过,A回,再AC过,A回,即temp=2*A+C+D(忘了这种情况)

然后取其中的最小值即可。也就是说,在2*B<A+C时选方案1,否则选方案2.


代码如下:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define PI acos(-1.0)
#define INF 0x3fffffff
//typedef long long LL;
//typedef __int64 LL;
const int M = 1017;
int main()
{int n;int a[M];int i;while(~scanf("%d",&n)){for(i = 1; i <= n; i++){scanf("%d",&a[i]);}if(n == 1){printf("%d\n",a[1]);continue;}sort(a+1,a+n+1);if(n == 2){printf("%d\n",a[2]);continue;}if(n == 3){printf("%d\n",a[1]+a[2]+a[3]);continue;}int sum = 0;while(n){if(2*a[2] < a[1]+a[n-1]){sum+=a[1]+2*a[2]+a[n];n-=2;}else{sum+=2*a[1]+a[n-1]+a[n];n-=2;}if(n <= 3)break;}if(n == 1)sum+=a[1];else if(n == 2)sum+=a[2];else if(n == 3){printf("%d\n",sum+a[1]+a[2]+a[3]);continue;}printf("%d\n",sum);}return 0;
}



更多推荐

poj 3404 Bridge over a rough river(过桥问题)

本文发布于:2024-02-12 00:14:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1684524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过桥   poj   Bridge   river   rough

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!