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的更多相关文章

  1. PAT 1018 Public Bike Management[难]

    链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018  Public ...

  2. PAT 1018. Public Bike Management

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  3. PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  4. PAT 1018 Public Bike Management(Dijkstra 最短路)

    1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  5. [PAT] A1018 Public Bike Management

    [思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...

  6. PAT_A1018#Public Bike Management

    Source: PAT A1018 Public Bike Management (30 分) Description: There is a public bike service in Hangz ...

  7. PAT甲级1018. Public Bike Management

    PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...

  8. 【PAT甲级】Public Bike Management 题解

    题目描述 There is a public bike service in Hangzhou City which provides great convenience to the tourist ...

  9. 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 ...

随机推荐

  1. git Bash 学习

    ,ranh新建一个本地仓库并与github连接的方法 注:该终端也具有按tab键补全功能,应该合理应用 1. 新建一个文件夹,并将git bash的位置转到相应文件夹下(cd 命令转移) 2.git ...

  2. 项目十八-Hadoop+Hbase分布式集群架构“完全篇”

    本文收录在Linux运维企业架构实战系列 前言:本篇博客是博主踩过无数坑,反复查阅资料,一步步搭建,操作完成后整理的个人心得,分享给大家~~~ 1.认识Hadoop和Hbase 1.1 hadoop简 ...

  3. vue.js 图表chart.js使用

    在使用这个chart.js之前,自己写过一个饼图,总之碰到的问题不少,所以能用现成的插件就用,能节省不少时间 这里不打算介绍chart.js里面详细的参数意义和各个参数的用法,只作为首次使用chart ...

  4. awk速查手册

    简介awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进 ...

  5. Federated引擎

    Federated就像他的名字所说“联盟”,其作用就是把两个不同区域的数据库联系起来,以至可以访问在远程数据库的表中的数据,而不是本地的表. 1.进入mysql命令行,查看是否已安装Federated ...

  6. Essential C++ 3.1 节的代码练习——指针方式

    // // PointerToValue.cpp // Working // // Created by Hawkins, Dakota Y on 6/3/16. // Copyright 2016 ...

  7. Fliptile POJ - 3279 (开关问题)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16483   Accepted: 6017 Descrip ...

  8. 动态规划:HDU-1203-0-1背包问题:I NEED A OFFER!

    解题心得: 动态规划就是找到状态转移方程式,但是就本题0-1背包问题来说转移方程式很简单,几乎看模板就行了. 在本题来说WA了很多次,很郁闷,因为我记录v[i]的时候i是从0开始的,一些特殊数据就很尴 ...

  9. Python高级主题:Python ABC(抽象基类)

    #抽象类实例 作用统一规范接口,降低使用复杂度.import abcclass Animal(metaclass = abc.ABCMeta): ##只能被继承,不能实例化,实例化会报错 @abc.a ...

  10. jQuery+Asp.net 实现简单的下拉加载更多功能

    原来做过的商城项目现在需要增加下拉加载的功能,简单的实现了一下.大概可以整理一下思路跟代码. 把需要下拉加载的内容进行转为JSON处理存在当前页面: <script type="tex ...