CSP 201609-4交通规划(Java)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/l870358133/article/details/98085212

这个题是我做的最迷茫的一个题了,收获很多,对dijkstra算法更多了一些了解,不得不说,学是一回事,在学数据结构的时候就学了dijkstra算法,但真正的利用是另一回事,怎么用编程语言来实现还是要多一些思考。这个题我做了很长时间了,到现在也没能拿满分,真的有点受打击,我现在还是有点懵逼,先把这个题发出来吧,代码用两组样例测试都正确,应该是有什么没有考虑全,这次希望有哪位大佬如果看出我的错误,一定要评论给我指出来,感激不尽。


问题描述

  G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。
  建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。

输入格式

  输入的第一行包含两个整数n, m,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到n编号,首都为1号。
  接下来m行,每行三个整数a, b, c,表示城市a和城市b之间有一条长度为c的双向铁路。这条铁路不会经过ab以外的城市。

输出格式

  输出一行,表示在满足条件的情况下最少要改造的铁路长度。

样例输入

4 5
1 2 4
1 3 5
2 3 2
2 4 3
3 4 2

样例输出

11

第二组样例:

样例输入:

6 9
1 2 6
1 3 3
2 3 2
2 4 5
3 4 3
3 5 4
4 5 2
4 6 3
5 6 5

样例输出:

15

评测用例规模与约定

  对于20%的评测用例,1 ≤ n ≤ 10,1 ≤ m ≤ 50;
  对于50%的评测用例,1 ≤ n ≤ 100,1 ≤ m ≤ 5000;
  对于80%的评测用例,1 ≤ n ≤ 1000,1 ≤ m ≤ 50000;
  对于100%的评测用例,1 ≤ n ≤ 10000,1 ≤ m ≤ 100000,1 ≤ a, b ≤ n,1 ≤ c ≤ 1000。输入保证每个城市都可以通过铁路达到首都。

import java.util.*;
public class Main {
	/*
	 * 节点类,存的为某一城市到其他城市及距离
	 * */
	static class Node {
		private int targetCity;		//目标城市即其连接的城市
		private int distance;		//到目标城市的距离
		public Node(int targetCity, int distance) {
			this.targetCity = targetCity;
			this.distance = distance;
		}
		
		public int getTargetCity() {
			return targetCity;
		}
		
		public int getDistance() {
			return distance;
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();		//城市数
		int m = sc.nextInt();		//铁路数
		int Sum = 0;				//最小总和,即最少要改造多长的铁路,实际为下面cost[]的总和
		ArrayList<Node> nodes[] = new ArrayList[n+1];		//用于存放各城市都连接哪几个城市
		int[] citysDis = new int[n+1];						//首都距其他城市的最小距离
		int[] cost = new int[n+1];							//存放最近一次连接的铁路的长度
		nodes[1] = new ArrayList<Main.Node>();				//首都城市初始化
		for(int i=2;i<=n;i++) {
			citysDis[i] = 0x3f3f3f;							//首都距其他城市距离初始化,初始为无穷大
			nodes[i] = new ArrayList<Main.Node>();			//其他城市初始化
		}
		for(int i=0;i<m;i++) {
			int city1 = sc.nextInt();						//第一个城市
			int city2 = sc.nextInt();						//第二个城市
			int distance = sc.nextInt();					//两城市间铁路距离
			nodes[city1].add(new Node(city2, distance));	//对于城市1来说为城市1到城市2距离distance
			nodes[city2].add(new Node(city1, distance));	//对于城市2来说为城市2到城市1距离distance
		}
		for(int city=1;city<=n;city++) {					//从首都开始逐个城市遍历其连接的城市
			for(int j=0,size=nodes[city].size();j<size;j++) {
				Node tmp = nodes[city].get(j);
				int targetCity = tmp.getTargetCity();
				int distance = tmp.getDistance();
				int tmpDis = citysDis[city]+distance;		//首都到city的距离 + city到targetCity的距离
				if(tmpDis < citysDis[targetCity]) {			//如果小于,需要替换
					citysDis[targetCity] = tmpDis;				//更新首都到targetCity的最小距离
					cost[targetCity] = distance;				//保存最近一次替换的铁路长度
				}
				else if(tmpDis == citysDis[targetCity]) {		//当出现等长的情况
					//对比最近一次替换的铁路长度和当前铁路长度长短,如果成立,更新最近一次替换
					cost[targetCity] = Math.min(cost[targetCity], distance);
				}
			}
		}
		for(int i=2;i<=n;i++)
			Sum += cost[i];
		System.out.println(Sum);		//输出最小长度
	}
}

思路应该是没有什么问题,就是dijkstra算法,然后保留到各个城市最近的铁路长度,其和即为最小长度,我是这样认为的,然后看了看别人做的这个题好像也是这样,希望各位可以帮我找一下问题吧,拜托了。

猜你喜欢

转载自blog.csdn.net/l870358133/article/details/98085212
CSP
今日推荐