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. 解决: Intelij IDEA 创建WEB项目时没有Servlet的jar包

    今天创建SpringMVC项目时 用到HttpServletRequest时, 发现项目中根本没有Servlet这个包, 在网上搜了一下,这个问题是因为web项目没有添加服务器导致的. 配置tomec ...

  2. vmware 开机自动启动

    vmware开机自动启动, 可以使用vmrun命令. 1. 首先在“我的电脑”-“属性”-“高级”-“环境变量”-“PATH”中添加vmware路径,如:C:\Program Files (x86)\ ...

  3. ubuntu下vim的简单配置

    该文章只是进行符合自己习惯的最基本的配置,更加高级的配置请参考更加有含量的博文! 1.打开vim下的配置文件 sudo vim /etc/vim/vimrc 2.在这个文件中,会有这么一句:synta ...

  4. 将Web项目War包部署到Tomcat服务器基本步骤(完整版)

    1. 常识:   1.1 War包 War包一般是在进行Web开发时,通常是一个网站Project下的所有源码的集合,里面包含前台HTML/CSS/JS的代码,也包含Java的代码. 当开发人员在自己 ...

  5. python爬虫入门八:多进程/多线程

    什么是多线程/多进程 引用虫师的解释: 计算机程序只不过是磁盘中可执行的,二进制(或其它类型)的数据.它们只有在被读取到内存中,被操作系统调用的时候才开始它们的生命期. 进程(有时被称为重量级进程)是 ...

  6. 【Umezawa's Jitte】真正用起来svn来管理版本

    之前用过一次 但是没有真正的用起来 只是知道了一些基本概念 好了 决定开始真正的用这个svn了 参考大神http://www.cnblogs.com/wrmfw/archive/2011/09/08/ ...

  7. 库函数的使用:sscanf的使用方法

    先贴代码,可以看懂代码的直接看代码: /***************************************************** ** Name : sscanf.c ** Auth ...

  8. HDU 4919 Exclusive or 数学

    题意: 定义 \[f(n)=\sum\limits_{i=1}^{n-1}(i\oplus (n-i))\] 求\(f(n),n \leq 10^{500}\) 分析: 这个数列对应OEIS的A006 ...

  9. bzoj2733: [HNOI2012]永无乡(splay)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3778  Solved: 2020 Description 永 ...

  10. loj2092 「ZJOI2016」大森林

    ref不是太懂-- #include <algorithm> #include <iostream> #include <cstring> #include < ...