PAT1018 Public Bike Management

PAT甲级 1018 Public Bike Management DFS 【代码】PAT 1018 Public Bike Management DFS。 阅读详情

Public Bike Management

题目描述

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.
The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.
When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

Figure 1

Figure 1 illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3, we have 2 different shortest paths:
1. PBMC -> S1 -> S3. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1 and then take 5 bikes to S3, so that both stations will be in perfect conditions.
2. PBMC -> S2 -> S3. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

输入描述:

Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax (<= 100), always an even number, is the maximum capacity of each station; N (<= 500), the total number of stations; Sp, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci (i=1,…N) where each Ci is the current number of bikes at Si respectively. Then M lines follow, each contains 3 numbers: Si, Sj, and Tij which describe the time Tij taken to move betwen stations Si and Sj. All the numbers in a line are separated by a space.

输出描述:

For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0->S1->…->Sp. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp is adjusted to perfect.
Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge’s data guarantee that such a path is unique.

输入例子:
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

输出例子:
3 0->2->3 0

大体思路:

找出从PBMC到Sp的最短路径,如果最短路径有多条,找从PBMC送出的车最少的,需要返回的车最少的
本题使用深度优先搜索策略较dijkstra算法要简单。
题目的解必须满足以下三个条件中的任何一个即可:
1.在所有路径中选择最短的;
2.如果路径相等,则选择从PBMC中送出最少的;
3.如果路径相等且PBMC送出的车也相等,则选择带回最少的。
另外,在实现的具体细节的时候,要注意curSend(从PBMC送出的车)与curBack(带回的车,即多余的车)的更新。
在选择点加入路径集合过程中,要把该点调整为最佳状态。
(1)如果curBack(多余的车)+该点本来就有的车数<最佳状态车数,说明需要用携带的车数来弥补,使该点达到最佳状态,同时curBack(剩余的车数)也该更新为0。
携带的车数不断累加起来就是需要从PBMC送出的车,即为curSend。
(2)如果curBack(多余的车)+该点本来就有的车数>最佳状态车数,curBack(多余的车数)就要更新=curBack(多余的车数)+该点本来就有的车数-最佳状态车数。

#include<iostream>
#include<cstring>
#include<vector>
#define INF 0x6FFFFFFF
using namespace std;
int CMax, N, Sp, M;//每个站的自行车最大容量,N个自行车,Sp终点,M条路
int map[505][505];
int vis[505];
int capacity[505];
vector<int> curPath;
vector<int> minPath;
int curCost;
int minCost;
int curSend;
int minSend;
int curBack;
int minBack;
bool isChose;
void DFS(int start)
{
    if (curCost > minCost)
    {
        return;
    }
    if (start == Sp)
    {
        isChose = false;
        if (minCost > curCost)
        {
            isChose = true;
        }
        else if (minCost == curCost)
        {
            if (minSend > curSend)
            {
                isChose = true;
            }
            else if (minSend == curSend)
            {
                if (minBack > curBack)
                {
                    isChose = true;
                }
            }
        }
        if (isChose)
        {
            minCost = curCost;
            minSend = curSend;
            minBack = curBack;
            minPath = curPath;
        }
    }
    for (int i = 1; i <= N; i++)
    {
        if (!vis[i] && map[start][i])
        {
            int lastCurCost = curCost;
            int lastCurSend = curSend;
            int lastCurBack = curBack;
            curCost += map[start][i];
            if (capacity[i]+curBack<(CMax / 2))
            {
                curSend += (CMax / 2) - (capacity[i]+curBack);
                curBack = 0;
            }
            else
            {
                curBack= (capacity[i]+curBack)-CMax/2;
            }
            vis[i] = 1;
            curPath.push_back(i);
            DFS(i);
            vis[i] = 0;
            curCost = lastCurCost;
            curSend = lastCurSend;
            curBack = lastCurBack;
            curPath.pop_back();
        }
    }
}
int main()
{
    while (scanf("%d%d%d%d",&CMax,&N,&Sp,&M)!=EOF && (CMax|| N||Sp||M))
    {
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        memset(capacity,0,sizeof(capacity));
        curPath.clear();
        minPath.clear();
        minCost = INF;
        minSend = INF;
        minBack = INF;
        for (int i = 1; i <= N; i++)
        {
            scanf("%d",&capacity[i]);
        }
        int x, y, Cost;
        while (M--)
        {
            scanf("%d%d%d",&x,&y,&Cost);
            map[x][y] = map[y][x] = Cost;
        }
        curPath.push_back(0);
        vis[0] = 1;
        DFS(0);
        printf("%d %d",minSend,minPath[0]);
        for (int i = 1; i < minPath.size(); i++)
        {
            printf("->%d",minPath[i]);
        }
        printf(" %d\n",minBack);

    }
    system("pause");
    return 0;
}
PAT甲级1018【Public Bike Management】 dfs 先附上题目链接~https://www.patest.cn/contests/pat-a-practise/1018 分析:求0点到sp点的最短距离,在此基础上要求从0点调出去调节的车辆最少,并且调回的车辆也最少。有几个坑点:比如调整是一次完成的,不能用经过后面站的车辆去补前面缺失的车,因此前面缺少的车只能靠更前面剩下的车去补。由上,dfs遍历所有经过的站,每次更新当前所剩的车和缺少的车,更新操... 阅读详情

