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
- PAT甲级1001-Public Bike Management (30)
- 【PAT甲级】1018. Public Bike Management (30)
- PAT甲级1018. Public Bike Management (30)
- 1018. Public Bike Management (30) PAT 甲级
- PAT 甲级 1018. Public Bike Management (30)
- PAT 甲级 1018. Public Bike Management (30)
- PAT甲级练习题A1018. Public Bike Management (30)
- PAT甲级练习1018. Public Bike Management (30)
- PAT 甲级 1018. Public Bike Management
- PAT 1001 Public Bike Management
- 1018. Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS)
- 【PAT 1001Public Bike Management (30)】+ dfs + dijkstra
- 1018. Public Bike Management (30)-PAT
- PAT A 1018.Public Bike Management (30)
- pat 1018. Public Bike Management (30)
- PAT 1018. Public Bike Management (30)
- PAT 1018. Public Bike Management (30)
- PAT 1018. Public Bike Management (30)
- python numpy教程
- 集训队专题(8)1005 小明系列故事——未知剩余系
- 分布式架构下的会话追踪实践【基于Cookie和Redis实现】
- UART 初始化及使用示例代码
- 模板匹配函数详解
- PAT甲级1001-Public Bike Management (30)
- 1053. Path of Equal Weight (30)
- Asp.net B/S模式下使用Console.Write()
- 字符串和字典的相互转化
- 共享onload事件的使用(javascriptDOM编程艺术学习笔记)
- java内存模型
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- Numpy使用3
- UISegment