PAT练习-牛客练兵场1001:Public BikeManagement

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.

在这里插入图片描述
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.
输入描述

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.

输出描述

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.

解题思路

        题目意思是从0点开始,找到离故障点最近的一条路,通过这条路时顺便整理沿途的节点上的自行车量,多了的带走,少了的要从0点发出补上,最后到达故障点,处理故障点的情况,不能再返回处理。所以即使最后的故障点有多余的车辆也不能挪到前面的节点进行补偿(我开始以为还可以补偿就弄错拉意思)。再路径最短的情况下选择最少需要从0点发出补偿的车辆,如果有两条路径同样长,需要发出的自行车也一样,则选择最后从故障点带走的车少的那条(保证只有一条)。然后输出这条路径和需要发出、带走的自行车数量。
        最开始看觉得可以用最短路算法来做,不过因为有多个判断标准,还要记录路径,所以就选择简单的深搜回溯,这样更加方便。从根节点0开始扩展,直至故障点或者不能再走为止,沿途记录到达当前点所需要的权重、所需送出车辆、所需带回车辆,还要记录当前的路劲,到达故障点后进行比较,然后返回上一层,继续深搜,这样便可找到答案。

Java代码
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;

public class Main{
	// max为最大存放自行车数,n表示有多少个节点,end表示故障点,m表示有多少条路,halfMax:理想车数
    int max, n, m, end, halfMax;
    // 记录每个点的自行车数
    int[] numberOfBike;
    // 记录每个点有多少条路通向其他节点
    int[][] path;
    // 两个节点之间的权重
    int[][] d;
    // 深搜时判断当前节点有没有走过
    boolean[] flag;
    // minSend表示最小所需带出车辆(满足最小路径的前提),同理minBack表示最小带回车辆,minDis表示最小路径
    int minSend = Integer.MAX_VALUE, minBack = Integer.MAX_VALUE, minDis = Integer.MAX_VALUE;
    // 存储最终的路径
    Deque<Integer> ansPath = new LinkedList<>();
    // 深搜时临时路径存储
    Deque<Integer> tmpPath = new LinkedList<>();
    
    public static void main(String[] args) {
    	// 。。。不知道牛客上怎么提交的。所以忽悠下面的写法。。。
        Main num = new Main();
        // 读取数据
        num.begin();
        // 深搜回溯寻找答案
        num.dfs(0, 0, 0, 0);
        // 输出答案
        num.end();
    }

	// 输出答案
    private void end() {
        StringBuilder sb = new StringBuilder();
        sb.append(minSend + " 0");
        ansPath.removeFirst();
        for (Integer x : ansPath) {
            sb.append("->" + x);
        }
        sb.append(" " + minBack);
        System.out.println(sb);
    }
	
	// 读取数据
    private void begin() {
        Scanner in = new Scanner(System.in);
        max = in.nextInt();
        n = in.nextInt();
        end = in.nextInt();
        m = in.nextInt();
        halfMax = max / 2;

        // 读取每个点的自行车数量
        numberOfBike = new int[n + 1];
        for (int i = 1; i <= n ; i++) {
            numberOfBike[i] = in.nextInt();
        }
        // 读取路径
        path = new int[n + 1][n + 1];
        // 每条路的花费
        d = new int[n + 1][n + 1];
        int x, y, z;
        for (int i = 0; i < m; i++) {
            x = in.nextInt();
            y = in.nextInt();
            z = in.nextInt();
            path[x][++path[x][0]] = y;
            path[y][++path[y][0]] = x;
            d[x][y] = z;
            d[y][x] = z;
        }
        // 记录深搜有没有到达这个店
        flag = new boolean[n + 1];
        flag[0] = true;
    }
	
	// 深搜回溯寻找路径,x表示当前到达的点,countSend表示当前所需要发送的车辆,countBack表示当前要带回的车辆,countDis表示当前权重
    private void dfs(int x, int countSend, int countBack, int countDis) {
    	// 标记当前点已经到达,并加入临时路径中
        flag[x] = true;
        tmpPath.addLast(x);
        // 到达故障点,进行判断,是否需要更新答案,
        if (x == end) {
            if (countDis < minDis) {
                minSend = countSend;
                minBack = countBack;
                minDis = countDis;
                // 更新路径
                ansPath = new LinkedList<>(tmpPath);
            }
            else if (countDis == minDis) {
                if (countSend < minSend) {
                    minSend = countSend;
                    minBack = countBack;
                    ansPath = new LinkedList<>(tmpPath);
                }
                else if (countSend == minSend && minBack > countBack) {
                    minBack = countBack;
                    ansPath = new LinkedList<>(tmpPath);
                }
            }
            // 返回
            return;
        }
		
		// 遍历当前点能到达的下一个点
        for (int i = 1; i <= path[x][0] ; i++) {
            int y = path[x][i];
            if (flag[y]) {
                continue;
            }
            int send = countSend, back = countBack;
            // 当前点是需要车辆还是要带走车辆
            int state = numberOfBike[y] - halfMax;
            // >0表示需要带走,在back上加上state
            if (state >= 0) {
                back += state;
            }
            // 如果 前面节点剩下的车辆还不能满足,就需要从0点发出来
            else if (back + state < 0){
                send -= back + state;
                back = 0;
            }
            // 前面节点的车可以补上,好像这里写多余拉。。算拉
            else {
                back += state;
            }
            dfs(y, send, back, countDis + d[x][y]);
            // 回溯
            flag[y] = false;
            tmpPath.removeLast();
        }
    }
}

在这里插入图片描述
Java是真的慢,在一群C++和python的提交中完全没有时间优势。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值