相关推荐

PAT-ADVANCED1018——Public Bike Management

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED 原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805489282433024 题目描述: 题目翻译: 1018 公共自行车管理 杭州的公共自行车服务给来自世界各地的游客带来了便利...

清风阁 452

PAT 甲级 1018 Public Bike Management

PAT 甲级 1018 Public Bike Management 题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and ...

努力不脱发的程序媛 223

PAT 1018. Public Bike Management (30)

题目说明:有公共自行车网,从原点0出发到目标点sp,要把所有点的自行车数都置为half-full,找到最优路径,优先级依次为:路径长度最短,send最少,back最少。 算法:最短路径,dfs回溯。求出所有0到sp的路径,然后比较找出最优解。

彼岸大鱼的专栏 3602

PAT 1018 Public Bike Management

参考链接:柳婼 の blog—1018. Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;题目就不贴了。 &amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;个人感觉这道题挺有质量的,做一下学习记录。 ...

phil的博客 258

Anchor graph 对齐文献解读

Align then Fusion: Generalized Large-scale Multi-view Clustering with Anchor Matching Correspondences文献解读

atease0001的博客 1163

岭回归的MATLAB步骤

MATLAB 实现框架,它涵盖了从数据导入到岭回归的步骤,包括计算共线性、使用 MAE、MSE、R²、MAPE 进行评价,以及绘制相应的可视化图表。

subject625Ruben的博客 1089

PAT甲级 1018 Public Bike Management

PAT甲级 1018 Public Bike Management There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and retur...

旺崽的博客 2992

2025年三级等保新规实战:从零开始搭建企业安全防护体系(附完整配置清单)

本文详细解析2025年三级等保新规的核心变化,提供从零开始搭建企业安全防护体系的实战指南。重点介绍动态防护能力、实战化攻防验证和全链路数据保护等关键要求,并附完整配置清单,帮助企业高效合规。

weixin_30836759的博客 468

PAT 1018 Public Bike Management

PAT 1018 C++版[updating…] 1.题意 共享单车需要维护,如果一个车站能最多容纳Cmax【Cmax是一个偶数】辆车,一个车站的当前车数为Cmax/2,则称之为最佳状态。现在给出不是最佳状态的车站,问到这个车站的路径,以及从管理中心需要发送和收回的车子数。 2.分析 2.1 方法1 单纯的 dijkstra方法是难以满足这题的需求。下面给出原因: 因为需要求出收回的车子数,所以...

说文科技,做有态度的研究。 663

PCIe Gen4 Switch芯片PM40028调试踩坑实录:从boot引脚设置到串口无输出的诡异问题

本文记录了PCIe Gen4 Switch芯片PM40028的实战调试过程,重点剖析了从boot引脚配置错误导致串口无输出,到使用逻辑分析仪定位并解决启动问题的完整排障思路。文章深入探讨了芯片启动流程、硬件信号测量方法,并总结了高速硬件调试的核心经验与系统性排查清单,为工程师解决类似PCIe Gen4设备调试难题提供实用指南。

i8j9k0l1的博客 251

ICCV 2025 | 哈工大、清华等提出首个双曲学习框架:HLFormer,革新局部相关视频检索

