1018 Public Bike Management (30 分)
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:
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 (≤), always an even number, is the maximum capacity of each station; N (≤), 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 (,) 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 after one space, output the path in the format: 0. 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
题目分析:用Dijkstra算法求解后不对 看了柳神的博客才知道要使用dijkstra与dfs结合的方式
因为minneed和minback在求解过程中不满足最优子结构 换言之 无法在求解过程中就知道那条路是最优的,因此,将最短路径求出后在利用dfs进行遍历判断最优解
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#define INIFITY 65535
using namespace std;
int g[][];
int dist[];
int collected[];
int weight[];
int minNeed = INIFITY;
int minBack = INIFITY;
vector<int> pre[], path, tempath;
int C, N, D, M;
void dfs(int v){
tempath.push_back(v);
if (v == ) {
int need = ;
int back = ;
for (int i = tempath.size()-; i>=; i--){
int id = tempath[i];
if (weight[id] > )
back += weight[id];
else if (back>(-weight[id]))
back += weight[id];
else{
need+= ( - weight[id]) - back;
back = ;
}
}
if (need < minNeed){
minNeed = need;
minBack = back;
path = tempath;
}
else if (need == minNeed && back < minBack){
minBack=back;
path = tempath;
}
tempath.pop_back();
return;
}
for (int i = ; i<pre[v].size(); i++)
dfs(pre[v][i]);
tempath.pop_back();
}
int main()
{
fill(g[], g[] + * , INIFITY);
fill(dist, dist + , INIFITY);
cin >> C >> N >> D >> M;
for (int i = ; i <=N; i++)
{
int w;
cin >> w;
weight[i] = w - C / ;
}
for (int i = ; i < M; i++){
int v1, v2, length;
cin >> v1 >> v2 >> length;
g[v1][v2]=g[v2][v1]= length;
}
//Dijkstra
dist[] = ;
for (int i = ; i <= N; i++){
int Min = INIFITY;
int Minp = -;
for (int v = ; v <= N; v++){
if (!collected[v]&&dist[v] < Min){
Min = dist[v];
Minp = v;
}
}
collected[Minp] = ;
for (int u = ; u <= N; u++){
if(!collected[u]&&g[Minp][u]!=INIFITY)
if (dist[Minp] + g[Minp][u] < dist[u]){
dist[u] = dist[Minp] + g[Minp][u];
pre[u].clear();
pre[u].push_back(Minp);
}
else if (dist[u] == dist[Minp] + g[Minp][u])
pre[u].push_back(Minp);
}
}
dfs(D);
cout << minNeed << "";
for (int i = path.size() - ; i >= ; i--)
cout << "->" << path[i];
cout << " " << minBack;
return ;
}
1018 Public Bike Management (30 分)的更多相关文章
- 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 ...
- 1018 Public Bike Management (30分) 思路分析 + 满分代码
题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
- 1018 Public Bike Management (30分) (迪杰斯特拉+dfs)
思路就是dijkstra找出最短路,dfs比较每一个最短路. dijkstra可以找出每个点的前一个点, 所以dfs搜索比较的时候怎么处理携带和带走的数量就是关键,考虑到这个携带和带走和路径顺序有关, ...
- 【PAT甲级】1018 Public Bike Management (30 分)(SPFA,DFS)
题意: 输入四个正整数C,N,S,M(c<=100,n<=500),分别表示每个自行车站的最大容量,车站个数,此次行动的终点站以及接下来的M行输入即通路.接下来输入一行N个正整数表示每个自 ...
- 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)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...
- 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 ...
- PAT A 1018. Public Bike Management (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1018 先用Dijkstra算出最短路,然后二分答案来验证,顺便求出剩余最小,然后再从终点dfs回去求出路 ...
随机推荐
- 对Ajax的原理的理解和使用
1.什么是ajax? AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方 ...
- (转)协议森林08 不放弃 (TCP协议与流通信)
协议森林08 不放弃 (TCP协议与流通信) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! TCP(Transportation ...
- 你知道吗,Flutter内置了10多种show
注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 showDialog showDialog 用于弹出Mat ...
- react 给选中的li添加样式(转载)
路:使用事件委托,关键:获取到的index必须转为数字,因为它是字符串 handleClick = (e) => { const nodeName = e.target.nodeName.toU ...
- oracle中plsql练习-----在控制台输出1到100以内的素数。
一.思路:首先需要知道素数的概念即质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数. 中心思想是,外循环所有的自然数,内循环折半查询,增加代码的速度,注意:从1开始,需要大于1,但是pl ...
- 项目部署Django+celery+redis
celery介绍 1.celery应用举例 1.Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以 ...
- [Microsoft Teams]使用连接器接收Azure Devops的通知
1. 什么是连接器 连接器(connector)是Teams中频道的一个接受消息的功能,官方的解释如下: 连接器允许用户订阅来自 web 服务的接收通知和消息. 它们公开服务的 HTTPS 终结点,通 ...
- 从零开始学习R语言(八)——R语言绘图
本文首发于知乎专栏:https://zhuanlan.zhihu.com/p/74051739 也同步更新于我的个人博客:https://www.cnblogs.com/nickwu/p/125683 ...
- SQL的分类使用(增删改查)
1.SQL的分类使用(*代表重点的程度) DDL ** (Data Definition Language)数据库定义语言 用来定义数据库对象: 库 表 列 等 DCL (D ...
- 环境篇:Virtualbox+Vagrant安装Centos7
环境篇:Virtualbox+Vagrant安装Centos7 1 安装Vagrant Vagrant下载地址:https://www.vagrantup.com/ Vagrant百度网盘:https ...