PAT - 1033 To Fill or Not to Fill (25)

介绍了一种算法用于计算从杭州到其他城市的最经济驾车路线,考虑到油箱容量限制及沿途不同加油站的油价变化。

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

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

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C~max~ (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; D~avg~ (<=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: P~i~, the unit gas price, and D~i~ (<=D), the distance between this station and Hangzhou, for i=1,…N. All the numbers in a line are separated by a space.

Output

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


【题目大意】
有了高速公路,从杭州开车到其他城市都很容易。但是因为汽车的油箱容量有限,我们在路上不得不不时地寻找加油站。不同的加油站油价可能不同。你被要求仔细地设计最便宜的路线。

【输入】
第一行:Cmax D Davg N
Cmax - 油箱最大容量
D - 杭州到目的地的距离
Davg - 每升油可以行驶的距离
N - 加油站的个数
接下去 N 行:Pi Di
Pi - 汽油单价
Di - 该加油站离杭州的距离
【输出】
最便宜的价格,保留两位小数。
最开始油量为 0,如果无法到达目的地,输出 The maximum travel distance = X
X - 可到达最远距离

【数据范围】
Cmax <=100
D <=30000
Davg <=20
N <=500
0 <= Pi
0 <= Di <= D <= 30000

【解题思路】
贪心。先读入数据,然后按照离起点(杭州)的距离从小至大排序,最后再加上终点的信息(距离 D,油价 0)。
大致思路是,记录当前所在加油站的油价,先假设加满,寻找可到达的距离内能否找到比当前加油站更便宜的油价。
1、如果可以,我们在当前加油站需要的油量足够到达更便宜那个加油站就可以了。
2、如果没有更便宜的,就在当前加油站加满,然后在可到达距离内找出相对较便宜的那个加油站开过去。
起点和终点都不用特殊处理。因为一开始可到达最远距离是 0,如果起点没有加油站,那么最开始就无法到达第一个点。终点的油价是 0,这样在行驶到终点之前的某个加油站时,会找到终点这个最便宜的加油站,然后油量加到恰好可以到达终点。这就是为什么最开始要加上终点信息的原因。
思路其实比较简单,应该都能想到。这题比较抠细节,具体的注释在代码中有写出。好像很久没写注释了。


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 500+10;
const int INF = 0x3f3f3f3f;
int C, D, R, N;
struct Node {
    int dis;
    double price;
}node[MAXN];
bool cmp(Node a, Node b) {
    return a.dis < b.dis;
}
int main() {
//  freopen("in.txt", "r", stdin);
    scanf("%d%d%d%d", &C, &D, &R, &N);
    for (int i = 0; i < N; i++)
        scanf("%lf %d", &node[i].price, &node[i].dis);
    node[N].dis = D;
    node[N].price = 0;
    sort(node, node+N, cmp);

    double cost = 0;    //花费 
    double cur = 0;     //当前油量 
    double poi = 0;     //可到达的最远距离 
    for (int i = 0; i <= N; i++) {
        if (poi < node[i].dis) break;   //如果到不了当前的点,跳出循环 
        int tag = -1;
        for (int j = i+1; j <= N; j++) {    //寻找可到达范围内是否有更便宜的加油站 
            if (node[j].dis > node[i].dis + R * C) break;   //假设加满都找不到更便宜的车站,跳出 
            if (node[j].price < node[i].price) {    //如果有更便宜的,记录一下
                tag = j;
                break;
            }
        }
        if (tag == -1) {    //找不到更便宜的加油站 
            double minn = INF;
            int ttag = -1; 
            for (int j = i+1; j <= N; j++) { //找一个虽然比当前更贵,但是价格相对比较低的 
                if (node[j].dis > node[i].dis + R * C) break;
                if (node[j].price < minn) {
                    minn = node[j].price; 
                    ttag = j;
                }
            }
            if (ttag == -1) {
                poi += R * C; //可到达范围内没有任何车站,在当前加满,然后已经是最远距离了 
                break;
            } else {
                cost += (C - cur) * node[i].price; //先在当前位置加满
                cur = C - (node[ttag].dis - node[i].dis) * 1.0 / R;
                i = ttag - 1; //然后去这个相对便宜的车站
                poi = node[ttag].dis;
            }
        } else {
            cur = (node[tag].dis - node[i].dis) * 1.0 / R - cur; //油量加到可以到达这个更便宜的车站
            cost += cur * node[i].price; 
            poi = node[tag].dis;    //可到达的最远距离就是这个车站 
            i = tag - 1;
            cur = 0;
        } 
    }
    if (poi < D) printf("The maximum travel distance = %.2lf\n", poi); 
    else printf("%.2lf\n", cost);

    return 0;
}

提交时间状态分数题目编译器耗时
2018/7/19 23:53:12答案正确251033C++ (g++)7 ms

这题之前上算法分析课有写过,不过数据没有这个严格。重写一遍发现那个时候的代码大概还是有 bug 的,写得头皮发麻。建议脑子要保持清醒,写注释可以帮自己理一理思路。复杂样例的好处是样例过了就能 AC(大概

import cv2 import numpy as np import ezdxf from ezdxf.math import Vec2 import math import os def gray_to_dxf_fixed_line(input_image, output_dxf, line_length_um=70, line_width_um=5, grid_factor=0.2, max_angle_deg=180): """ 将灰度图像转换为带定向线段的DXF文件 固定线段长度和宽度,方向基于灰度值 参数: input_image: 输入图像路径 output_dxf: 输出DXF文件路径 line_length_um: 线段长度(微米) line_width_um: 线段宽度(微米) grid_factor: 网格尺寸相对于线段长度的比例(0-1) max_angle_deg: 最大旋转角度() """ # 读取灰度图像 if not os.path.exists(input_image): raise FileNotFoundError(f"输入图像不存在: {input_image}") gray_img = cv2.imread(input_image, cv2.IMREAD_GRAYSCALE) if gray_img is None: raise ValueError(f"无法读取图像: {input_image}") height, width = gray_img.shape print(f"图像尺寸: {width}×{height}像素") # 创建DXF文档 doc = ezdxf.new(dxfversion="R2010") msp = doc.modelspace() # 添加自定义图层和属性 lines_layer = doc.layers.add("FILL_LINES") lines_layer.color = 7 # 白色 lines_layer.lineweight = int(line_width_um * 100) # 1单位=0.01mm # 计算网格尺寸 grid_size = int(line_length_um * grid_factor) if grid_size < 1: grid_size = 1 print(f"网格尺寸: {grid_size}像素, 线段长度: {line_length_um}微米") # 角度转换 max_angle_rad = math.radians(max_angle_deg) # 遍历网格单元 line_count = 0 for y in range(0, height, grid_size): for x in range(0, width, grid_size): # 获取网格区域 y_end = min(y + grid_size, height) x_end = min(x + grid_size, width) if y_end - y <= 0 or x_end - x <= 0: continue # 计算网格平均灰度值 grid = gray_img[y:y_end, x:x_end] avg_gray = np.mean(grid) # 计算旋转角度 angle = (avg_gray / 255.0) * 2 * max_angle_rad - max_angle_rad # 计算线段中心点 (DXF坐标系y轴朝上) center_x = x + (x_end - x) / 2 center_y = height - (y + (y_end - y) / 2) # 翻转y轴 # 计算线段端点 half_length = line_length_um / 2 cos_angle = math.cos(angle) sin_angle = math.sin(angle) start_x = center_x - half_length * cos_angle start_y = center_y - half_length * sin_angle end_x = center_x + half_length * cos_angle end_y = center_y + half_length * sin_angle # 创建DXF线段并添加到图层 line = msp.add_line( start=(start_x, start_y), end=(end_x, end_y), dxfattribs={'layer': 'FILL_LINES'} ) line_count += 1 # 保存DXF文件 doc.saveas(output_dxf) print(f"生成成功: 创建{line_count}条线段, 输出文件: {output_dxf}") return True # 示例用法 if __name__ == "__main__": # 输入输出设置 input_image = "input.jpg" # 输入灰度图像路径 output_dxf = "line_fill.dxf" # 输出DXF文件路径 # 线段参数 (单位: 微米) line_length_um = 0.007 # 线段长度 line_width_um = 0.005 # 线段宽度 # 调用转换函数 gray_to_dxf_fixed_line( input_image=input_image, output_dxf=output_dxf, line_length_um=line_length_um, line_width_um=line_width_um, grid_factor=0.0001, # 网格尺寸比例 max_angle_deg=160 # 最大旋转角度 )这段代码生成的CAD图纸线条过于稀疏
最新发布
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值