【CCF CSP】 20171203 行车路线 Java(有问题)80分
问题描述
小明和小芳出去乡村玩,小明负责开车,小芳来导航。
小芳将可能的道路分为大道和小道。大道比较好走,每走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,当t为0时c ≤ 105,当t为1时c≤ 80。保证答案不超过106。
对于另外20%的评测用例,不存在小道;
对于另外20%的评测用例,所有的小道不相交;
对于所有评测用例,1 ≤ n ≤ 500,1 ≤ m ≤ 105,1 ≤ a, b ≤ n,t是0或1,当t为0时c ≤ 105,当t为1时c≤ 80。保证答案不超过106。
解题思路
使用邻接表存储边,基于迪杰斯特拉算法计算最短的疲劳值。使用一个长度为n的数组记录,某结点之前走的小道的长度。
在Dijstra中比较更新最短路时,如果迭代到小路时,比较路的长度为((选取结点的最短路)-选取结点之前走的小路的平方+(选取结点之前走的小路长度+本结点选择的小路长度)的平方),如果比较路小于本结点当前最短路,则替换当前最短路,本结点之前走的小路长度记为(选取结点之前走的小路+本次走的小路长度),如果大于等于,不修改,继续迭代;如果迭代到大路,比较路的长度为(选取结点的最短路+本结点选择的大路),如果比较路小于本结点当前最短路,则替换当前最短路,且本结点之前走的最短路记为0,如果大于等于,不修改,继续迭代。直至n结点确定最短路,程序结束,输入n结点的最短路数值。
(有问题。。)
代码实现
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 { private static int n; private final static int MAX = Integer.MAX_VALUE; private static boolean[] finalVex; private static int[] shortPath; private static List<LinkedList<Edge>> list; public static void shortPathDij() { Edge tmp = null; shortPath = new int[n]; int[] tails = new int[n]; int[] exp = new int[n]; finalVex = new boolean[n]; Arrays.fill(shortPath, MAX); Arrays.fill(finalVex, false); Arrays.fill(exp, 0); shortPath[0] = 0; tails[0] = 1; while(!finalVex[n-1]) { int index = min(shortPath); if(index == -1) break; LinkedList<Edge> p = list.get(index); Iterator<Edge> it = p.iterator(); int j=0; while(it.hasNext()) { tmp = it.next(); j = tmp.end; if(finalVex[j]) continue; if(tmp.type==1) { int eee = exp[index]+tmp.weight; int sum = shortPath[index]-(int)Math.pow(exp[index], 2)+(int)Math.pow(eee, 2); if(sum<shortPath[j]) { shortPath[j] = sum; tails[j] = index+1; exp[j] = eee; } } else { if((shortPath[index]+tmp.weight)<shortPath[j]) { shortPath[j] = shortPath[index]+tmp.weight; tails[j] = index+1; exp[j] = 0; } } } } } private static int min(int[] arr) { int index = -1; for(int i=0; i<n; i++) if(!finalVex[i]) index = i; if(index==-1) return -1; for(int i=0; i<arr.length; i++) if(arr[index]>arr[i]&&!finalVex[i]) index = i; finalVex[index] = true; return index; } public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); int nums = in.nextInt(); list = new ArrayList<>(n); for(int i=0; i<n; i++) { list.add(new LinkedList<Edge>()); } for(int i=0; i<nums; i++) { int type = in.nextInt(); int start = in.nextInt(); int end = in.nextInt(); int weight = in.nextInt(); list.get(start-1).add(new Edge(type, start-1, end-1, weight)); list.get(end-1).add(new Edge(type, end-1, start-1, weight)); } shortPathDij(); System.out.println(shortPath[n-1]); in.close(); } } class Edge{ public int type; public int start; public int end; public int weight; public Edge(int type, int start, int end, int weight) { this.type = type; this.start = start; this.end = end; this.weight = weight; } }

浙公网安备 33010602011771号