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回去求出路 ...
 
随机推荐
- 异常 context 包的扫描
			
异常信息: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 55; schema_reference.4: 无法读取方案文档 ' ...
 - ASP.NET Core身份认证服务框架IdentityServer4 介绍
			
IdentityServer4是ASP.NET Core 2的OpenID Connect和OAuth 2.0框架.它可以在您的应用程序中提供以下功能: 它使你的应用程序具有如下特点: 认证即服务 适 ...
 - js 碰撞+拖拽
			
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
 - 学习Shader所需的数学基础(坐标系,点和矢量)
			
数学对于计算机图形学的重要性是不言而喻的.在学习Shader之前,首先就要打好数学基础,好在入门Unity Shader所需的数学知识都是线性代数中很基础的的内容.按部就班的来,第一篇文章记录总结的是 ...
 - 分享几个 PHP 编码的最佳实践
			
对于初学者而言,可能很难理解为什么某些做法更安全. 但是,以下一些技巧可能超出了 PHP 的范围. 始终使用大括号 让我们看下面的代码: if (isset($condition) && ...
 - 计算机网络 Computer Networks 期末复习总提纲
			
平时不学习,期末火葬场. 一周时间靠王道考研和各路 pdf 自学计网,留下的提纲都在这里了.全是干货.全文 pdf 可以在这里下载:http://cloud.billc.io/s/xNHarppQPG ...
 - oracle去除重复数据与oracle分页
			
一.去除oracle中重复数据,可以使用rowid列,rowid列是一个伪列,该列在数据库中灭一个表中都有,但是我们查询数据库的时候,默认都没有给我们返回这一列,这一列用来区分数据库中的每一行时间,可 ...
 - 手把手教大家如何用scrapy爬虫框架爬取王者荣耀官网英雄资料
			
之前被两个关系很好的朋友拉入了王者荣耀的大坑,奈何技术太差,就想着做一个英雄的随查手册,这样就可以边打边查了.菜归菜,至少得说明咱打王者的态度是没得说的,对吧?大神不喜勿喷!!!感谢!!废话不多说,开 ...
 - 我的Keras使用总结(2)——构建图像分类模型(针对小数据集)
			
Keras基本的使用都已经清楚了,那么这篇主要学习如何使用Keras进行训练模型,训练训练,主要就是“练”,所以多做几个案例就知道怎么做了. 在本文中,我们将提供一些面向小数据集(几百张到几千张图片) ...
 - Bisecting GlcNAc is a general suppressor of terminal modification of N-glycan (解读人:王茹凯)
			
文献名:Bisecting GlcNAc is a general suppressor of terminal modification of N-glycan(平分GlcNAc是N-聚糖末端修饰的 ...