1018 Public Bike Management (PAT甲级)

news2024/9/21 2:50:39

这道题一开始测试点7过不了,后来发现是需要“output the one that requires minimum number of bikes that we must take back to PBMC”,补上就可以了。

#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
const int maxN = 501;
const int INF = 99999999;

int cap, N, sp, M, u, v, t, init, pivot, minDist, need, minNeed, takeBack;
int c[maxN];
int dist[maxN];
int d[maxN][maxN];
bool visited[maxN];
std::set<int> path[maxN];
std::vector<int> curr;
std::vector<std::vector<int>> ans;

void dijkstra(int k){
    dist[k] = 0;
    for(int i = 0; i <= N; ++i){
        u = -1;
        minDist = INF;
        for(int j = 0; j <= N; ++j){
            if(!visited[j] && dist[j] < minDist){
                u = j;
                minDist = dist[j];
            }
        }
        if(u == -1){
            return;
        }
        visited[u] = true;
        for(v = 0; v <= N; ++v){
            if(!visited[v] && d[u][v] < INF && dist[u] + d[u][v] < dist[v]){
                dist[v] = dist[u] + d[u][v];
                path[v].clear();
                path[v].insert(u);
            } else if(!visited[v] && d[u][v] < INF && dist[u] + d[u][v] == dist[v]){
                path[v].insert(u);
            }
        }
    }
}

void dfs(int k){
    curr.push_back(k);
    if(k == 0){
        ans.push_back(curr);
        curr.pop_back();
        return;
    }
    for(auto it = path[k].begin(); it != path[k].end(); ++it){
        dfs(*it);
    }
    curr.pop_back();
}

int main(){
    scanf("%d %d %d %d", &cap, &N, &sp, &M);
    for(int i = 1; i <= N; ++i){
        scanf("%d", &c[i]);
    }
    std::fill(d[0], d[0] + maxN * maxN, INF);
    for(int i = 0; i < M; ++i){
        scanf("%d %d %d", &u, &v, &t);
        d[u][v] = t;
        d[v][u] = t;
    }
    std::fill(visited, visited + N + 1, false);
    std::fill(dist, dist + N + 1, INF);
    dijkstra(0);
    cap /= 2;
    dfs(sp);
    minNeed = INF;
    for(int i = 0; i < ans.size(); ++i){
        reverse(ans[i].begin(), ans[i].end());
        init = 0;
        need = 0;
        for(int j = 1; j < ans[i].size(); ++j){
            if(c[ans[i][j]] >= cap){
                init += c[ans[i][j]] - cap;
            } else if(init < cap - c[ans[i][j]]){
                need += cap - c[ans[i][j]] - init;
                init = 0;
            } else{
                init -= cap - c[ans[i][j]];
            }
        }
        if(need < minNeed){
            minNeed = need;
            pivot = i;
            takeBack = init;
        } else if(need == minNeed && init < takeBack){
            pivot = i;
            takeBack = init;
        }
    }
    printf("%d 0", minNeed);
    for(int i = 1; i < ans[pivot].size(); ++i){
        printf("->%d", ans[pivot][i]);
    }
    printf(" %d", takeBack);
    return 0;
}

题目如下:

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.

The above figure 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.

Input Specification:

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 Mlines 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.

Output Specification:

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.

Sample Input:

10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

Sample Output:

3 0->2->3 0

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/815706.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

小红书运营推广方法分享

大家好&#xff0c;我是网媒智星&#xff0c;今天跟大家讨论一下小红书的运营推广方法&#xff0c;总结了七点经验分享给大家。 首先&#xff0c;让我们了解一下什么是热门文案。热门文案可从以下三个方面来定义&#xff1a; 1. 阅读量&#xff1a;如果一篇小红书的阅读量达到上…

汽车后视镜反射率测定仪

汽车后视镜位于汽车头部的左右两侧&#xff0c;顶部以及汽车内部的前方。汽车后视镜反映汽车正后方视野、两侧视野和汽车前端区域视野&#xff0c;以便驾驶员可以间接看清楚这些位置的情况&#xff0c;它起着“第二只眼睛”的作用&#xff0c;扩大了驾驶者的视野范围&#xff0…

手写自定义的spring-boot-start

需求&#xff1a;手写一个加密的spring-boot-start&#xff0c;按着用户定义的加密算法&#xff08;可选&#xff1a;MD5、SHA&#xff09;去加密内容 新建一个maven项目 新建好的项目结构和pom.xml如图 添加pom.xml 完整的pom.xml文件 <?xml version"1.0" …

高并发下判重难?架构必备技能 - 布隆过滤器

系列文章目录 当Dubbo遇到高并发&#xff1a;探究流量控制解决方案 主从选举机制&#xff0c;架构高可用性的不二选择 高并发下判重难&#xff1f;架构必备技能 - 布隆过滤器 系列文章目录前言一、布隆过滤器简介二、特性与应用场景三、参数定制四、java版本的Demo五、总结 前…

七个步骤, 编写一个 Servlet 的 HelloWorld 程序

目录 1.创建项目 2.引入依赖 ①直接粘贴仓库目标地址代码 ② 下载jar包, 然后导入jar包 3.创建目录 4.编写代码 5.打包代码 6. 部署程序 ①使用IDEA打成war包 ②更方便的部署方式 7.验证程序 1.创建项目 使用IDEA创建一个maven项目 2.引入依赖 引入依赖的方式两种…

selenium-web自动化测试

一、selenium环境部署 1.准备chrome浏览器&#xff08;其他浏览器也行&#xff09; 2.准备chrome驱动包 步骤一&#xff1a;查看自己的谷歌浏览器版本(浏览器版本和驱动版本一定要对应) 步骤二&#xff1a;下载对应的驱动包, 下载路径 : ChromeDriver - WebDriver for Chrom…

环境搭建的上手指南

前言 测试环境是QA开展测试工作的前置条件。稳定和可控的测试环境&#xff0c;可以使测试人员在执行测试用例时无需花费额外的时间去维护。 有些公司运维或者研发部门会帮忙准备好测试环境&#xff0c;但是QA如果一味依赖其他部门&#xff0c;会局限测试工作的开展。 一、什…

OSPF随记

邻居状态机&#xff1a;进度条 工作原理&#xff1a; 1.发送hello报文&#xff0c;建立邻居关系 down&#xff1a;没有发送报文之前 init:开始发送hello报文&#xff0c;自己收到的hello报文中没有自己的route-id 2way:收到的hello报文中有自己的route-id&#xff08;在2w…

selenium如何打开浏览器,等待用户输入完成后,再运行

selenium如何打开浏览器&#xff0c;等待用户输入完成后&#xff0c;再运行 一、在脚本中&#xff0c;等待用户输入 在使用 Selenium 打开浏览器后等待用户输入完成&#xff0c;可以使用 Python 编写一个简单的脚本来实现。首先&#xff0c;确保你已经安装了 Selenium 和对应的…

这 4 个系统可靠性评估指标,可能比 MTTR 更靠谱!

如果要评选研发效能管理中最重要的 10 个度量指标&#xff0c;相信 MTTR&#xff08;Mean Time to Recover&#xff0c;平均恢复时间&#xff09;一定榜上有名。 MTTR 代表一定周期内可修复系统不可用状态的平均持续时长&#xff0c;可以帮助企业更好地理解技术团队与研发工作…

GPU显卡驱动安装

查看GPU版本 lspci | grep -i nvidia从下面的网址ThePCI ID Repository中输入ID查看对应的GPU版本 官网NVIDIA下载对应的驱动 安装下载的文件 sudo sh ./NVIDIA-Linux-x86_64-535.86.05.run 检验是否安装成功 nvidia-smi

机器学习-Regression

机器学习(Regression:Case Study) 前言&#xff1a; 学习资料 videopptblog Example Application 建立一个model&#xff0c;将宝可梦的一些数据作为输入&#xff0c;然后输出宝可梦进化以后的战斗力CP值&#xff0c;这个model的建立尤为重要&#xff0c;但是这个模型的建立…

目标检测中 anchor base和anchor free

目标检测中两种不同anchor的生成 趋势&#xff1a;anchor free越来越受到实时性检测的青睐&#xff0c;&#xff0c;&#xff0c;

redis 高级篇 redis 源码的读取分析

一 redis源码分析 1.1 源码分析 1每一个kv键值对应有一个dictEntry。 2.底层数据结构

《QDebug 2023年7月》

一、Qt Widgets 问题交流 1.QPainter旋转角度绘制线条的一点问题 QPainter 旋转角度&#xff0c;等距绘制若干线条&#xff0c;会出现绘制不均匀的情况&#xff1a; 但是在测试 QML Canvas 绘制时&#xff0c;发现效果是正常的&#xff0c;原来是因为 Canvas 默认的 capStyle…

正则表达式的应用及示例解析

正则表达式&#xff08;Regular Expression&#xff0c;简称Regex&#xff09;是由特殊字符组成的模式字符串&#xff0c;用于匹配和搜索文本中的特定模式。它在数据处理、文本搜索和替换等方面广泛应用。本文将介绍正则表达式的基本语法&#xff0c;并提供常见的正则表达式示例…

Linux系统增加新用户

使用adduser命令&#xff0c;不使用useradd命令。 登录root用户&#xff0c;或者有sudo权限的用户创建。 sudo adduser test修改文件目录的权限 sudo chmod -R 777 /opt/登录新创建的用户创建文件 mkdir test删除用户 userdel -r test增加网络映射&#xff0c;安装samba sudo…

RBAC三级树状菜单实现(从前端到后端)未完待续

1、表格设计 RBAC 2、前端路由 根据不同的用户id显示不同的菜单。 根据路由 3、多级菜单 展示所有权限&#xff0c;并且根据当前用户id展示它所属的角色的所有菜单。 前端树状展示 思路&#xff1a; 后端&#xff1a;传给前端map&#xff0c;map里1个是所有菜单&am…

汽车过户时,怎么选到理想的好车牌?

在汽车过户的过程中&#xff0c;选到一副理想的好车牌就像买彩票中大奖一样令人兴奋。但是&#xff0c;怎样找到这样一块车牌呢&#xff1f;这就是本文要探讨的问题。 首先&#xff0c;我们来聊聊选车牌的技巧。很多人喜欢选择有特别数字的车牌&#xff0c;如“8888”、“6666”…

太狠了,Spring 全家桶笔记, 一站式通关全攻略, 已入职某厂涨薪 18K

Spring 早已成为 Java 后端开发事实上的行业标准&#xff0c;无数的公司选择 Spring 作为基础的开发框架&#xff0c;大部分 Java 后端程序员在日常工作中也会接触到 Spring &#xff0c;因此&#xff0c;如何用好 Spring &#xff0c;也就成为 Java 程序员的必修课之一。 为了…