pat 甲级 Public Bike Management
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.
Figure 1
Figure 1 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.
输入描述:
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 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.
输出描述:
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.
输入例子:
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
输出例子:
3 0->2->3 0 题意:感觉题意叙述的是真够混乱的,题目描述中说公司派出的车辆越少越好,输出描述中又说回收的车辆越少越好,看得一头雾水,简而言之,若有多条最短路径,筛选出一条最合适的路径。筛选的规则遵循两点,1:公司派出的车辆数目越少越好 2:在公司派出车辆尽量少的情况下,公司需要回收的车辆
数目也尽量的少。至于perfect状态,指一个车站车辆数目正好是车站能够容纳的车辆数目的一半,多了要回收,少了要添加。
思路:最短路,最后求路径可以dfs搜索并筛选。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<set>
#include<queue>
using namespace std;
#define N_MAX 500+5
#define INF 0x3f3f3f3f
typedef long long ll;
int c_max, n, m, Index;
int c[N_MAX];
struct edge {
int to, cost;
edge() {}
edge(int to,int cost):to(to),cost(cost) {}
};
struct P{
int first, second;
P() {}
P(int first,int second):first(first),second(second) {}
bool operator < (const P&b) const{
return first > b.first;
}
};
vector<edge>G[N_MAX];
int d[N_MAX];
vector<int>Prev[N_MAX];
int V;
void dijkstra(int s) {
priority_queue<P>que;
fill(d, d + V, INF);
d[s] = ;
que.push(P(, s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (p.first > d[v])continue;
for (int i = ; i < G[v].size();i++) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
Prev[e.to].clear();
Prev[e.to].push_back(v);
}
else if (d[e.to] == d[v] + e.cost) {
Prev[e.to].push_back(v);
}
}
}
}
vector<int>r;
int road[N_MAX],process[N_MAX];
int min_num_go = INF,min_num_back=INF;
void dfs(int x,int step,int num) {
road[step] = x;
process[step] = num;
if (x == ) {//前驱结点为起点,搜索终止
int num_go = , num_back;
int tmp_num=;
for (int i = step-; i >= ; i--) {
tmp_num += process[i];
if (tmp_num< && num_go > tmp_num) {//派出车辆必须保证经过的每个站都能达到perfect状态
num_go = tmp_num;
}
}
num_go = -num_go;
num_back = num_go+tmp_num;//假若派出的车辆为0,回收的车辆的数目为tmp_num,实际派出车辆为num_go,则实际回收车辆为num_go+tmp_num
if (min_num_go > num_go) {
min_num_go = num_go;
min_num_back = num_back;
r.clear();
for (int i = step; i >= ; i--)r.push_back(road[i]);
}
else if (min_num_go == num_go&&min_num_back > num_back) {
min_num_back = num_back;
r.clear();
for (int i = step; i >= ; i--)r.push_back(road[i]);
}
return;
}
for (int i = ; i < Prev[x].size();i++) {
int from = Prev[x][i];
if (from)dfs(from, step + , (-c_max / + c[from]));
else dfs(from, step + , num );
}
} int main() {
while (scanf("%d%d%d%d", &c_max, &n, &Index, &m) != EOF) {
V = n + ;
for (int i = ; i <= n; i++)scanf("%d", &c[i]);
for (int i = ; i < m; i++) {
int from, to, cost;
scanf("%d%d%d", &from, &to, &cost);
G[from].push_back(edge(to, cost));
G[to].push_back(edge(from, cost));
}
dijkstra();
dfs(Index, , -c_max / + c[Index]);
cout << min_num_go << " ";
for (int i = ; i < r.size();i++) {
printf("%d%s", r[i], i + == r.size()? " ":"->");
}
cout << min_num_back << endl;
}
return ;
}
pat 甲级 Public Bike Management的更多相关文章
- PAT 1018 Public Bike Management[难]
链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018 Public ...
- PAT 1018. Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT 1018 Public Bike Management(Dijkstra 最短路)
1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- [PAT] A1018 Public Bike Management
[思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...
- PAT_A1018#Public Bike Management
Source: PAT A1018 Public Bike Management (30 分) Description: There is a public bike service in Hangz ...
- PAT甲级1018. Public Bike Management
PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
- 【PAT甲级】Public Bike Management 题解
题目描述 There is a public bike service in Hangzhou City which provides great convenience to the tourist ...
- 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 ...
随机推荐
- java链接MySQL数据库时使用com.mysql.jdbc.Connection的包会出红线问题 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
package com.swift; //这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connecti ...
- Uva 网络(Network,Seoul 2007,LA 3902)
#include<iostream> #include<cstring> #include<vector> using namespace std; +; int ...
- mysql的性能优化案例
在一次项目实现中,以前写了个程序,将在txt文件中的电话号码和对应的类型往数据库中插入,小数据量的情况下,用个数组遍历循环的方式,很容易解决,但是当数据量一下 但是,几十万个电话一次性插入,就变得耗时 ...
- Android Studio 3.0 安装注意点
在安装Android studio 3.0+ 时候,会遇到默认不带Android SDK 的问题. 在启动Android studio 后,会提示让选择SDK目录,选择下载目录,对应的去下载 那么问题 ...
- PHP 批量操作 Excel
自己封装了一个批量操作excel文件的方法,通过xls文件地址集合遍历,第三个参数传入一个匿名函数用于每个需求的不同进行的操作,实例中我想要得到列表中含有折字的行,封装成sql语句返回. xls文件超 ...
- BFS:Nightmare(可返回路径)
解题心得: 1.point:关于可以返回路径的BFS的标记方法,并非是简单的0-1,而是可以用时间比较之后判断是否push. 2.queue创建的地点(初始化问题),在全局中创建queue在一次调用B ...
- 商品评分效果JavaScript
<script> window.onload=function(){ //----------选中的星星会多出一个属性:isClick="true" 藉此来获取评分-- ...
- proguaid 混淆代码
注意:这里有一个坑.就是-ignorewarnings 他老是混淆不了,告诉你不行.其实加上这句话,就可以了. 下面贴一下代码: -injars c:/ceb_lib.jar -outjars c:/ ...
- kettle Spoon.bat闪退解决办法!
1.Java环境配置问题: java_home:D:\Program Files\Java\jdk1.7.0_25(安装jdk路径) classpath:.;%java_home%\lib\dt.ja ...
- Nhibernate官方体系结构图部分中文翻译
原文链接 :http://nhibernate.info/doc/nh/en/index.html#architecture 体系结构图 高度抽象NHibernate体系架构图 这幅图展示了NHibe ...