PAT 1018. Public Bike Management (30)(Dijkstra,dfs根据pre[]输出路径,双向计算)

题目

1018. Public Bike Management (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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 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

解题思路

  • 1.题目要求最短路径,最小带出去的车辆,最小带回来的车辆,先Dijkstra求最短路径,然后dfs回溯。
  • 2.还有一点,我总想着车还会回来,如果远处车比较多还可以补近处的缺陷,其实题目意思不是这样的,只能拿前面收集的车俩来补偿后面缺的,你就当做他直走过去没回来好了,所以需要俩个变量分别来保存,如果此时带回来的车为负数了,就把带回来的置0,然后把带出去的车加上它的负数即可,有点囧。。
  • 2.因为可能有多条pre路径,可以用vecter<int> pre[n],来保存多条前向路径,然后再用dfs倒着回去比较输出路径。

dfs根据pre[]输出路径测试代码(先根据输入画出路径图)

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include<iostream>
using namespace std;
//这里假设定点比较少而已
#define N 10
vector<int> pre[N];
vector<int> path;
void dfs(int where ,int to){
    path.push_back(where);
    if (where == to) {
        //输出路径
        for (int i = 0; i < path.size(); ++i) {
            cout << path[i] <<" ";
        }
        cout << endl;
        //找完了就退出??!!!
        //可以尝试一下注释一行代码的结果。(因为这里return了,直接结束了,没有把最后的那个排出来,所以要再排出来)
        path.pop_back();
        return;
    }
    //对于每个他的邻居dfs
    for (int i = 0; i < pre[where].size(); ++i) {
        dfs(pre[where][i],to);
    }
    path.pop_back();
}

int main(int argc, char *argv[])
{
    pre[0].push_back(1);
    pre[0].push_back(2);
    pre[0].push_back(3);

    pre[1].push_back(4);
    pre[1].push_back(5);

    pre[2].push_back(6);

    pre[3].push_back(7);
    pre[4].push_back(7);
    pre[5].push_back(7);
    pre[6].push_back(7);
    dfs(0,7);
    return 0;
}

AC代码(参考链接

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

const int inf = 1000000000;
const int N = 502;
int n;
bool mark[N];
int a[N][N];
int b[N];
int cost[N];
vector<int> pre[N];
vector<int> answer;
int bestin, bestout, m;

void dijkstra() {
    for (int i = 0; i < n; ++i) {
        cost[i] = inf;
        mark[i] = false;
    }
    cost[0] = 0;
    for (int j = 0; j < n; ++j) {
        int k = -1;
        for (int i = 0; i < n; ++i) {
            if ((!mark[i]) && ((k < 0) || (cost[i] < cost[k]))) {
                k = i;
            }
        }
        mark[k] = true;
        for (int i = 0; i < n; ++i) {
            if ((!mark[i]) && (a[k][i] < inf)) {
                int temp = cost[k] + a[k][i];
                if (temp < cost[i]) {
                    pre[i].resize(1);
                    pre[i][0] = k;
                    cost[i] = temp;
                }
                else if (temp == cost[i]) {
                    pre[i].push_back(k);
                }
            }
        }

    }
}

void dfs(int now, int mayin, int mayout, vector<int> &path) {
    path.push_back(now);
    if (now == 0) {
        if ((mayin < bestin) || ((mayin == bestin) && (mayout < bestout))) {
            bestin = mayin;
            bestout = mayout;
            answer = path;
        }
        //这个时候已经把0加到path里面去了,
        path.pop_back();
        return;
    }
    //如果这个车站的自行车比半数(m现在已经是半数了)多
    if (b[now] >= m) {
        //那么送出去的车,mayin代表送出去的车就相应减掉多的
        mayin -= (b[now] - m);
        //如果送出去的车为负了,就代表有收入了
        if (mayin < 0) {
            mayout -= mayin;
            mayin = 0;
        }
    }
    else {
        //否则送出去的车就+上少了的
        mayin += m - b[now];
    }
    //一直找它的邻居
    for (int i = 0; i < pre[now].size(); ++i) {
        dfs(pre[now][i], mayin, mayout, path);
    }
    //对应最前面push(now),找完这个了就把她排出来
    path.pop_back();
}




int main() {
int  p, to;
    scanf("%d%d%d%d",&m,&n,&to,&p);
    for (int i = 1; i <= n; ++i) {
        scanf("%d",b + i);
    }
    ++n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            a[i][j] = inf;
        }
    }
    for (;p;--p) {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        a[x][y] = a[y][x] = min(a[x][y],z);

    }
    m >>= 1;
    dijkstra();
    bestin = bestout = inf;
    vector<int> path;
    dfs(to, 0, 0, path);
    reverse(answer.begin(), answer.end());
    printf("%d ",bestin);
    for (int i = 0; i < answer.size(); ++i) {
        if (i) {
            printf("->");
        }
        printf("%d", answer[i]);
    }
    printf(" %d\n",bestout);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值