• 题目描述

      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存储路径,最后输出,代码如下:

       #include <iostream>
      #include <vector>
      using namespace std; int cmax,n,sp,m;
      int bike[];
      int route[][];
      int visit[];
      int minsend,minback,mindis;
      vector<int> path,npath; //path记录最后结果的路径,npath记录求解过程中的路径 void dfs(int v,int nsend,int nback,int ndis){ //当前访问节点v,需要送来的总车辆nsend,需要带回的总车辆nback,需要的总路程ndis
      int i;
      int send,back,state; visit[v]=;
      npath.push_back(v); if(sp==v){ //到达终点
      if(ndis<mindis){ //路程最短
      path=npath;
      minsend=nsend;
      minback=nback;
      mindis=ndis;
      }
      else if(ndis==mindis){
      if(nsend<minsend){ //需要送来的车最少
      path=npath;
      minsend=nsend;
      minback=nback;
      }
      else if(nsend==minsend && nback<minback){ //需要返回的车最少
      path=npath;
      minback=nback;
      }
      }
      return;
      } for(i=;i<=n;i++){ //访问下一个节点
      if(visit[i]== && route[v][i]!=-){ //未被访问且可访问
      state=bike[i]-cmax; //判断该节点的状态,为正表示需要带回车辆,为负表示需要送车辆
      send=nsend;
      back=nback;
      if(state>=) back+=state;
      else{
      if(nback+state<){ //若前面返回的车辆足够供给该节点,则从返回的车辆中取
      send-=(nback+state);
      back=;
      }
      else back+=state;
      }
      dfs(i,send,back,ndis+route[v][i]);
      npath.pop_back();
      visit[i]=;
      }
      }
      } int main(){ int i,j;
      int x,y,z;
      unsigned int k; scanf("%d%d%d%d",&cmax,&n,&sp,&m);
      cmax/=; //perfect状态 for(i=;i<n;i++) scanf("%d",&bike[i+]); for(i=;i<=n;i++)
      for(j=;j<=n;j++)
      if(i==j) route[i][j]=;
      else route[i][j]=-; //边预处理
      for(i=;i<m;i++){
      scanf("%d%d%d",&x,&y,&z);
      route[x][y]=route[y][x]=z;
      } for(i=;i<=n;i++) visit[i]=;
      minsend=minback=mindis=; dfs(,,,); printf("%d ",minsend);
      for(k=;k < path.size();k++){
      if(k!=) printf("->");
      printf("%d",path[k]);
      }
      printf(" %d",minback); return ;
      }

【PAT甲级】Public Bike Management 题解的更多相关文章

  1. pat 甲级 Public Bike Management

    Public Bike Management (30) 题目描述 There is a public bike service in Hangzhou City which provides grea ...

  2. PAT 1018 Public Bike Management[难]

    链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018  Public ...

  3. PAT 1018. Public Bike Management

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  4. PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  5. PAT 1018 Public Bike Management(Dijkstra 最短路)

    1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  6. [PAT] A1018 Public Bike Management

    [思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...

  7. PAT_A1018#Public Bike Management

    Source: PAT A1018 Public Bike Management (30 分) Description: There is a public bike service in Hangz ...

  8. PAT甲级1018. Public Bike Management

    PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...

  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. ES6-map数据结构,增加、删除、查找 方法(set get has delete clear ) 属性:size

    map数据结构: 本质上是键值对的集合,类似集合: 可以遍历,方法很多,可以跟各种数据格式转换. let json = { name:'ananiah', age:'18' } //效率低 需要遍历j ...

  2. Oracle数据库小知识点整理

    -- 数据库存储数据 -- 市面上主流的数据库有哪些 -- 甲骨文  oracle   mysql --  IBM  db2  金融 --  微软  sqlserver --这些是关系型数据库. -- ...

  3. Android五大布局详解——TableLayout(表格布局)

    TableLayout 前面所学的LinearLayout和RelativeLayout两大布局已经完全适用于各种开发条件下,其他的布局仅供参考学习,毕竟知识就是力量,以后的开发过程中万一遇到也能游刃 ...

  4. linux 执行 javac 报错 javac: command not found

    bash: javac: command not found java 版本 1.8 [root@localhost home]# java -version openjdk version &quo ...

  5. [Go] 轻量服务器框架基础TCP服务模块

    框架要先把整体的结构定义好,一般都是在$GOPATH目录的src下建立自己的目录 zinterface是一些接口的定义 znet就是接口的具体实现 IServer.go package zinterf ...

  6. java学习路线推荐,希望能帮到你

    很多小白刚开始学习java时,肯定迷惘过,因为对java基本是啥也不懂的,一直想知道java的具体学习路线,我曾经也看了许许多多的java经验分享的帖子,评论,以及其他各种培训机构所谓的学习路线,特别 ...

  7. Python关于去除字符串中空格的方法

    Python关于去除字符串中空格的方法 在编写程序时我们经常会遇到需要将字符串中的空格去掉的情况,通常我们可以使用下面几种解决方法: 1.strip()方法:该方法只能把字符串头和尾的空格去掉,但是不 ...

  8. redis(二)集群 redis-cluster & redis主从同步

    参考文档: http://geek.csdn.net/news/detail/200023 redis主从复制:https://blog.csdn.net/imxiangzi/article/deta ...

  9. ESP8266源码分析--打印的基本用法

    缘由: 物联网小芯片中,很多都不是特别复杂,绝大多数问题都需要靠串口打印就能定位的.所以,串口打印是一个非常靠谱的玩意.一定要好好的利用.ESP8266的打印模板设计的特别好,这里我就列出来,供大家参 ...

  10. github仓库迁移到gitlab以及gitlab仓库迁移到另一个gitlab服务器

    一. github仓库迁移到gitlab 先进入 new project: 选择 Import project, 选择下面的github: 进入后,这里需要github的 personal acces ...