poj 1700 Crossing River

本文探讨了经典的过河问题,给出了一个通过编程求解最少过河时间的算法实现。该算法采用排序和动态选择最优策略的方法来解决任意数量的人过河的问题。

原题: http://poj.org/problem?id=1700

//经典过河问题
//题目大意: 有N个人要过河,但是只有一艘船,每次最多只能坐两个人,而过河后,如果还有人需要渡河,就需要岸上某个人重新把船划回来
//          每个人单独划船的速度已知,两个人一起坐船的速度取决于速度较慢的那个人,问这n个人渡河需要的最少时间
//思路:如果有4个人渡河a,b,c,d,速度由小到大是 t1,t2,t3,t4,问题的关键在于如何把速度最慢的两个人运过去. 
//     共有两种方案: ①a,b先过河,a回;c,d过河,b回  时间:t2+t1+t4+t2
//                   ②a,d先过河,a回;a,c过河,a回   时间:t4+t1+t3+t1
//     每次过河取时间最短的方案,直到最后人数<=3人,我们便可以计算出最终结果 
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int t[1001]={0};
int cmp(int a,int b)
{
	return a<b;
}
int m(int a,int b)
{
	if(a<b)return a;
	else return b;
}
int main()
{
	int k;
	scanf("%d",&k);
	while(k--)
	{
		int n;
		scanf("%d",&n);
		int i;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&t[i]);
		}
		if(n==1){
			printf("%d\n",t[1]);
		}else{
			sort(t+1,t+n+1,cmp);
			int end=n;
			int min=0;
			while(end>=4){
				min=min+m(t[2]+t[1]+t[2]+t[end],t[end]+t[end-1]+2*t[1]);//每次取时间最短的方案 
				end=end-2;
			}
			if(end==3){
				min=min+t[1]+t[2]+t[3];
			}else if(end==2){
				min=min+t[2];
			}
			printf("%d\n",min);
		} 
	}
	return 0;
}          

### POJ 平台上的常见问题及解决方案 #### 贪心算法的应用场景 贪心算法是一种通过局部最优选择来达到全局最优解的方法。然而,这种方法并不总是能够提供正确的解答,尤其是在某些复杂情况下[^1]。为了有效利用贪心算法解决问题,必须深入理解问题的本质以及其适用条件。 #### POJ 上的经典贪心问题及其解决思路 以下是几个经典的 POJ 问题及其对应的解决方法: ##### 1. **POJ 1700 - Crossing River** 该问题是关于一组人过河的时间优化问题。核心在于如何合理安排人员渡河以最小化总时间。 - 解决方案:按照每次运送最快的人返回的原则设计贪心策略。具体实现可以通过模拟每一步的选择过程完成。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> times(n); for (auto &t : times) cin >> t; sort(times.begin(), times.end()); int time = 0; while (!times.empty()) { if (times.size() >= 2) { time += times[1]; // Send the two fastest people. if (times.size() > 2) { time += times.front(); // Return one of them back with the boat. } } else { time += times.back(); } times.erase(times.begin()); // Remove processed elements. } cout << time; } ``` ##### 2. **POJ 1017 - Packets** 此问题涉及将不同大小的数据包放入固定容量的盒子中,目标是最小化使用的盒子数量。 - 解决方案:采用优先队列或者排序的方式处理数据包尺寸,并尝试尽可能多地填充每个盒子。 ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int k, m; cin>>k>>m; priority_queue<int,vector<int>,greater<int>> pq; // Min heap to store packets' sizes. for(int i=0;i<m;i++){ int size; cin>>size; pq.push(size); } int boxesUsed = 0; while(!pq.empty()){ int currentBoxCapacity = k; bool filled=false; while(!pq.empty() && !filled){ if(pq.top()<=currentBoxCapacity){ currentBoxCapacity -= pq.top(); pq.pop(); } else{ break; } if(currentBoxCapacity==0 || pq.empty()) filled=true; } ++boxesUsed; } cout<<boxesUsed; } ``` ##### 3. **POJ 1065 - Wooden Sticks** 这是一个典型的区间覆盖问题,目的是找到最少的数量使得所有木棍被完全覆盖。 - 解决方案:先按长度升序排列所有的木棍,再依次遍历并记录当前区间的最大右端点位置。 ```cpp #include<stdio.h> struct Stick { double l,w;}; bool cmp(const Stick& a,const Stick& b){return a.l<b.l;} Stick sticks[5000]; int main(){ int N,i,j,k,cnt; scanf("%d",&N); for(i=0;i<N;i++)scanf("%lf%lf",&sticks[i].l,&sticks[i].w); sort(sticks,sticks+N,cmp); cnt=k=1; for(i=1;i<N;i++) if(sticks[k-1].w<sticks[i].w){cnt++;k=i;} printf("%d\n",cnt); } ``` #### 关键注意事项 在使用贪心算法时需要注意以下几点: - 验证所选策略是否满足无后效性和可证明的正确性。 - 对于无法直接验证的情况,考虑动态规划或其他更通用的技术作为备选方案。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值