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

img

The above figure 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 is the current number of bikes stored at . Given that the maximum capacity of each station is 10. To solve the problem at , we have 2 different shortest paths:

  1. PBMC -> -> . In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from and then take 5 bikes to , so that both stations will be in perfect conditions.
  2. PBMC -> -> . 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: , always an even number, is the maximum capacity of each station; , the total number of stations; , the index of the problem station (the stations are numbered from 1 to , and PBMC is represented by the vertex 0); and , the number of roads. The second line contains non-negative numbers where each is the current number of bikes at respectively. Then lines follow, each contains 3 numbers: , , and which describe the time taken to move betwen stations and . 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−> −>⋯−> . Finally after another space, output the number of bikes that we must take back to PBMC after the condition of 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

Ω

极繁翻译:PBMC是杭州公共自行车管理部门,负责管理平衡各个自行车租凭点的自行车数量。当一个租凭点的自行车数量刚好是最大数量的一半时,我们认为是恰好的。当一个站点上报至PBMC需要调整其自行车数量时,PBMC会选择一条耗时最短的路前往,同时将沿途站点的自行车数量也调整至恰好的状态。在前往的途中,途经站点多出来的自行车可以填补到后面需要的站点,这样带回PBMC的自行车就会少一些。注意,返回途中不调整任何站点。如果有多条耗时相同的路径则选择带上自行车数量最少的路,如果还是有相同的,则选择带回自行车最少的路。现给出站点的最大容量、站点数量、上报的站点编号、各条路径的耗时情况,需要输出最优路线以及需要带上和带回的自行车数。题目保证最优路径唯一。

这题目,有毒。深刻理解什么叫,有坑的地方就有我。

