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回去求出路 ...
随机推荐
- JAVA校内赛
第一题: 问题描述 在计算机存储中,15.125GB是多少MB?答案提交 这是一道结果填空的题,你只需要算出结果后提交即可.本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分. ...
- 数据库中的两个最重要的日志redo log和binlog
mysql整体来看其实只有两部分,一部分是server层,一部分是引擎层. 1.redo log(重做日志):当有一条记录需要更新的时候,InnoDB 引擎就会先把记录写入redo log里面,并更新 ...
- Python模块三
collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict. ...
- JsonFormat 日期少了8个小时?还我
JsonFormat 后日期少了8个小时什么鬼? 前言 今天测试的时候发现时间对不上,比数据库里的时间少了8个小时?测试小姐姐一顿狂轰乱炸,一点都不温柔. 什么鬼?哪里出了问题?数据库显示的是下面
- Navicat15最新版本破解 亲测可用!!!
1.下载Navicat Premium官网https://www.navicat.com.cn/下载最新版本下载安装 2.本人网盘链接:https://pan.baidu.com/s/1ncSaxId ...
- gitbook 入门教程之超高颜值的思维导图simple-mind-map插件
欢迎访问 gitbook-plugin-simple-mind-map 官网
- 工具之scroolToIndex
需求定位:导航中实现子元素滚动到父元素的最左侧 解决方案:查找该子元素的offsetLeft值,然后让父元素滚动offsetLeft,parenDom.scrollLeft = childDom.of ...
- 你知道吗,Flutter内置了10多种show
注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 showDialog showDialog 用于弹出Mat ...
- ALSA driver---DPCM
https://www.kernel.org/doc/html/v4.11/sound/soc/dpcm.html Description Dynamic PCM allows an ALSA PCM ...
- JavaScript每日学习日记(0)
8.10.2019 1.JavaScript能改变HTML内容.属性.样式,能隐藏或显示HTML元素. 2.JavaScript函数可以任意数量被放置在<body>.<head> ...