【PAT甲级】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.

      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


    • 题目要求:
    • 从PMBC出发,到终点Sp,找出一条最短的路径,并将路径上的每个站单车数量调整到最佳状态(即半满)。这需要你从PMBC携带一些车出发,调整路上的每个站的单车,少则补,多则带走,到终点可能还需带回一些单车。如果最短的路径有多条,选需要送去的车最少的一条;如果需要送去的车一样少的路径还不止一条,选需要带回的车最少的一条,保证这种情况只有一条路径。最后要输出需要送去的单车数,这条路径经过的每个站,以及最后要带回的单车数

    • 解题方法:
      这道题可以使用深搜结合回溯算法,从PMBC(节点0)开始搜索,找出最终符合条件的路径,并用vector存储路径,最后输出,代码如下:

       1 #include <iostream>
       2 #include <vector>
       3 using namespace std;
       4 
       5 int cmax,n,sp,m;
       6 int bike[505];
       7 int route[505][505];
       8 int visit[505];
       9 int minsend,minback,mindis;
      10 vector<int> path,npath;    //path记录最后结果的路径,npath记录求解过程中的路径
      11 
      12 void dfs(int v,int nsend,int nback,int ndis){    //当前访问节点v,需要送来的总车辆nsend,需要带回的总车辆nback,需要的总路程ndis
      13     int i;
      14     int send,back,state;
      15 
      16     visit[v]=1;
      17     npath.push_back(v);
      18 
      19     if(sp==v){    //到达终点
      20         if(ndis<mindis){    //路程最短
      21             path=npath;
      22             minsend=nsend;
      23             minback=nback;
      24             mindis=ndis;
      25         }
      26         else if(ndis==mindis){
      27             if(nsend<minsend){    //需要送来的车最少
      28                 path=npath;
      29                 minsend=nsend;
      30                 minback=nback;
      31             }
      32             else if(nsend==minsend && nback<minback){    //需要返回的车最少
      33                 path=npath;
      34                 minback=nback;
      35             }
      36         }
      37         return;
      38     }
      39 
      40     for(i=1;i<=n;i++){    //访问下一个节点
      41         if(visit[i]==0 && route[v][i]!=-1){    //未被访问且可访问
      42             state=bike[i]-cmax;    //判断该节点的状态,为正表示需要带回车辆,为负表示需要送车辆
      43             send=nsend;
      44             back=nback;
      45             if(state>=0)    back+=state;
      46             else{
      47                 if(nback+state<0){    //若前面返回的车辆足够供给该节点,则从返回的车辆中取
      48                     send-=(nback+state);
      49                     back=0;
      50                 }
      51                 else back+=state;
      52             }
      53             dfs(i,send,back,ndis+route[v][i]);
      54             npath.pop_back();
      55             visit[i]=0;
      56         }
      57     }
      58 }
      59 
      60 int main(){
      61 
      62     int i,j;
      63     int x,y,z;
      64     unsigned int k;
      65 
      66     scanf("%d%d%d%d",&cmax,&n,&sp,&m);
      67     cmax/=2;    //perfect状态
      68 
      69     for(i=0;i<n;i++)    scanf("%d",&bike[i+1]);
      70     
      71     for(i=0;i<=n;i++)
      72         for(j=0;j<=n;j++)
      73             if(i==j)    route[i][j]=0;
      74             else route[i][j]=-1;    //边预处理
      75     for(i=0;i<m;i++){
      76         scanf("%d%d%d",&x,&y,&z);
      77         route[x][y]=route[y][x]=z;
      78     }
      79 
      80     for(i=0;i<=n;i++)    visit[i]=0;
      81     minsend=minback=mindis=100007;
      82     
      83     dfs(0,0,0,0);
      84 
      85     printf("%d ",minsend);
      86     for(k=0;k < path.size();k++){
      87         if(k!=0)    printf("->");
      88         printf("%d",path[k]);
      89     }
      90     printf(" %d",minback);
      91 
      92     return 0;
      93 }

       


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM