1018. Public Bike Management

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.

image

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

题意

给定站点数n(编号为1-n)、每个站点最大容量c、道路数m、问题站点编号s,定义一个站点的最优状态为自行车数正好为c的一半,容量为空或满就说该站点出现问题。现要求找到从自行车管理中心(编号0)到问题站点s的最短路径,并使该路径上所有站点达到最优状态。

思路

典型的求最短路径问题,这里使用 SPFA + DFS,利用SPFA找到所有可能的最短路径,再用DFS遍历枚举所有最短路径,统计需要派出或收回的自行车数。

这个题目有两个比较坑的点:

  1. 仔细阅读题目,会发现其实有三个优先级判断点:题目正文中要求第一优先级为最短路径,第二优先级为派出自行车最少,而在结果输出中有第三优先级,即收回自行车最少。遗漏后两个优先级会导致有的点通不过;
  2. 在统计派出数和收回数时,要注意一个站点中多出来的自行车只能向后面的站点补充,而不能向前面的站点补充,这也是很容易忽视的问题。

代码实现

#include 
#include 
#include 
using namespace std;

const int maxn = 501;
const int INF = 1000000000;

struct node
{
    int v, l;
    node(int _v, int _l): v(_v), l(_l) {}
};

vector<node> adj[maxn];         // 邻接表
int bike[maxn];                 // 存储每个站点现有自行车数
int c, n, s, m;                 // 最大容量、站点数、问题站点编号、道路数

vector<int> pre[maxn];          // 前驱结点数组
int d[maxn];
int inq[maxn] = {false};

vector<int> tempPath, path;     // 路径记录
int sent = INF, take = INF;     // 派送数,回收数

void SPFA()         // 由于不存在负边,所以无需判断负环
{
    queue<int> q;
    for (int i = 1; i <= n; i++)    // 初始化距离
        d[i] = INF;
    d[0] = 0;
    q.push(0);
    inq[0] = true;

    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        inq[u] = false;

        for (int i = 0; i < adj[u].size(); i++)
        {
            int v = adj[u][i].v;
            int l = adj[u][i].l;
            if (v == 0)                     // 0不需要进行判断 
                continue;       
            if (d[u] + l < d[v])            // 松弛        
            {
                d[v] = d[u] + l;
                pre[v].clear();             // 最短距离减小需要先清空前驱结点
                pre[v].push_back(u);
                if (!inq[v])
                {
                    q.push(v);
                    inq[v] = true;
                }
            }
            else if (d[u] + l == d[v])      // 距离相等,只要增加前驱结点
                pre[v].push_back(u);
        }
    }
}

void DFS(int x)
{
    if (x == 0)         // 递归边界
    {
        int remain = 0, tempSent = 0;       // 记录处理完第i个站点时多出的数量,和已经派送的数量
        tempPath.push_back(x);
        for (int i = tempPath.size() - 2; i >= 0; i--)      // 遍历路径
        {
            int u = tempPath[i];
            remain =  remain + bike[u] - c / 2;     // 更新剩余数量
            if (remain < 0)                 // 剩余数量小于0
            {
                tempSent += -remain;        // 当前站点需要派送
                remain = 0;                 // 剩余数量重置为0
            }
        }
        if (tempSent < sent)        // 第二优先级:派送数量少
        {
            path = tempPath;
            sent = tempSent;
            take = remain;
        }
        else if (tempSent == sent && remain < take)     // 第三优先级:回收数量少
        {
            path = tempPath;
            take = remain;
        }
        tempPath.pop_back();
    }

    tempPath.push_back(x);
    for (int i = 0; i < pre[x].size(); i++)         // 处理所有可能的前驱结点
        DFS(pre[x][i]);
    tempPath.pop_back();
}

int main()
{
    int s1, s2, t;

    scanf("%d%d%d%d", &c, &n, &s, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &bike[i]);
    for (int i = 0; i < m; i++)
    {
        scanf("%d%d%d", &s1, &s2, &t);
        node x = node(s2, t);
        node y = node(s1, t);
        adj[s1].push_back(x);
        adj[s2].push_back(y);
    }

    SPFA();
    DFS(s);

    printf("%d ", sent);
    for (int i = path.size() - 1; i >= 0; i--)
    {
        if (i < path.size() - 1)
            printf("->");
        printf("%d", path[i]);
    }
    printf(" %d", take);

    return 0;
}

你可能感兴趣的:(PAT,(Advanced,Level))