(Java)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.

杭州市有公共自行车服务,为来自世界各地的游客提供了极大的便利。人们可以在任何一个车站租一辆自行车,然后把它送回城市的任何其他车站。

公共自行车管理中心(PBMC)持续监控所有站点的实时容量。如果一个加油站刚好满了一半,那么它就被认为处于完美状态。如果一个加油站已满或空,PBMC将收集或发送自行车,以将该加油站的状况调整到完美状态。此外,途中的所有车站也将进行调整。

当报告有问题的站点时,PBMC将始终选择到达该站点的最短路径。如果有多条最短路径,将选择PBMC发送的自行车数量最少的路径。

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 chose

上图举例说明。桩号由顶点表示,道路与边相对应。边缘上的数字是从另一端到达一个终点站所花费的时间。写在顶点S内的数字是存储在S处的当前自行车数量。假设每个站点的最大容量为10。解决S3中的问题​, 我们有两种不同的最短路径:

PBMC->S1​->S3​. 在这种情况下,PBMC必须发送4辆自行车,因为我们可以从S1收集1辆自行车​然后乘5辆自行车到S3​, 从而使两个站都处于完美的状态。

PBMC->S2​->S3​. 这条路径需要与路径1相同的时间,但PBMC只发送了3辆自行车,因此将选择这条路径。

 

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.

每个输入文件包含一个测试用例。对于每种情况,第一行包含4个数字:Cmax​(≤100),总是一个偶数,是每个站的最大容量;N(≤500),车站总数;Sp​, 问题站点的索引(站点编号从1到N,PBMC由顶点0表示);M表示道路的数量。第二行包含N个非负数Ci​(i=1,…,N)其中每个Ci​是Si目前的自行车数量​分别。接下来是M行,每行包含3个数字:Si​,Sj​, 和Tij​其描述时间Tij​被带到各处​和Sj​. 一行中的所有数字都用空格隔开。

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.

对于每个测试用例,将结果打印在一行中。首先输出PBMC必须发送的自行车数量。然后在一个空格后,以以下格式输出路径:0−>S1​−>⋯−>Sp​. 最后,在另一个空间后,输出Sp条件后我们必须带回PBMC的自行车数量​已调整为完美。
请注意,如果这样的路径不是唯一的,请将需要最少自行车数量的路径输出给PBMC。法官的数据保证了这样的路径是唯一的。

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

 思路:这个题相对来说还是非常难的,模拟题,图论,用Java写有很多细节需要注意

到最后,没能够拿到满分,尽力了,欢迎各大网友指点更正

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;

class Main{
    static int cmax,n,sp,m ;//cmax代表每个站的最大车数,n站数,sp问题站编号,m路数
    static int []bike ;//保存初始时每个站的车数
    static int [][]dis;//图
    static int []mindis_to ;//到达i站点的最短距离
    static ArrayList<ArrayList<Integer>> arr = new ArrayList<>();//邻接表,存储每个站点的邻居
    static ArrayList<Integer> path =new ArrayList<>();//记录路径
    static int final_dis = Integer.MAX_VALUE, final_send, final_take;//记录最终的答案
    static ArrayList<Integer> final_ans;//记录最终的路径
    public static void main(String[] args)throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(bf);
        in.nextToken();
        cmax = (int) in.nval;

        in.nextToken();
        n = (int) in.nval;

        in.nextToken();
        sp = (int) in.nval;

        in.nextToken();
        m = (int) in.nval;
        //因为编号是从1开始的,所以创建n + 1 个
        bike = new int[n + 1];
        mindis_to = new int[n + 1];
        dis = new int[n + 1][n + 1];
        for (int i = 0; i <= n + 1; i++) {
            //创建对象,不可省略
            arr.add(new ArrayList<>());
        }
        for (int i = 1; i <= n; i++) {
            in.nextToken();
            bike[i] = (int) in.nval;
        }
        while (m-- > 0) {
            in.nextToken();
            int i = (int) in.nval ;

            in.nextToken();
            int j = (int) in.nval;

            in.nextToken();
            int k = (int) in.nval;

            arr.get(i).add(j);
            arr.get(j).add(i);
            dis[i][j] = dis[j][i] = k;
        }
        for(int i = 0 ;i <= n;i++)
        {
            //初始化
            mindis_to[i] = Integer.MAX_VALUE;
        }
        dfs(0,0,0,0);
        System.out.print(final_send + " ");
        for(int i = 0 ; i < final_ans.size() ;i++)
        {
            if(i == 0) System.out.print(final_ans.get(i));
            else
            {
                System.out.print("->" + final_ans.get(i));
            }
        }
        System.out.print(" " + final_take);
    }
    //curstation当前到了哪个站 curdis 当前走了多远 cursend 当前需要发送多少车 , curtake当前需要带回多少车
    private static void dfs(int curstation , int curdis , int cursend , int curtake) {
        //及时止损,当前到i站点路的距离已经大于了最短距离,就没必要走下去了
        if(curdis > mindis_to[curstation]) return ;
        path.add(curstation);
        //如果到达了终点
        if(curstation == sp)
        {
            if(curdis < final_dis || (curdis == final_dis && cursend < final_send ) ||(curdis == final_dis && cursend < final_send && curtake < final_take ))
            {
                final_ans = new ArrayList<>(path);
                final_dis = curdis;
                final_send = cursend;
                final_take = curtake;
            }
        }
        else
        {
            //更新达到i站点的最短距离
            if(curdis < mindis_to[curstation])
            {
                mindis_to[curstation] = curdis;
            }
            //遍历每个站点的邻居站点
            for(Integer i : arr.get(curstation))
            {
                //如果所携带的车数还不能够让i站点达到完美状态
                if(curtake + bike[i] < cmax / 2)
                {
                    dfs(i,curdis + dis[curstation][i],cursend + (cmax / 2 - curtake - bike[i]),0);
                }
                //如果能够让其达到完美状态
                else
                {
                    dfs(i,curdis + dis[curstation][i],cursend,curtake + bike[i] - cmax /2);
                }
            }
        }
        //回溯
        path.remove(path.size() - 1);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值