当前位置: 代码迷 >> 综合 >> [PAT A1018]Public Bike Management
  详细解决方案

[PAT A1018]Public Bike Management

热度:76   发布时间:2023-12-15 06:08:08.0

[PAT A1018]Public Bike Management

题目描述

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 S?3??, we have 2 different shortest paths:

  1. PBMC -> S?1?? -> S?3??. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S?1?? and then take 5 bikes to S?3??, so that both stations will be in perfect conditions.

  2. PBMC -> S?2?? -> S?3??. 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: C?max?? (≤100), always an even number, is the maximum capacity of each station; N (≤500), the total number of stations; S?p??, 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 C?i?? (i=1,?,N) where each C?i?? is the current number of bikes at S?i?? respectively. Then M lines follow, each contains 3 numbers: S?i??, S?j??, and T?ij?? which describe the time T?ij?? taken to move betwen stations S?i?? and S?j??. 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?>S?1???>??>S?p??. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of S?p?? 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.首先题目就看得很闹心,因为很长,而且需要理解,这里我来帮大家梳理一下:给出cmax,n,m,v,这里引入一个概念,即cmax是一个偶数,表示最大容量,当某地的自行车数目为cmax/2时,我们称其为“完美状态”,n表示除起点外结点的个数,m表示要调整的结点,v,表示边数。然后下一行,输入每个结点的自行车数量;然后下v行,输入边和路径长度。要我们找到最短路径,并且用need表示从起点(默认为0)到终点需要携带的单车数量,remain表示到终点时带回的自行车数量。要求满足最短路径上所有的结点都调整为“完美状态”,如果有多条最短路径,输出a最小的,如果有a相等的,输出b最小的。

2.解题思路就是:Dijkstra+DFS,Dijkstra我写的还是很熟练了,但是对于递归写的DFS,还是有点难理解,还是要多刷题。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 510;
const int INF = 1000000000;  //设置最大值
int cmax, n, m, v;      
int Graph[maxn][maxn], w[maxn];  //邻接表存储
int d[maxn], visit[maxn];  //最短路径数组和访问数组
int need = 0, remain = 0;  //某个状态时的需求量和手中的自行车余量
int min_need = INF, min_remain = INF;  //最短路径的最小需求量和访问结束后的自行车余量
vector<int> pre[maxn];  //存放前驱节点
vector<int> road, temproad; //最短路劲和temp(暂存)路径
void Dijkstra(int s);
void DFS(int s);
int main()
{fill(Graph[0], Graph[0] + maxn * maxn, INF);fill(d, d + maxn, INF);scanf("%d%d%d%d", &cmax, &n, &m, &v);for (int i = 1; i <= n; i++) {scanf("%d", &w[i]);w[i] -= cmax / 2;   //这里是一个非常好的处理方法,即减去cmax/2,如果为正表示我能从该结点拿走的自行车数量,为负表示要给该结点放的自行车数量}for (int i = 1; i <= v; i++) {int a, b, len;scanf("%d%d%d", &a, &b, &len);Graph[a][b] = Graph[b][a] = len;}Dijkstra(0);DFS(m);printf("%d ", min_need);for (int i = road.size() - 1; i >= 0; i--) {printf("%d", road[i]);if (i > 0) printf("->");}printf(" %d", min_remain);return 0;
}
void Dijkstra(int s) {  //必须牢记迪杰斯特拉算法的代码,节省做题时间d[s] = 0;for (int i = 0; i <= n; i++) {int u = -1, min = INF;for (int j = 0; j <= n; j++) {if (min > d[j] && visit[j] == false) {u = j;min = d[j];}}if (u == -1)return;visit[u] = 1;for (int v = 0; v <= n; v++) {if (Graph[u][v] != INF && visit[v] == false) {if (Graph[u][v] + d[u] < d[v]) {d[v] = Graph[u][v] + d[u];pre[v].clear();pre[v].push_back(u);}else if (Graph[u][v] + d[u] == d[v]) {pre[v].push_back(u);}}}}
}
void DFS(int s) {if (s == 0) {      //如果当前结点点是根结点temproad.push_back(s);int need = 0, remain = 0;  //模拟过程for (int i = temproad.size() - 1; i >= 0; i--) { //注意,由于是倒序,所以开始结点放在最后,要逆序访问int id = temproad[i];if (w[id] > 0) {  //该结点有空余的自行车remain += w[id]; //拿走多余的}else {if (remain + w[id] > 0) remain += w[id];  //如果需要放了之后手里有多余的else {                        //放了后手里没有多余的need -= remain + w[id];   //这时就要从起点补,自带remain = 0;         //所有都放了,remain为0}}}if (need < min_need) {min_need = need;min_remain = remain;road = temproad;}else if (need == min_need && remain < min_remain) {min_remain = remain;road = temproad;}temproad.pop_back();return;}temproad.push_back(s);for (int i = 0; i < pre[s].size(); i++) DFS(pre[s][i]);temproad.pop_back();return;
}

 

  相关解决方案