码迷,mamicode.com
首页 > 其他好文 > 详细

PAT.Public bike management(SPFA + DFS)

时间:2020-03-22 15:52:07      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:ant   city   when   current   only   unique   http   利用   using   

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 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?? (≤), always an even number, is the maximum capacity of each station; N (≤), 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?? (,) 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. 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->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\n", ans[0].back);
    return 0;
}

 



PAT.Public bike management(SPFA + DFS)

标签:ant   city   when   current   only   unique   http   利用   using   

原文地址:https://www.cnblogs.com/bianjunting/p/12546178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!