理解错题意,重构了几次代码,废了好多时间。做完再仔细阅读题目,感觉还是没get到那几个坑:

  1. 回去的路上不能调整各站点自行车,只能在来的路上用前面站点多出来的自行车填补后面的站点,也就是说后面站点多出来不能填补前面的站点,只能带回去。这是什么人工智障?PBMC早就知道各个站点的自行车数量,为什么不能做到全局规划呢(格局小了,锻炼编程能力而已,以后还要面对各种甲方爸爸呢
  2. 输出要求里说,如果路径不唯一,则输出带回数量最少的路径。当时我也纳闷,因为前面举的那个例子里面选择的是带上数量最少的路。结果其实是先选择带上最少的,再选择带回最少的。。(请打开麦克风好好交流

总之,不懂。

显然,这不是一道单纯的Dijkstra算法题,Dijkstra的基本思想本质上是广度优先搜索BFS,但只能找到一条最短路,而我们需要找到所有最短路,因此需要加以改进。

Dijkstra算法中,当一个新点被纳入起点所在的集合中时,我们会更新与该点相连的点到起点的距离,如果更新的距离≥ 原距离的话就不需要更新。那么在这个问题中,如果更新的距离==原距离,我们依然需要记录,从而保留所有长度相同的路径信息。

首先我们需要一个vector来记录所有站点到PBMC(编号为0)的最短距离(这里的距离是时间),并初始化所有距离为最大值INF

vector<int> dist(n + 1, INF); // #define INF INT32_MAX

然后初始化的时候需要将所有与PBMC相连的站点dist修改为他们之间的距离。

另外还需要标记各个站点是否已被纳入集合 ,其中 表示所有已经求出最短距离的站点编号,初始情况下只含0。

vector<bool> isVisited(n + 1, false);
isVisited[0] = true;

最重要的我们还需要存储路径,由于最后是根据带上和带回的自行车数进行最优选择,因此也一并存了,因此诞生了一个究极复杂的类型:

// vector<vector<pair<pair<send num,take back num>,path points>>>
vector<vector<pair<pair<int, int>, vector<int>>>> track(n + 1);

简单地说,就是所有站点都拥有一个vector,里面存储了PBMC到该站点所有最短路径的信息,每个最短路径信息是一个pair<pair<int,int>,vector<int>>,后面的vector<int>就是这条路径上依次的站点编号,前面pair<int,int>则是这条路径到当前站点所必须带上和带回的自行车数量。

接下来每次循环都从dist中找到距离最小的站点min_idx,将该站点编号纳入集合 。在更新与该点相连的站点dist[i]时,如果更短了,那么更新dist[i]并将其track[i]清空,然后进行“如果是相等的”的操作;如果是相等的,说明又找到了一条最短路径,那么将track[min_idx]中的每一条记录更新之后推入track[i]。也就是说,track[i]中存储的路径长度均为dist[i],因此修改dist[i]时必须清空track[i]

另外num_to_perfect[i]存储站点 需要多少辆自行车才能到达恰好的状态,为负则表示多出来的自行车数量:

num_to_perfect[i] = max_cap / 2 - num_to_perfect[i];

C☺DE

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

#define INF INT32_MAX

using namespace std;
typedef pair<int, int> info;
typedef pair<info, vector<int>> path;

bool cmp(path &a, path &b)
{return a.first.first < b.first.first || a.first.first == b.first.first && a.first.second < b.first.second;}

int main()
{
    int max_cap, n, sp, m, s1, s2, t;
    cin >> max_cap >> n >> sp >> m;
    vector<int> num_to_perfect(n + 1, 0), dist(n + 1, INF);
    for (int i = 1; i < n + 1; ++i)
    {
        cin >> num_to_perfect[i];
        num_to_perfect[i] = max_cap / 2 - num_to_perfect[i];
    }
    map<int, vector<pair<int, int>>> road;
    for (int i = 0; i < m; ++i)
    {
        cin >> s1 >> s2 >> t;
        road[s1].emplace_back(s2, t);
        road[s2].emplace_back(s1, t);
    }
    vector<vector<path>> track(n + 1);
    vector<bool> isVisited(n + 1, false);
    isVisited[0] = true;
    int min_dist = INF, min_idx = 0;
  //initialization
    for (auto &s: road[0])
    {
        dist[s.first] = s.second;
        info tmp = move(num_to_perfect[s.first] < 0 ? info{0, -num_to_perfect[s.first]} : info{num_to_perfect[s.first], 0});
        track[s.first].emplace_back(tmp, vector<int>{s.first});
        min_dist = s.second < min_dist ? (min_idx = s.first, s.second) : min_dist;
    }
    isVisited[min_idx] = true;
    while (!isVisited[sp])
    {
      // update dist[i] for i in neighbor of min_idx
        for (auto &d: road[min_idx])
        {
            if (isVisited[d.first] || dist[min_idx] + d.second > dist[d.first])
                continue;
            else if (dist[min_idx] + d.second < dist[d.first])
            {
                track[d.first].clear();
                dist[d.first] = dist[min_idx] + d.second;
            }
          // 将min_idx的路径信息延续到d.first
            // k不能是引用,会直接修改track[min_idx]
            for (auto k: track[min_idx])
            {
                k.second.push_back(d.first);
              // 包括了num_to_perfect[d.first]<0的情况
                if (num_to_perfect[d.first] <= k.first.second)
                    k.first.second -= num_to_perfect[d.first];
                else
                {
                    k.first.first += (num_to_perfect[d.first] - k.first.second);
                    k.first.second = 0;
                }
                track[d.first].push_back(k);
            }
        }
      // 更新完后min_idx的路径信息可以清空以减少空间消耗
        track[min_idx].clear();
        min_dist = INF, min_idx = 0;
        for (int i = 1; i < n + 1; ++i)
            min_dist = (!isVisited[i] && dist[i] < min_dist) ? (min_idx = i, dist[i]) : min_dist;
        isVisited[min_idx] = true;
    }

    sort(track[sp].begin(), track[sp].end(), cmp);
    cout << track[sp][0].first.first << " 0";
    for (auto &s: track[sp][0].second)
        cout << "->" << s;
    cout << " " << track[sp][0].first.second;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值