程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Pat(Advanced Level)Practice--1033(To Fill or Not to Fill)

Pat(Advanced Level)Practice--1033(To Fill or Not to Fill)

編輯:C++入門知識

Pat1033代碼

題目描述:

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00

比較復雜的貪心算法:關鍵是貪心策略的選擇 我們用maxdis表示加一次油所能行駛的最遠距離,那麼從目前到maxdis的范圍內搜索,會有以下幾種選擇的策略 1,在maxdis范圍內找到價格最低的加油站,則應加的油使它能跑到該加油站 2,在1的基礎上,如果在價格最低的加油站之前,存在第一個比當前加油站價格更低的加油站,則應該加油使之跑到 價格較低的加油站,然後再加油使之跑到價格最低的加油站 3,在價格最低的加油站加滿油,重復1,2就可以了 AC代碼:
#include
#include
#include

using namespace std;

typedef struct Gas
{
	float price;
	float distance;
}Gas;

bool cmp(const Gas &l,const Gas &r)
{
	if(l.distance v;
	Gas temp;
	float maxdis;
	scanf("%f%f%f%d",&c,&d,&a,&n);
	for(i=0;i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved