CCF-201712-4-行车路线-JAVA
问题描述
小明和小芳出去乡村玩,小明负责开车,小芳来导航。
小芳将可能的道路分为大道和小道。大道比较好走,每走1公里小明会增加1的疲劳度。小道不好走,如果连续走小道,小明的疲劳值会快速增加,连续走s公里小明会增加s2的疲劳度。
例如:有5个路口,1号路口到2号路口为小道,2号路口到3号路口为小道,3号路口到4号路口为大道,4号路口到5号路口为小道,相邻路口之间的距离都是2公里。如果小明从1号路口到5号路口,则总疲劳值为(2+2)2+2+22=16+2+4=22。
现在小芳拿到了地图,请帮助她规划一个开车的路线,使得按这个路线开车小明的疲劳度最小。
小芳将可能的道路分为大道和小道。大道比较好走,每走1公里小明会增加1的疲劳度。小道不好走,如果连续走小道,小明的疲劳值会快速增加,连续走s公里小明会增加s2的疲劳度。
例如:有5个路口,1号路口到2号路口为小道,2号路口到3号路口为小道,3号路口到4号路口为大道,4号路口到5号路口为小道,相邻路口之间的距离都是2公里。如果小明从1号路口到5号路口,则总疲劳值为(2+2)2+2+22=16+2+4=22。
现在小芳拿到了地图,请帮助她规划一个开车的路线,使得按这个路线开车小明的疲劳度最小。
输入格式
输入的第一行包含两个整数n, m,分别表示路口的数量和道路的数量。路口由1至n编号,小明需要开车从1号路口到n号路口。
接下来m行描述道路,每行包含四个整数t, a, b, c,表示一条类型为t,连接a与b两个路口,长度为c公里的双向道路。其中t为0表示大道,t为1表示小道。保证1号路口和n号路口是连通的。
接下来m行描述道路,每行包含四个整数t, a, b, c,表示一条类型为t,连接a与b两个路口,长度为c公里的双向道路。其中t为0表示大道,t为1表示小道。保证1号路口和n号路口是连通的。
输出格式
输出一个整数,表示最优路线下小明的疲劳度。
样例输入
6 7
1 1 2 3
1 2 3 2
0 1 3 30
0 3 4 20
0 4 5 30
1 3 5 6
1 5 6 1
1 1 2 3
1 2 3 2
0 1 3 30
0 3 4 20
0 4 5 30
1 3 5 6
1 5 6 1
样例输出
76
样例说明
从1走小道到2,再走小道到3,疲劳度为52=25;然后从3走大道经过4到达5,疲劳度为20+30=50;最后从5走小道到6,疲劳度为1。总共为76。
数据规模和约定
对于30%的评测用例,1 ≤ n ≤ 8,1 ≤ m ≤ 10;
对于另外20%的评测用例,不存在小道;
对于另外20%的评测用例,所有的小道不相交;
对于所有评测用例,1 ≤ n ≤ 500,1 ≤ m ≤ 105,1 ≤ a, b ≤ n,t是0或1,c≤ 105。保证答案不超过106。
对于另外20%的评测用例,不存在小道;
对于另外20%的评测用例,所有的小道不相交;
对于所有评测用例,1 ≤ n ≤ 500,1 ≤ m ≤ 105,1 ≤ a, b ≤ n,t是0或1,c≤ 105。保证答案不超过106。
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main { public static int n, m; public static boolean[] mFinalVex; // 判断当前节点是否使用 public static double[] mShortPath; // 存储到达当前路径的最短距离 public static List<LinkedList<EDGE>> list; // 类似于邻接矩阵 存储图 public static void main(String[] args) { Scanner input = new Scanner(System.in); n = input.nextInt(); m = input.nextInt(); list = new ArrayList<LinkedList<EDGE>>(n); for (int i = 0; i < n; i++) { list.add(new LinkedList<EDGE>()); } int type; int start; int end; double length; for (int i = 0; i < m; i++) { type = input.nextInt(); start = input.nextInt(); end = input.nextInt(); length = input.nextInt(); list.get(start - 1).add(new EDGE(type, start - 1, end - 1, length)); list.get(end - 1).add(new EDGE(type, end - 1, start - 1, length)); } mDijkstra(); System.out.println((long) mShortPath[n - 1]); input.close(); } private static void mDijkstra() { double[] trail = new double[n]; // 存储到达当前节点连续走的小路长度 mFinalVex = new boolean[n]; mShortPath = new double[n]; int index = -1; // 表示当前选择并处理某个节点 EDGE tmp; Arrays.fill(mFinalVex, false); Arrays.fill(mShortPath, Integer.MAX_VALUE); // 初始化都是无穷大 Arrays.fill(trail, 0); mShortPath[0] = 0; while (!mFinalVex[n - 1]) { // 当未搜索到最后一点的时候 index = min(mShortPath); if (index == -1) { break; } // 加入之后开始遍历更新存储长度的数组 LinkedList<EDGE> edges = list.get(index); Iterator<EDGE> it = edges.iterator(); while (it.hasNext()) { tmp = it.next(); int j = tmp.end; if (mFinalVex[j]) { // 如果已经被用过了 continue; } if (tmp.type == 1) { double eee = trail[index] + tmp.length; double sum = mShortPath[index] - (int) Math.pow(trail[index], 2) + (int) Math.pow(eee, 2); if (sum < mShortPath[j]) { mShortPath[j] = sum; trail[j] = eee; } } else { double sum = mShortPath[index] + tmp.length; if (sum < mShortPath[j]) { mShortPath[j] = sum; trail[j] = 0; } } } } } private static int min(double[] arr) { int index = -1; for (int i = 0; i < n; i++) { if (!mFinalVex[i]) { index = i; break; } } if (index == -1) { return index; } for (int i = 0; i < n; i++) { if (!mFinalVex[i] && arr[index] > arr[i]) { index = i; } } mFinalVex[index] = true; // 表明将当前节点加入到已用节点里面 return index; } } class EDGE { public int type; public int start; public int end; public double length; public EDGE(int type, int start, int end, double length) { super(); this.type = type; this.start = start; this.end = end; this.length = length; } }