您的位置:首页 > 其它

pat 甲级 Public Bike Management

2018-03-04 11:24 381 查看

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] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (p.first > d[v])continue;
for (int i = 0; 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 == 0) {//前驱结点为起点,搜索终止
int num_go = 0, num_back;
int tmp_num=0;
for (int i = step-1; i >= 0; i--) {
tmp_num += process[i];
if (tmp_num<0 && 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 >= 0; 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 >= 0; i--)r.push_back(road[i]);
}
return;
}
for (int i = 0; i < Prev[x].size();i++) {
int from = Prev[x][i];
if (from)dfs(from, step + 1, (-c_max / 2 + c[from]));
else dfs(from, step + 1, num );
}
}

int main() {
while (scanf("%d%d%d%d", &c_max, &n, &Index, &m) != EOF) {
V = n + 1;
for (int i = 1; i <= n; i++)scanf("%d", &c[i]);
for (int i = 0; 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(0);
dfs(Index, 0, -c_max / 2 + c[Index]);
cout << min_num_go << " ";
for (int i = 0; i < r.size();i++) {
printf("%d%s", r[i], i + 1 == r.size()? " ":"->");
}
cout << min_num_back << endl;
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: