POJ-1700 Crossing River---过河问题(贪心)

时间:2023-03-09 00:32:52
POJ-1700 Crossing River---过河问题(贪心)

题目链接:

https://vjudge.net/problem/POJ-1700

题目大意:

有N个人要渡河,但是只有一艘船,船上每次最多只能载两个人,渡河的速度由两个人中较慢的那个决定,小船来回载人直到所有人都渡河,求最短的渡河时间。

思路:

假设有速度最快的人a、速度次快的人b,速度最慢的人c,速度次慢的人d,把c和d送到终点考虑两种策略

1、 a和b出发,a回来,c和d出发,b回来

2、 a和c出发,a回来,a和d出发,a回来

只需要比较这两种策略的快慢,每次选择较快的策略,当起点的人数少于4时直接处理,即可得出结果。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int maxn = 1e4 + ;
typedef long long ll;
int T, n, m;
int a[maxn];
int main()
{
cin >> T;
while(T--)
{
cin >> n;
for(int i = ; i < n; i++)
{
cin >> a[i];
}
sort(a, a + n);
int i = n - ;
int ans = ;
for(; i >= ; i -= )
{
//策略一:最快的和次快的先过河(时间a[1]),然后最快的回来送灯(a[0]),最慢的和次慢的过河a[i],然后次快的回来送灯a[1];
int time1 = a[] + a[] + a[i] + a[];
//策略二:由最快的和最慢的过河(时间a[i]),然后最快的回来送灯(a[0]),次慢的和最慢的过河a[i-1],然后最快的回来送灯a[0];
int time2 = a[i] + a[] +a[i - ] + a[];
ans += min(time1, time2);
}
if(i == )ans += a[] + a[] + a[];
else if(i == )ans += a[];
else ans += a[];
cout<<ans<<endl;
}
return ;
}

进阶版:传送门