CSP 201712-4行车路线(Java)

这是一个关于道路规划的问题,目标是找到一条从1号路口到n号路口的路线,使得司机小明的疲劳度最小。疲劳度计算方式为:大道每公里增加1,连续走小道每公里增加平方值。题目提供了样例输入和输出,以及不同评测用例的数据规模。博主表示在解决这个问题时遇到困难,希望能得到他人的帮助。
摘要由CSDN通过智能技术生成

问题描述

  小明和小芳出去乡村玩,小明负责开车,小芳来导航。
  小芳将可能的道路分为大道和小道。大道比较好走,每走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,连接ab两个路口,长度为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

样例输出

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, bnt是0或1,c ≤ 105。保证答案不超过106。


这个题我完全做懵了,只得了50分,暂时先保存下来,等理清思路再重新做,如果大家有更好的思路请评论告诉我,我现在脑子一片混沌,真的跪了,我知道自己哪里有问题但就是不会改。。。

import java.util.*;
public class Main {
	static class Edge {
		int t;
		int a , b;
		int c;
		public Edge(int t, int a, int b, int c) {
			this.t = t;
			this.a = a;
			this.b = b;
			this.c = c;
		}
		
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), m = sc.nextInt();
		int[][][] map = new int[n+1][n+1][2];
		long[][] node = new long[n+1][2];
		long[][] cost = new long[n+1][2];
		for(int i=2;i<=n;i++) {
			node[i][1] = (long) 1e18;
		}
		for(int i=0;i<m;i++) {
			int t = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
			map[a][b][0] = t;
			map[b][a][0] = t;
			map[a][b][1] = c;
			map[b][a][1] = c;
		}
		for(int i=1;i<=n;i++) {
			for(int j=2;j<=n;j++) {
				if(map[i][j][0]==0 && map[i][j][1]!=0) {
					long tmp1 = (long) 1e18;
					if(cost[i][1]!=0) {
						tmp1 = cost[i][1] + map[i][j][1];
					}
					long tmp = node[i][1] + map[i][j][1];
					if(tmp1<tmp && tmp1<=node[j][1]) {
						node[j][0] = 0;
						node[j][1] = tmp1;
						cost[j][0] = 0;
						cost[j][1] = tmp;
						
					}
					else if(tmp<tmp1 && tmp <= node[j][1]) {
						cost[j][0] = node[j][0];
						cost[j][1] = node[j][1];
						node[j][0] = 0;
						node[j][1] = tmp;
					}
				}
				else if(map[i][j][0]==1 && map[i][j][1]!=0){
					long temp1 = cost[i][0] + map[i][j][1];
					long tmp1 = (long) 1e18;
					if(cost[i][1]!=0) {
						tmp1 = cost[i][1] - cost[i][0]*cost[i][0] + temp1*temp1;
					}
					long temp = node[i][0] + map[i][j][1];
					long tmp = node[i][1] - node[i][0]*node[i][0] + temp*temp;
					if(tmp1<tmp && tmp1<=node[j][1]) {
						node[j][0] = temp1;
						node[j][1] = tmp1;
						cost[j][0] = temp;
						cost[j][1] = tmp;
						
					}
					else if(tmp<tmp1 && tmp <= node[j][1]) {
						cost[j][0] = node[j][0];
						cost[j][1] = node[j][1];
						node[j][0] = temp;
						node[j][1] = tmp;
					}
				}
			}
		}
		System.out.println(node[n][1]);
	}

}
/*
4 5
1 1 2 2
0 1 2 10
0 1 3 5
0 3 2 4
1 2 4 3

5 5
0 1 2 30
0 1 3 10
1 2 4 9
1 3 4 10
1 4 5 1
 */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值