1018. Public Bike Management (30)(java版)

来源:互联网 发布:unity3d 布料插件 编辑:程序博客网 时间:2024/06/15 10:46

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

题目分析:

每个自行车车站的最大容量为一个偶数cmax,如果一个车站里面自行车的数量恰好为cmax / 2,那么称处于完美状态。如果一个车展容量是满的或者空的,控制中心(处于结点0处)就会携带或者从路上收集一定数量的自行车前往该车站,一路上会让所有的车展沿途都达到完美。现在给出cmax,车站的数量n,问题车站sp,m条边,还有距离,求最短路径。如果最短路径有多个,求能带的最少的自行车数目的那条。如果还是有很多条不同的路,那么就找一个从车站带回的自行车数目最少的。带回的时候是不调整的。

思路:

先用Dijkstra算法记录最短路径,然后用dfs从多条最短路径中选取符合条件的路径,主要细节见注释。

代码:

import java.util.*;public class Main {    static final int inf = 9999999;    static int cmax = 0, n = 0, sp = 0, m = 0;    static int[][] map = new int[505][505];    static int[] bikenum = new int[505];    static int[] dist = new int[505];    static boolean[] book = new boolean[505];    static ArrayList[] pre = new ArrayList[505];    static int minback = inf, minsend = inf;    static ArrayList minpath = new ArrayList();    static ArrayList temppath = new ArrayList();    static public void dfs(int start) {        //System.out.println(start);        if(start == 0) {            temppath.add(start);            int send = 0, back = 0;            for(int j=temppath.size() - 1;j>=0;j--) {//从0站点开始计算需要送出或者返回的自行车数量                int id = (Integer)temppath.get(j);                if(bikenum[id] > 0) {//如果当前站点的自行车数量有剩余,说明可以发送到下几个站点                    back += bikenum[id];                }                else {//当前站点的自行车数量不足                    if(back > (0 - bikenum[id])) {//如果前面站点的自行车数量有剩余,且数量多余当前站点缺少的数量,则从前面站点中取自行车                        back += bikenum[id];                    }                    else {//前面站点的自行车数量不足以补充当前站点的亏损数量,那么就将前面站点的自行车全部送到当前站点,并且从总部调自行车                        send += (-bikenum[id] - back);                        back = 0;                    }                }            }            if(send < minsend) {                minsend = send;                minback = back;                minpath.clear();                for(int j=0;j<temppath.size();j++) {                    minpath.add(temppath.get(j));                }            }            else if(send == minsend && back < minback) {                minback = back;                minpath.clear();                for(int j=0;j<temppath.size();j++) {                    minpath.add(temppath.get(j));                }            }            temppath.remove(temppath.size() - 1);            return ;        }        temppath.add(start);        for(int i=0;i<pre[start].size();i++) {            dfs((Integer)pre[start].get(i));        }        temppath.remove(temppath.size() - 1);    }    public static void main(String[] args) {        Scanner cin = new Scanner(System.in);        int i = 0, j = 0, k = 0;        Arrays.fill(dist, inf);        Arrays.fill(book, false);        for(i=0;i<505;i++) {            for(j=0;j<505;j++)                 map[i][j] = inf;        }        cmax = cin.nextInt();        n = cin.nextInt();        sp = cin.nextInt();        m = cin.nextInt();        for(i=1;i<=n;i++) {            bikenum[i] = cin.nextInt();            bikenum[i] -= cmax / 2;//直接用bikenum数组记录车站缺少或(多出)自行车的数量,可以方便后面的计算        }        for(i=0;i<m;i++) {            int x = cin.nextInt();            int y = cin.nextInt();            int z = cin.nextInt();            map[x][y] = map[y][x] = z;        }        dist[0] = 0;        for(i=0;i<=n;i++) {            int index = -1, minvalue = inf;            for(j=0;j<=n;j++) {                if(book[j] == false && dist[j] < minvalue) {                    index = j;                    minvalue = dist[j];                }            }            if(index == -1) break;            book[index] = true;            for(k=0;k<=n;k++) {                if(book[k] == false && map[index][k] != inf) {                    if(dist[k] > dist[index] + map[index][k]) {                        dist[k] = dist[index] + map[index][k];                        pre[k] = new ArrayList();                        pre[k].clear();//如果到达k的最短路径发生改变,那么将原来记录的路径清空                        pre[k].add(index);                    }                    else if(dist[k] == dist[index] + map[index][k]) {                        pre[k].add(index);                    }                }            }        }//这里的Dijkstra算法可以将每个站点的最短路径用倒序的方法记录,这里pre是一个二维数组,pre[k]表示        //从0到达k的最短路径中,k的前一个站点的编号。        dfs(sp);        System.out.print(minsend + " ");        System.out.print(minpath.get(minpath.size() - 1));        for(i=minpath.size() - 2;i>=0;i--) {            System.out.print("->" + minpath.get(i));        }        System.out.print(" " + minback);    }}
原创粉丝点击