PAT Advanced 1018 Public Bike Management (30) [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 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.
- 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.
- 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. Thesecond 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.
Output Specification:
For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then afer one space, output the path in the format: 0->S1->…->Sp. Finally afer another space, output the number of bikes that we must take back to PBMC afer 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
题意
广州公共自行车服务为来自世界各地的旅客提供了很大的便利,从城市中任意站借出自行车后可以在任意站归还
公共自行车管理中心PBMC实时监控城市中每个公共自行车租聘站的容量,若一个站容量刚好半满则该站达到理想状态,如果一个站满或者空,PBMC将收集或者发送自行车去调整该站到理想状态,在去往该问题站的路径上经过的所有站都将被调整为理想状态
当一个站出现问题后,PBMC将选择最短路径到达该问题站,如果最短路径超过一个,则选择发送自行车最少的路径,如果仍然有多条满足条件的路径路径,取从PBMC带回自行车最少的最短路径
题目分析
已知网图顶点数,点权,边权,顶点理想状态定义为点权为最大点权一半,需要走最短路径(边权和最小)将指定顶点的点权调整为理想状态,并将沿途所有顶点点权都调整为最大点权一半。
求满足条件最短路径,若有多个最短路径,取能将路径上所有顶点点权调整为理想状态需要携带的自行车数最少,若仍然有多个满足条件的最短路径,取路径所有顶点调整完毕后需从问题顶点带回的自行车数最少的最短路径
注意:
本题中的多条满足条件最短路径进行筛选时,统计路径上需携带自行车数minNeed最少和返回需携带自行车数minRemain最少时,不能只使用Dijskstra+简单累加思想,因为minNeed和minRemain在路径上的传递不满足最优子结构
测试数据如下:
Smaple2 Input:
10 4 4 5
4 8 9 0
0 1 1
1 2 1
1 3 2
2 3 1
3 4 1
Sample2 Output:
1 0->1->2->3->4 2
解题思路
- 存储图
- 邻接矩阵存储边和边权,int数组存储点权
- 邻接表存储边和边权,int数组存储点权
- 结构体邻接表存储边和边权,int数组存储点权
求单源带权最短路径
Dijkstra算法:核心为两个整型数组dist[maxn]和path[maxn],每次收集一个顶点v到最短路径中,并判断该顶点是否影响到其他顶点路径边权和,并修改其边权和dist[i]为最小值,修改path[i]为v
注:必须记录所有最短路径,以便后续步骤进行路径筛选路径筛选
DFS算法:遍历所有最短路径,并挑选出发送自行车最少且带回自行车最少的路径
易错点
本题筛选路径时,若考虑为路径上自行车数的简单加减,会出错(有两个测试点不通过,得分25)
Code
Code 01
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int INF=9999999;
const int maxn=510;
int n,cm,sp,vw[maxn],gw[maxn][maxn],dist[maxn],path[maxn],col[maxn];
int minNeed=INF,minRemain=INF;
vector<int> pre[maxn],temp,minp; //统计所有最短路径-逆序
int dmin() {
int min=INF,mini=-1;
for(int i=0; i<=n; i++) {
if(col[i]==0 && min>dist[i]) {
min=dist[i];
mini=i;
}
}
return mini;
}
void dijkstra(int c) {
// 初始化
fill(dist,dist+n+1,INF);
fill(path,path+n+1,-1);
dist[0]=0;
// 取最小值
for(int j=0; j<=n; j++) {
int min=dmin();
col[min]=1;
if(min==sp)break; //已找到终点 最短路径
for(int i=0; i<=n; i++) {
if(gw[min][i]==0||col[i]==1)continue;
if(dist[min]+gw[min][i]<dist[i]) {
dist[i]=dist[min]+gw[min][i];
path[i]=min;
pre[i].clear();
pre[i].push_back(min);
} else if(dist[min]+gw[min][i]==dist[i]) {
pre[i].push_back(min);
}
}
}
}
void dfs(int e) {
if(e==0) {
int need=0,remain=0;
for(int i=temp.size()-1; i>=0; i--) {
int id=temp[i];
if(vw[id]>0) {
remain+=vw[id];
}else{
if(remain>abs(vw[id])){
remain-=abs(vw[id]);
}else{
need+=abs(vw[id])-remain;
remain=0;
}
}
}
if(need<minNeed) {
minp=temp;
minNeed=need;
minRemain=remain;
} else if(need==minNeed&&remain<minRemain) {
minp=temp;
minRemain=remain;
}
return;
}
temp.push_back(e);
for(int i=0; i<pre[e].size(); i++) {
dfs(pre[e][i]);
}
temp.pop_back();
}
int main(int argc,char * argv[]) {
int m,a,b;
scanf("%d %d %d %d",&cm,&n,&sp,&m);
for(int i=1; i<=n; i++){
scanf("%d",&vw[i]);
vw[i]-=(cm>>1);
}
for(int i=0; i<m; i++) {
scanf("%d %d",&a,&b);
scanf("%d",&gw[a][b]);
gw[b][a]=gw[a][b];
}
dijkstra(0);
dfs(sp);
printf("%d ",minNeed);
printf("0");
for(int i=minp.size()-1; i>=0; i--) {
printf("->%d",minp[i]);
}
printf(" %d",minRemain);
return 0;
}
PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]的更多相关文章
- 1018 Public Bike Management (30) Dijkstra算法 + DFS
题目及题解 https://blog.csdn.net/CV_Jason/article/details/81385228 迪杰斯特拉重新认识 两个核心的存储结构: int dis[n]: //记录每 ...
- PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)
1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides ...
- PAT A 1018. Public Bike Management (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1018 先用Dijkstra算出最短路,然后二分答案来验证,顺便求出剩余最小,然后再从终点dfs回去求出路 ...
- PAT 1018 Public Bike Management(Dijkstra 最短路)
1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT甲级1018. Public Bike Management
PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
- 1018. Public Bike Management (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...
- 1018 Public Bike Management (30 分)
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- 1018 Public Bike Management (30)(30 分)
时间限制400 ms 内存限制65536 kB 代码长度限制16000 B There is a public bike service in Hangzhou City which provides ...
- 1018 Public Bike Management (30分) 思路分析 + 满分代码
题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...
随机推荐
- Button btn = sender as Button; //创建Button对象 这句话中Sencler和as是什么。怎么使用Sender和as
ender是个object类型的变量名,通常都是事件的默认参数名,而这个变量存的是触发这个事件的控件,而as 可以理解为抽象,它把object类理的sender变量抽象成了(Button)类型.这样它 ...
- ubuntu 环境下pycharm的 安装与激活教程
1. 基本安装: 1.1 打开Ubuntu的应用市场,并在搜索栏搜索pycharm,结果如下图所示 1.2 选择pro版本进行安装,结果如下图所示: 1.3打开安装后的pycharm,如果出现下图所示 ...
- 图像检索:CEDD(Color and Edge Directivity Descriptor)算法 颜色和边缘的方向性描述符
颜色和边缘的方向性描述符(Color and Edge Directivity Descriptor,CEDD) 本文节选自论文<Android手机上图像分类技术的研究>. CEDD具有抽 ...
- R 《回归分析与线性统计模型》page140,5.1
rm(list = ls()) library(car) library(MASS) library(openxlsx) A = read.xlsx("data140.xlsx") ...
- PHP使用ElasticSearch做搜索
PHP 使用 ElasticSearch 做搜索 https://blog.csdn.net/zhanghao143lina/article/details/80280321 https://www. ...
- Vue(十)---路由
Vue.js 路由允许我们通过不同的 URL 访问不同的内容.通过 Vue.js 可以实现多视图的单页Web应用(single page web application,SPA). 需要引入vue-r ...
- numpy.linspace使用详解
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) 在指定的间隔内返回均匀间隔的数字. 返回nu ...
- class(二)--派生类的继承
前言 从我之前的一篇笔记对象的继承中, 我们可以知道JS的继承方式依赖原型链,而比较好的继承方式是寄生组合式继承 先来温习下什么是寄生组合式继承 function Rectangle(length, ...
- windows上通过自定义后缀文件启动Unity应用
好吧,一个Unity应用并不常见的需求.. 两个步骤 1.关联注册表 2.把自定义后缀文件作为启动参数传给Unity做处理 1.关联注册表 (.XXXX是自定义后缀) Windows Registr ...
- PIP无法使用,script文件夹为空解决
[问题]环境变量已配置,但pip.pip3无法使用,且script文件夹为空解决: 一.安装pip3 python -m ensurepip 运行完之后就pip3有了: 二.安装pip python ...