1. 程式人生 > >PAT甲級1018. Public Bike Management (30)

PAT甲級1018. 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.
Input Specification:

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.
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->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.
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

分析:
用DFS。
關鍵在於如何更新當前需要派出的自行車數目currentSend和當前需要送回的自行車數目currentBack。這兩個量必須要分開考慮,並不能進行相加。它們每經過一個自行車站點,就會根據供求關係進行累加。
舉個例子,考慮和題目的例子相反的情形,PBMC->S1->S2這條路線上,S1的單車數為0,S2的單車數為6。從PBMC到S1,需要派出5輛車(+5),再從S1到S2,需要送回1輛車(-1),相加後是+4,所以從PBMC到S2需要派出4輛車;但事實是,從PBMC到S2,還是需要派出5輛車,否則S1的需求就得不到滿足。

#include <cstdio>
using namespace std;
#include <vector>

const int MAX=500+10;
const int INF=(1<<30);
int Bike[MAX]={0};
int vis[MAX]={0};
int Time[MAX][MAX]={0};
vector<int> path,ans;
int Cmax,N,Sp,M;
int minTime=INF,minSend=INF,minBack=INF;
int currentTime=0,currentSend=0,currentBack=0;

void DFS(int station){
    if(currentTime>minTime) return;
    if(station==Sp){
    /*優先順序:路上花費的時間 > 需要派出的自行車數量 > 需要帶回的自行車數量*/
        if(currentTime<minTime||
        (currentTime==minTime&&currentSend<minSend)||
        (currentTime==minTime&&currentSend==minSend&&currentBack<minBack)){
            minTime=currentTime;
            minSend=currentSend;
            minBack=currentBack;
            ans=path;
        }
        return;
    }
    for(int i=1;i<=N;i++){
        if(vis[i]==0&&Time[station][i]!=0){
            currentTime+=Time[station][i];
            path.push_back(i);
            vis[i]=1;

            /*用lastSend,lastBack儲存目前的currentSend和currentBack,便於後面的回溯*/
            int lastSend=currentSend,lastBack=currentBack; 
            /*供過於求*/
            if(currentBack>=Cmax/2-Bike[i]){ 
                 currentBack+=(Bike[i]-Cmax/2);
            }
            /*供不應求*/
            else{ 
                currentSend+=Cmax/2-Bike[i]-currentBack;
                currentBack=0;
            }

            DFS(i);

            currentTime-=Time[station][i];
            path.pop_back();
            vis[i]=0;   
            currentSend=lastSend;
            currentBack=lastBack;
        }
    }
}

int main(){ 
  scanf("%d %d %d %d",&Cmax,&N,&Sp,&M);

  for(int i=1;i<=N;i++) scanf("%d",&Bike[i]);

  for(int i=0;i<M;i++){
    int Si,Sj,Tij;
    scanf("%d %d %d",&Si,&Sj,&Tij);
    Time[Si][Sj]=Tij;
    Time[Sj][Si]=Tij;
  }

  vis[0]=1;
  DFS(0);

  printf("%d 0",minSend);
  for(int i=0;i<ans.size();i++){
    printf("->%d",ans[i]);
  }
  printf(" %d",minBack);

  return 0;
}

相關推薦

PAT甲級1018 Public Bike Management (30)

1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from

PAT甲級1018. 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 r

【未完成】【笨方法學PAT1018 Public Bike Management30 分)

一、題目 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

PAT Advanced Level 1018. Public Bike Management (30)(Java and C++)

PAT Advanced Level 1018. Public Bike Management (30) 最短路徑問題。題意有三:1.時間最短 2.送出車輛最少 3.回收車輛最少     ps:(注意我的lessThan函式) 我的思路:是 SPFA(求出最短路徑)

1018. Public Bike Management (30)-PAT

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 stat

1018 Public Bike Management (30)

const ios perf orm repr sts div break tput There is a public bike service in Hangzhou City which provides great convenience to the touris

1018. 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

1018 . Public Bike Management (30)自己寫的,只有14分

#include<iostream> #include<vector> using namespace std; class CA { public:  enum{C=100,N=501,INF=0x6FFFFFFF};  void run();  

1018 Public Bike Management (30)(30 分)(C++)

1018 Public Bike Management (30)(30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists f

1018 Public Bike Management (30) Dijkstra算法 + DFS

== net pub blog sta 認識 pop false inf 題目及題解 https://blog.csdn.net/CV_Jason/article/details/81385228 迪傑斯特拉重新認識 兩個核心的存儲結構: int dis[n]

PAT 1018 Public Bike Management30 分)

1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from all ove

Public Bike Management (30)(DFS,VRCTOR,模擬)(PAT甲級

continue mes ret esp 之間 長度 space 修改 優先 #include<bits/stdc++.h>using namespace std;const int inf = 1e9;int sum,n,tar,m;int num[507];

1018 Public Bike Management30 分)(Dij+DFS)

1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over th

1018 Public Bike Management30 分)

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

1018 Public Bike Management30 分)(圖的遍歷and最短路徑)

    這題不能直接在Dijkstra中寫這個第一 標尺和第二標尺的要求 因為這是需要完整路徑以後才能計算的  所以寫完後可以在遍歷   #include<bits/stdc++.h> using namespace std; int c

PAT 1018 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 b

pat 1018. Public Bike Management (dijkstra+dfs)

最短路問題,複習dijkstra和dfs 首先dijkstra求出最短路,然後dfs求出所有路徑 求出路徑中send最小的,再求其中back最小的 point:題設的要求是修正最短路徑上的所有點,實際上必須嚴格遵循訪問順序修改每個點的權值(eg:要依次修改權值分別為3,1

PAT 1018 Public Bike Management(Dijkstra 最短路)

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 stat

PATA 1018 Public Bike Management30 分)解題報告

1018 Public Bike Management(30 分) There is a public bike service in Hangzhou City which provides great convenience to the tourists from a

【ACM】- PAT. 1018 Public Bike Management 【圖

題目連結 題目分析 給出結點資訊,輸出最短路徑; 總站編號為0,其他站點編號為1-N 多條最短路徑時,其他標尺: 在最短路徑過程中,必須把每個結點的權值調整到題目要求的最佳; 標尺一:選擇需要從總站帶出最少量的路徑 標尺二:仍有多條,則選擇需要