• PAT Advanced Level 1018


    1018 Public Bike Management (30)(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 S~3~, we have 2 different shortest paths:

    1. PBMC -> S~1~ -> S~3~. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S~1~ and then take 5 bikes to S~3~, so that both stations will be in perfect conditions.

    2. PBMC -> S~2~ -> S~3~. 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: C~max~ (<= 100), always an even number, is the maximum capacity of each station; N (<= 500), the total number of stations; S~p~, 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 C~i~ (i=1,...N) where each C~i~ is the current number of bikes at S~i~ respectively. Then M lines follow, each contains 3 numbers: S~i~, S~j~, and T~ij~ which describe the time T~ij~ taken to move betwen stations S~i~ and S~j~. 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-&gtS~1~->...-&gtS~p~. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of S~p~ 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-&gt2-&gt3 0
    官网上的题目已经乱成这个样了 还好有书啊
    /**********************
    author: yomi
    date: 18.8.22
    ps: 思路非常清晰 只会简单dijksta的我 不得不停止做题 去学dijkstra+DFS的模板啊
    然后还四hin悲催 本题下标为0~n 我写成0~n-1 找BUG15分钟
    然而还是很开心 因为我以前都是照着题解找BUG
    25分 T^T
    这个这个这个。掉进了陷阱里。
    如果最短路径有多个,求能带的最少的自行车数目的那条。
    如果还是有很多条不同的路,那么就找一个从车站带回的自行车数目最少的。
    带回的时候是不调整的。
    看了题解。。。 我的错误之处就是dfs的核心部分 这部分代码和以前做过的加油站的题挺像
    **********************/
    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <cstring>
    using namespace std;
    const int maxn = 501;
    const int INF = 0x3fffffff;
    int g[maxn][maxn], d[maxn], weight[maxn], minNeed=INF, minRemain=INF;
    vector<int>pre[maxn];
    vector<int>path, temppath;
    int c, n, p, m, optvalue, ans;
    bool vis[maxn];
    void dijk()
    {
        d[0] = 0;
        for(int i=0; i<=n; i++){
            int u=-1, Min=INF;
            for(int j=0; j<=n; j++){
                if(!vis[j] && d[j]<Min){
                    Min = d[j];
                    u = j;
                }
            }
            if(u == -1)
                return;
            vis[u] = true;
            for(int v=0; v<=n; v++){
                if(vis[v]|| g[u][v]==INF){
                    continue;
                }
                if(d[u]+g[u][v]<d[v]){
                    d[v] = d[u]+g[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }
                else if(d[v] == d[u]+g[u][v]){
                    pre[v].push_back(u);
                }
            }
        }
    }
    void dfs(int v)
    {
        if(v == 0){
            temppath.push_back(v);
            int need=0, remain=0;
            for(int i=temppath.size()-1; i>=0; i--){
                int id = temppath[i];
                if(weight[id] > 0){
                    remain += weight[id];
                }
                else{
                    if(remain > abs(weight[id])){
                        remain -= abs(weight[id]);
                    }
                    else{
                        need += abs(weight[id])-remain;
                        remain = 0;
                    }
                }
            }
            if(need < minNeed){
                minNeed = need;
                minRemain = remain;
                path = temppath;
            }
            else if(need==minNeed && remain<minRemain){
                minRemain = remain;
                path = temppath;
            }
    
            temppath.pop_back();
            return;
        }
        temppath.push_back(v);
        int s = pre[v].size();
        for(int i=0; i<s; i++){
            dfs(pre[v][i]);
        }
        temppath.pop_back();
    }
    int main()
    {
        fill(d, d+maxn, INF);
        fill(g[0], g[0]+maxn*maxn, INF);
        fill(weight, weight+maxn, 0);
        cin >> c >> n >> p >> m;
        int da, per=c/2;
        int u, v, w;
        optvalue = INF;
        for(int i=1; i<=n; i++){
            cin >> da;
            weight[i] = da-per;
        }
        for(int i=0; i<m; i++){
            cin >> u >> v >> w;
            g[u][v] = g[v][u] = w;
        }
        dijk();
        dfs(p);
        cout << minNeed << ' ';
        for(int i=path.size()-1; i>0; i--){
            cout << path[i] << "->";
        }
        cout << p << ' ';
        cout << minRemain;
    
        return 0;
    }
    /**
    10 3 3 5
    6 7 0
    0 1 1
    0 2 1
    0 3 3
    1 3 1
    2 3 1
    **/
    
    
    


  • 相关阅读:
    单步调试及回滚测试
    程序员的自我修养阅读笔记03
    第八周总结
    NABCD项目分析
    程序员的自我修养阅读笔记02
    第七周总结
    程序员的自我修养阅读笔记01
    第六周总结
    结对地铁开发
    第五周总结
  • 原文地址:https://www.cnblogs.com/AbsolutelyPerfect/p/9519186.html
Copyright © 2020-2023  润新知