演算法›Ch7 貪婪演算法第 9 題/共 12 題
9. Greedy Algorithm、Priority Queue
#AL-07-009中Greedy AlgorithmPriority Queue
題組題幹(本題:9,共 3 小題)點擊展開
Consider a UAV (Unmanned Aerial Vehicle) with F unit of fuel travels from NCU to a destination "target" km away. There are n gas stations along the way. When the UAV refuels at a gas station, all the fuel of the gas station is transferred into the UAV.
Suppose that the UAV consumes one unit of fuel for every kilometer it travels. Let the position and fuel of a gas station indicate the distance between the gas station and NCU and its volume of fuel, respectively. Return the minimum number of refueling stops the UAV must make in order to reach its destination.
typedef struct {
int position;
int fuel;
} stop_info;
int Refuel(int target, int F, int n, stop_info s[]) {
int N_R = 0; // number of refuel
int i; //gas stations ID
stop_info X;
while (F < target) {
for (i=0; L1 ; ++i)
Q.push(s[i].fuel);
if (Q.empty()) return -1;
X=Q.pop();
F += X.fuel;
N_R++;
}
return N_R;
}
Consider Refuel algorithm above. Which of the following statements are true?
📄 中央114
▤完整推導請見《WH 資工筆記 · 演算法》Ch7 貪婪演算法