PAT甲级(Advanced Level) Practice 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.

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

题目翻译

杭州市的公共自行车服务为世界各地的游客提供了极大的便利。游客可在任意站点租用自行车,并在市内任意站点还车。

公共自行车管理中心(PBMC)对所有站点的容量进行实时监控。如果一个站点正好是半满,则表示该站点处于最佳状态。如果一个站点满了或空了,公共自行车管理中心就会收集或发送自行车,将该站点的状况调整到最佳状态。此外,途中的所有站点也会进行调整。

当收到问题站点的报告时,PBMC 总是会选择最短路径到达该站点。如果有多条最短路径,则会选择需要从 PBMC 发送最少单车的路径。

上图是一个示例。站点由顶点表示,道路则对应于边。边上的数字表示从一个终点站到达另一个终点站所需的时间。写在顶点 S 内的数字是当前存放在 S 的自行车数量。要解决 S3 的问题,我们有 2 条不同的最短路径:

输入规范:

每个输入文件包含一个测试案例。每个案例的第一行包含 4 个数字: Cmax(≤100),总是偶数,是每个站点的最大容量;N(≤500),站点总数;Sp,问题站点的索引(站点编号从 1 到 N,PBMC 由顶点 0 表示);M,道路数量。第二行包含 N 个非负数 Ci(i=1,⋯,N),每个 Ci 分别代表 Si 当前的自行车数量。然后是 M 行,每行包含 3 个数字: Si、Sj 和 Tij,它们描述了在站点 Si 和 Sj 之间移动所需的时间 Tij。一行中的所有数字之间用空格隔开。

输出规范:

对于每个测试案例,请在一行中打印结果。首先输出 PBMC 必须发送的自行车数量。然后在一个空格后,以下列格式输出路径: 0->S1->⋯->Sp。最后,在另一个空格后,输出 Sp 条件调整为完美后,我们必须送回 PBMC 的自行车数量。

注意,如果该路径不是唯一的,则输出我们必须带回 PBMC 的自行车数量最少的路径。法官的数据保证这样的路径是唯一的。

解题思路

我刚看到这道题的想法是用dijkstra求最短路,但是卡在找出带出且带回最少的路径。

在网上找了一下发现大家是用dfs来求的,根据这个思路想了想,发现了dijkstra的一个规律:

if(dist[i] == dist[j] + distance(i, j)), 则i在j的最短路路径上,并且i是j的前一个点。这也是本题dfs的核心。

还有一个难点:怎么计算一条路径带出的和带回的数量。我的方法是记录 dfs到的点(u)身边带着的车的数量 和 带出来的车的数量。这样到达最后一个点时身边剩下的车就是要带回的车数量。

到达新的点时,计算该点需要增加车的数量(可以是负数),如果是正数,则手上的车减少(不够就减则增加带出来的车数量);如果为负数,增加手上车的数量。

这道题的dijkstra就不加注释了,看不懂的可以去看看1003 Emergency

代码(c++)

#include <bits/stdc++.h>
#include <vector>

using namespace std;

const int N = 510;

int c, n, s, m;
int bike[N];
int dist[N], g[N][N];
bool st[N];
int min_bring = 1e9, min_back = 1e9;
vector<int> ans, path;

void dijkstra() {
    memset(dist, 0x3f, sizeof dist);
    dist[0] = 0;

    for(int i = 0; i < n; i++) {
        int t = -1;

        for(int j = 0; j <= n; j++) 
            if(!st[j] && (t == -1 || dist[j] < dist[t])) t = j;
        
        st[t] = true;
        for(int j = 0; j <= n; j++) 
            dist[j] = min(dist[j], dist[t] + g[t][j]);
    }
}

void dfs(int u, int bring, int have) {
    if(u == s) {                 // 到达s点,结算
        // bring是带出来的车辆数,have是手上的车数,刚刚说过结算时就是带走的车数
        if(bring < min_bring || (bring == min_bring && have < min_back))
            ans = path, min_bring = bring, min_back = have;
        
        return;
    }

    for(int i = 0; i <= n; i++) {
        if(dist[i] == g[u][i] + dist[u]) {          // 按最短路找下一个点
            int new_have = 0, new_bring = bring;
            new_have =  have + bike[i] - c / 2;
            if(new_have < 0) new_bring = bring - new_have, new_have = 0;
            
            path.push_back(i);
            dfs(i, new_bring, new_have);
            path.pop_back();
        }
    }
}

int main() {
    scanf("%d%d%d%d", &c, &n, &s, &m);
    for(int i = 1; i <= n; i++) scanf("%d", &bike[i]);

    memset(g, 0x3f, sizeof g);
    for(int i = 0; i < m; i++) {
        int a, b, w;
        scanf("%d%d%d", &a, &b, &w);
        g[a][b] = min(g[a][b], w);
        g[b][a] = min(g[b][a], w);
    }

    dijkstra();
    dfs(0, 0, 0);

    printf("%d 0", min_bring);
    for(int i = 0; i < ans.size(); ++ i) printf("->%d", ans[i]);
    printf(" %d\n", min_back);

    return 0;
}

  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值