PAT (Advanced Level)1030 Travel Plan【Java】

1030 Travel Plan (30分)

A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output:

0 2 3 3 40

题目大意:

N个城市,M个公路,每个公路都有距离S和权值D;找出从出发点到目标点的最短距离,最短距离可能不唯一,权值小的优先.输出路径并且输出最小权值

思路

使用邻接表存储每个边
dijkstra 算法找到每个节点距离出发点的最短距离,并且记录下每个节点的前置节点,最后使用深度搜索,从目标点逐步往回找到出发点输出路径.

AC代码

package TravelPlan;

import java.awt.geom.Ellipse2D;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;

public class Main {
	static int INF = 0x3f3f3f3f;
	static boolean[] vis;
	static List<Edge>[] graph;// 边权值
	static int[] dis;// 到源点距离
	static int[] fa;// 边的父级关系
	static int[] cost;//到源点的权值
	static int M;// 边数
	static int N;// 城市数
	static int start, target;

	static class Edge {
		int s;
		int t;
		int dis;
		int cost;

		public Edge(int s, int t, int dis, int cost) {
			super();
			this.s = s;
			this.t = t;
			this.dis = dis;
			this.cost = cost;
		}
	}

	public static void dijkstra() {
		for (Edge x : graph[start]) {
			dis[x.t] = x.dis;// 到源点距离
			cost[x.t] = x.cost;// 到源点花费
			fa[x.t] = start;
		}
		fa[start] = start;
		dis[start] = 0;
		vis[start] = true;
		while (true) {
			int k = -1;
			int minn = INF;
			for (int i = 0; i < N; i++) {//找到距当前节点的子节点最近的点
				if (dis[i] < minn && !vis[i]) {
					minn = dis[i];
					k = i;
				}
			}
			if (k == -1)
				break;
			vis[k] = true;
			for (Edge t : graph[k]) {// 遍历K的所有子节点
				if (!vis[t.t]) {
					//如果经k周转到源点距离比子节点单独到源点距离短 就更新
					if (dis[t.t] > dis[k] + t.dis) {
						dis[t.t] = dis[k] + t.dis;
						fa[t.t] = k;//将子节点的前置节点设为k
						cost[t.t] = cost[k] + t.cost;
					}
					if (dis[t.t] == dis[k] + t.dis) {
					//如果距离相等 就看权值大小
						if (cost[t.t] > cost[k] + t.cost) {
							dis[t.t] = dis[k] + t.dis;
							fa[t.t] = k;
							cost[t.t] = cost[k] + t.cost;
						}
					}
				}
			}
		}
	}

	public static void dfs(int i) {
	//直到前置节点为出发点时停止搜索
		if (fa[i] == i) {
			System.out.printf("%d ", i);
			return;
		}
		dfs(fa[i]);
		System.out.printf("%d ", i);
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		N = in.nextInt();
		M = in.nextInt();
		start = in.nextInt();
		target = in.nextInt();
		dis = new int[N];
		fa = new int[N];
		cost = new int[N];
		vis = new boolean[N];
		graph = new List[N];
		for (int i = 0; i < N; i++) {
			graph[i] = new ArrayList<Edge>();
		}
		Arrays.fill(vis, false);
		Arrays.fill(dis, INF);
		Arrays.fill(cost, INF);
		
		for (int i = 1; i <= M; i++) {
			int a = in.nextInt();
			int b = in.nextInt();
			int v = in.nextInt();
			int c = in.nextInt();
			graph[a].add(new Edge(a, b, v, c));
			graph[b].add(new Edge(b, a, v, c));
		}
		dijkstra();
		dfs(target);
		System.out.printf("%d %d", dis[target], cost[target]);
		in.close();
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值