zoukankan      html  css  js  c++  java
  • PAT.Public bike management(SPFA + DFS)

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

    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​​ (≤), always an even number, is the maximum capacity of each station; N (≤), 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​​ (,) 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. 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

    这题需要注意两个点

    第一点:当存在多条最短路sent最少时,输出back最小的那条最短路。也就是说优先级有三个。

    第二点:记住回溯保存中间状态。


    /*
        本题思路:
            1:求出最短路径
            2:利用dfs,求出每条最短路的具体路径,以及在某条路径上需要派遣和遣返
                的自行车的数量。
            3:将所有路径按照,派遣数目增序进行排序,派遣数目相同的按照遣返数目
                的增序进行排列,输出第一个答案。
    */
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <queue>
    #include <algorithm>
    using namespace std;
    
    const int maxn = 500 + 5, inf = 0x3f3f3f3f;
    
    int t, c;
    
    struct Edge {
        int to, cost, next;
    } edges[maxn * maxn];
    
    int head[maxn], cnt;
    
    void addedge(int u, int v, int w) {
        edges[cnt] = Edge{v, w, head[u]};
        head[u] = cnt ++;
        edges[cnt] = Edge{u, w, head[v]};
        head[v] = cnt ++;
    }
    
    void init() {
        memset(head, -1, sizeof head);
        cnt = 0;
    }
    
    bool vis[maxn];
    
    int dist[maxn], cap[maxn];
    
    void spfa(int s, int t) {
        memset(dist, inf, sizeof dist);
        memset(vis, false, sizeof vis);
        queue <int> que;
        que.push(s);
        vis[s] = true;
        dist[s] = 0;
        while(!que.empty()) {
            int u = que.front();
            que.pop();
            vis[u] = false;
            for(int k = head[u]; ~k; k = edges[k].next) {
                int v = edges[k].to;
                if(dist[v] > dist[u] + edges[k].cost) {
                    dist[v] = dist[u] + edges[k].cost;
                    if(!vis[v]) {
                        vis[v] = true;
                        que.push(v);
                    }
                }
            }
        }
    }
    
    struct Ans {
        int sent, back;
        vector <int> path; 
    } ans[maxn * maxn];
    int num;
    
    bool cmp(Ans a, Ans b) {
        if(a.sent == b.sent) return a.back < b.back;
        else return a.sent < b.sent;
    }
    
    int pre[maxn];
    
    void dfs(int u) {
        if(u == t) {
            int now = u;
            while(~ now) {
                ans[num].path.push_back(now);
                now = pre[now];
            }
            num ++;
            return;
        }
        for(int k = head[u]; ~k; k = edges[k].next) {
            int v = edges[k].to;
            int temp1 = ans[num].sent;
            int temp2 = ans[num].back;
            if(dist[v] == dist[u] + edges[k].cost) {
                pre[v] = u;
                if(cap[v] > c / 2) {
                    ans[num].back += cap[v] - c / 2;
                }
                else {
                    if(ans[num].back  > c / 2 - cap[v]) ans[num].back  -= (c / 2 - cap[v]);
                    else {
                        ans[num].sent += c / 2 - cap[v] - ans[num].back ; 
                        ans[num].back  = 0;
                    }
                }
                dfs(v);
                ans[num].sent = temp1;
                ans[num].back = temp2;
            }
        }
    }
    
    int main() {
        init();
        int n, m, u, v, w;
        scanf("%d %d %d %d", &c, &n, &t, &m);
        for(int i = 1; i <= n; i ++) scanf("%d", &cap[i]);
        while(m --) {
            scanf("%d %d %d", &u, &v, &w);
            addedge(u, v, w);
        }
        memset(pre, -1, sizeof pre);
        spfa(0, t);
        num = 0;
        dfs(0);
        sort(ans, ans + num, cmp);
        printf("%d ", ans[0].sent);
        for(int i = ans[0].path.size() - 1; i > 0; i --) {
            printf("%d->", ans[0].path[i]);
        }
        printf("%d", t);
        printf(" %d
    ", ans[0].back);
        return 0;
    }


  • 相关阅读:
    剑指offer---链表中倒数第k个结点
    剑指offer---反转链表
    剑指offer---从尾到头打印链表
    数据结构---链表ADT C++实现
    ubuntu解压zip文件出现乱码情况解决方法
    Ubuntu终端常用的快捷键(转载)
    requsets模块的学习
    爬虫的基本知识
    谈谈我们对userAgent的看法,为什么爬虫中需要userAgent?
    git的基本使用
  • 原文地址:https://www.cnblogs.com/bianjunting/p/12546178.html
Copyright © 2011-2022 走看看