PAT甲级1001-Public Bike Management (30)

来源:互联网 发布:淘宝首页资源位 编辑:程序博客网 时间:2024/05/01 11:54

题意:

题目背景是杭州公共自行车管理问题。包括一个管理中心,N个自行车站点,当站点没有自行车或者放满时,需要从管理中心出发去往问题站点调整到最佳状态Cmax/2,沿途经过的顶点也需要调整到最佳状态,要求:

1、去往问题站点的的路径要最短

2、如果有多天最短路径,那么拿回管理中心的车或者从管理中心拿过去的车越少越好(这点题目没有全,需要自己推断)

解决方法:

图使用邻接矩阵储存

涉及到最短路径问题,可以使用dijkstra,但是本题可直接使用基于dfs的回溯解决,但是要主要比较量的设定:

a、最短路径

b、最少送出去的车

c、最少拿回来的车

还要注意设置变量记录记录送出去的车和送回来的车,回溯的时候要用。

具体的java代码如下:

import java.util.*;public class Main {    static ArrayList<Integer> minPath = new ArrayList<>();    static ArrayList<Integer> curPath = new ArrayList<>();    static int curTime = 0;    static int minTime = Integer.MAX_VALUE;    static int curSend = 0;    static int curBikes = 0;    static int N;    static int minSend = Integer.MAX_VALUE;    static int minReturn = Integer.MAX_VALUE;    static boolean[] marked;    static int[][] roads;    static int[] C;    static int C_max;    static int balance;      public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        C_max = sc.nextInt();        balance = C_max/2;        N = sc.nextInt(); //the total number of stations        marked = new boolean[N+1];        int S_p = sc.nextInt(); //the index of the problem station        int M = sc.nextInt(); //the number of roads        C = new int[N + 1]; // current number of bikes at Si        C[0] = -1;        for (int i = 1; i <= N; i++) {            C[i] = sc.nextInt();        }          roads = new int[N + 1][N + 1]; //邻接矩阵        for (int i = 0; i < M; i++) {            int a = sc.nextInt();            int b = sc.nextInt();            int t = sc.nextInt();            roads[a][b] = t;            roads[b][a] = t;        }        sc.close();          dfs(0, S_p);        System.out.print(minSend + " 0");        for(Integer i : minPath)            System.out.print("->"+i);        System.out.print(" " + minReturn);    }      private static void dfs(int b, int des) {        if (curTime > minTime)            return;        if (b == des) {            boolean change = false;            if (curTime < minTime)                change = true;            else if(curTime == minTime){                if (curSend < minSend)                    change = true;                else if (curSend == minSend) {                    if (curBikes < minReturn)                        change = true;                }            }            if(change){                minTime = curTime;                minSend = curSend;                minReturn = curBikes;                minPath = new ArrayList<>(curPath);            }                 }        for(int t=1;t<=N;t++){            if(!marked[t]&&roads[b][t]>0){                marked[t] =true;                curPath.add(t);                curTime += roads[b][t];                int lastSend = curSend;                int lastBikes = curBikes;                if(curBikes+C[t]< balance){                    curSend += balance - curBikes-C[t];                    curBikes = 0;                }                else {                    curBikes = curBikes + C[t] - balance;                }                dfs(t,des);                curPath.remove(curPath.size()-1);                curTime -= roads[b][t];                marked[t] = false;                curSend = lastSend;                curBikes = lastBikes;              }        }    }}


0 0
原创粉丝点击