当前位置: 代码迷 >> 综合 >> PAT甲级-1018 Public Bike Management (30分)
  详细解决方案

PAT甲级-1018 Public Bike Management (30分)

热度:80   发布时间:2023-09-26 23:53:38.0

点击链接PAT甲级-AC全解汇总

题目:
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.

PAT甲级-1018 Public Bike Management (30分)

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.

Input Specification:
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.

Output Specification:
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.

Sample Input:

10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

Sample Output:

3 0->2->3 0

题意:
有一个自行车总站,好几个停车点,输入的时候选中一个停车点,要使得停车点的数量为最多的一半,不管是多了还是少了,从总站出发,沿着时间最少的那条路过去修改数量。其中要满足:

0.路过的每个站点都要变为最多的一半,
1.如果时间一样,选择需要补充最少数量的那条路;
2.如果补充的数量一样,选择拿回数量最少的那条路;
3.不够的从总站补,多出来的可以补后面;

注意: 这道题很坑的就是题目只说了0和1没有说2和3,没有特别说只能往后补,不能往前补考的是case5 6。如果不考虑补的方向,只能对前5case;最后3case考的是选拿回最少的路径;

我的代码:

#include<bits/stdc++.h>
using namespace std;int C,N,S,M;
int min_time_cost=INT_MAX;
int min_bike_need=INT_MAX;
int min_bike_rest=INT_MAX;
int time_cost=0;
int bike_need=0;
int bike_rest=0;
int now_have[520]={
    0};
int roadcost[520][520]={
    0};
deque<int>now_path;
deque<int>res;
bool flag_visited[520]={
    false};void DFS(int start)
{
    if(start==S){
    if((min_time_cost>time_cost)||((min_time_cost==time_cost)&&(min_bike_need>bike_need))||((min_time_cost==time_cost)&&(min_bike_need==bike_need)&&(min_bike_rest>bike_rest))){
    min_time_cost=time_cost;min_bike_need=bike_need;min_bike_rest=bike_rest;res=now_path;}}else{
    for(int i=1;i<=N;i++){
    if(roadcost[start][i]&&flag_visited[i]==false){
    flag_visited[i]=true;now_path.push_back(i);int t_cost=time_cost;//保存这三个数int t_rest=bike_rest;int t_need=bike_need;time_cost+=roadcost[start][i];if(now_have[i]>=C/2)//这个station车多bike_rest+=now_have[i]-C/2;else //这个station车少{
    if(bike_rest>=C/2-now_have[i])//够补bike_rest-=C/2-now_have[i];else//不够补,需要送{
    bike_need+=C/2-now_have[i]-bike_rest;bike_rest=0;}}DFS(i);time_cost=t_cost;//还原三个数bike_need=t_need;bike_rest=t_rest;now_path.pop_back();flag_visited[i]=false;}}}
}int main()
{
    cin>>C>>N>>S>>M;memset(roadcost,0,sizeof(roadcost));for(int i=1;i<=N;i++)cin>>now_have[i];for(int i=0;i<M;i++){
    int a,b,cost;cin>>a>>b>>cost;roadcost[a][b]=cost;roadcost[b][a]=cost;}now_path.push_back(0);DFS(0);printf("%d ",min_bike_need);for(deque<int>::iterator it=res.begin();it!=res.end();it++)if(it!=res.begin())cout<<"->"<<*it;else cout<<*it;printf(" %d",min_bike_rest);return 0;
}
  相关解决方案