PAT甲级练习题A1018. 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遍历的方法暴力解决,找到最短路径。
注意这里有三个优先级递减的条件,总时间,发送的自行车数,带回的自行车数;

还可使用Dijkstra方法求最短路径,参考Occult的代码:
1. 使用Dijkstra方法求出从0节点到图中每一节点的最短距离,这里并不直接得出pre链,后面使用dis[x]==dis[i]+map[x][i]判断最短路径。
2. 使用以从0节点送出bike数为二分法标的,求得最小的的值q;
3. 然后再运行一次求得最小的运回的bike数ans;
4. 最后根据前面求得q和ans,反向从目标节点DFS至0节点,找到目标路径。

代码

#include
#include
using namespace std;
const int N_max = 500 + 5;
vector<vector<int> >roads(N_max + 1, vector<int>(N_max + 1, -1));
vector<int> current(N_max, 0);
int C_max, N, S_p, M;
int min_t = 1<<15, min_sent_bike = 1<<15;
int back_bike, min_back_bike=1<<15;
vector<int>best_path;

int take_bike(vector<int>&path)
{
    int min_take = 0, bike_car = 0;
    for (int i = 1; i < path.size(); ++i)
    {
        bike_car += current[path[i]] - C_max / 2;
        if (bike_car < min_take)
        {
            min_take = bike_car;
        }
    }
    back_bike = -min_take + bike_car;
    return -min_take;
}

void DFS(int Si,vector<int>path,int curr_t)
{
    for(auto it:path)
    {
        if (it == Si)
            return;
    }
    if (Si != 0)
    {
        curr_t += roads[Si][path[path.size() - 1]];
    }

    if (curr_t > min_t)
        return;

    path.push_back(Si);
    if (Si == S_p)
    { 
        int take_b = take_bike(path);
        if (curr_t < min_t )
        {
            min_t = curr_t;
            min_sent_bike = take_b;
            min_back_bike = back_bike;
            best_path = path;
        }
        else if (curr_t == min_t&&take_b < min_sent_bike)
        {
            min_t = curr_t;
            min_sent_bike = take_b;
            min_back_bike = back_bike;
            best_path = path;
        }
        else if (curr_t == min_t&&take_b == min_sent_bike&&back_bike < min_back_bike)
        {
            min_t = curr_t;
            min_sent_bike = take_b;
            min_back_bike = back_bike;
            best_path = path;
        }
        return;
    }

    for (int i = 1; i <= N; ++i)
    {
        if (roads[Si][i] != -1)
        {
            DFS(i, path, curr_t);
        }
    }
    return;
}

int main()
{
    cin >> C_max >> N >> S_p >> M;
    for (int i = 1; i < N + 1; ++i)
    {
        cin >> current[i];
    }
    for (int i = 0; i < M; ++i)
    {
        int s1, s2, t;
        cin >> s1 >> s2 >> t;
        roads[s1][s2] = t;
        roads[s2][s1] = t;
    }
    vector<int>path;
    DFS(0, path, 0);

    cout << min_sent_bike<<" 0";
    for (int i = 1; i < best_path.size(); ++i)
    {
        cout << "->" << best_path[i];
    }
    cout << " " << min_back_bike << endl;

    system("pause");
    return 0;

}

你可能感兴趣的:(PAT甲级,图)