离原点越远,代表的语义越具体(部分)。现有方法大多在传统的欧几里得空间中对视频和文本进行建模,但这往往会导致对视频内在层次结构的错误表示,从而影响检索的准确性。通过UMAP对学习到的帧嵌入进行可视化(如下图),可以观察到,来自同一时刻的视频帧在嵌入空间中聚集在一起,清晰地展示了模型对时序关系的捕捉能力。下图的双曲空间可视化进一步证实了模型的层次建模能力:离原点越近的点,代表的语义层级越高、粒度越粗糙,反之亦然。:既然文本查询描述的是视频的一部分,那么在几何表示上,文本的嵌入向量应该从属于视频的嵌入向量。

我爱计算机视觉 1077

[PAT A1018]Public Bike Management

[PAT A1018]Public Bike Management 题目描述 There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station an...

欢迎来到我的编程小屋 802

Dify 从入门到熟悉100 天的学习大纲

理解 Dify 的核心概念,熟悉操作界面,并能创建简单的 AI 应用。祝你在这 100 天的旅程中学有所成,成为一名优秀的 AI 应用开发者!掌握生产环境部署、监控、调优,并关注 Dify 生态和前沿动态。探索多模态、Agent、API 集成、性能优化等高级主题。掌握工作流编排、高级提示词技巧、知识库优化等核心功能。

IT深耕十余载,大道之简 1735

PAT 甲级 1018  Public Bike Management

1018 Public Bike Management (30 point(s)) There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station ...

漫浸天空的雨色的备忘录~ 884

SAP 消息号T-CODE

SAP消息号修改汇总 确实有很多消息是可以修改的 FI: oBA5|oFMG|oFPM MM: oMRM|oKZZ|oMT4|oMCQ|o04C| SD: oVAH(V4)|oVM1(VL) CO:  oPR4_ACT|oPR4_CK|oPR4_CKML|oPR4_CKPF|oPR4_KKA|oPR4_KKP|oPR4_KKS| oPR4_KKS1|oPR4_P

heng0757的专栏 1万+

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

PAT甲级——1018 Public Bike Management

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the ...

Xiao2018428的博客 188

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

【PAT甲级 - C++题解】1018 Public Bike Management

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 824

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

【PAT甲级】1018 Public Bike Management

题目链接:1018Public Bike Management 这题昨天没过,今天改来改去还是过不了测试点7,令人头秃,先放着改天再回来改吧,用的是Dijkstra算法顺带看看相同路径长度的路径情况,以次权更新路径。网上我看大家都是结合DFS的,但我觉得我这思路也没毛病啊,啊啊啊 #include <iostream> #include <vector> #include <stack> using namespace std; #define MaxV 50..

五百零一 238

[PAT] A1018 Public Bike Management

【思路】 题目生词 figure n. 数字 v. 认为,认定;计算;是……重要部分 The stations are represented by vertices and the roads correspond to the edges. 顶点表示车站,边表示道路。 correspond to 相当于 capacity n. 能力;容量 ...

yue的博客 171

【PAT A1018】Public Bike Management

题目太长就不抄了,这题得做个重点记号,比较典型。 #include <cstdio> #include <vector> #include <stack> using namespace std; int cMax, N, Sp, M, cap[510] = {0}, G[510][510] = {0}, visited[510] = {0}; int dis...

lvmy3的博客 280

PAT_1018. Public Bike Management

第一份代码有一个测试点过不去 // // main.cpp // PAT_1018. Public Bike Management // // Created by wjq on 17/4/6. // Copyright © 2017年 wjq. All rights reserved. // #include #include #include using namespace

kukubao207的博客 245

【PAT】A1018 Public Bike Management【Dijkstra算法】【DFS】

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the ...

elegant coder 1154

【题解】PAT 1018 Public Bike Management

读题:最短路径问题的变化,需要从一个节点走最短路径到另一个节点(最短路径可能不止一条),每个节点都会有一些点数,然后要求输出满足点数大于等于某一个值,且点数尽可能小的最短路径...

i_want_to_studyi的博客 334

PAT 甲级 1018(Public Bike Management)

PAT 甲级 1016题目要求翻译代码 题目要求 A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a

qq_42865772的博客 231
上一篇: 头文件互相包含问题
下一篇: zoj 1649 Rescue
_IanXiao
博客等级 码龄12年 84粉丝 241原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值