POJ 1700----Crossing River(过桥问题)

本文探讨了一群人利用一艘只能容纳两人的船渡河的问题,并提供了一个算法解决方案。该算法通过合理安排人员往返渡河的方式,以最小化所有人完成渡河所需的总时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

#include <stdio.h>  
#include <algorithm>      
using namespace std;      
#define N 1010
int a[N];  
int main()  
{ 
    int T,i,n,s;
    scanf("%d", &T);  
    while(T--)  
    {  
        scanf("%d", &n);  
        for(i=0;i<n;i++)  scanf("%d", &a[i]);  
        sort(a,a+n);  
        s=0;  
        while(n)  
        {  
            if(n==1)  
            {  
                s+=a[0];  
                break;  
            }  
            else if(n==2)  
            {  
                s+=a[1];  
                break;  
            }  
            else if(n==3)  
            {  
                s+=a[0]+a[1]+a[2];  
                break;  
            }  
            else  
            {  //模式1:A将Z送过桥,然后返回,再把Y送过桥,再返回;
	      //模式2:A和B先过桥,然后A返回,Y和Z过桥,然后B返回
                if(2*a[1]>a[0]+a[n-2]) s+=2*a[0]+a[n-1]+a[n-2];  //当2b>a+y时,使用模式一
                else s+=a[0]+a[n-1]+a[1]*2;  //使用模式2 
            }  
            n-=2;     
        }   
		printf("%d\n", s);    
    } 
	return 0;
} 


内容概要:本文档详细介绍了基于Python的在线二手电子产品回收系统的设计与实现。项目旨在通过构建一个可靠、安全、透明的平台,提高废旧电子产品的回收率,推动资源的合理再利用,提供安全可靠的交易平台,加强环保意识,促进二手市场的发展,并实现数据驱动的智能化服务。项目面临的主要挑战包括废旧电子产品的检测与评估、信息不对称与交易风险、市场需求的预测与定价、用户体验优化及平台的安全性与数据保护。解决方案涵盖智能化评估与回收定价、高效的二手产品处理流程、完善的售后保障体系、创新的市场需求分析、全程透明化与安全性保障以及定制化用户体验。系统采用微服务架构,包括用户管理、商品评估、交易管理、数据分析、支付与结算等模块。项目还涉及前端界面设计、API接口开发、数据库设计与实现、模型训练与优化、部署与应用等方面。 适合人群:具备一定编程基础,特别是对Python和Web开发有一定了解的研发人员,以及对二手电子产品回收和环保事业感兴趣的从业者。 使用场景及目标:①帮助用户方便地将闲置电子产品回收、交易或再利用,提高废旧电子产品的回收率;②通过智能化的数据分析为用户提供价格评估、市场需求分析等服务,提高回收效率;③提供安全可靠的交易平台,确保交易的公平性和安全性;④推动二手市场的健康发展,为消费者提供经济实惠的产品选择;⑤增强公众的环保意识,推动社会向绿色、低碳方向发展。 其他说明:本文档不仅提供了系统的功能模块设计、数据库表结构、API接口规范,还展示了具体代码实现和GUI界面设计,为开发者提供了全面的技术参考。此外,项目强调了数据安全和隐私保护的重要性,确保平台在运行过程中能够有效保护用户信息。项目未来改进方向包括增强模型的精准度、拓展国际市场、提供更多支付和融资选项、跨平台数据集成与分析、更加智能的回收流程以及强化社交化与社区功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值