Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

使用Dijkstra算法的Java代码

Posted by 47872427533 at 2023-02-24 23:23:35 on Problem 3255
此代码在[洛谷](https://www.luogu.com.cn/problem/P2865)测试通过,因为POJ使用的JDK为1.5版本,JDK1.5不支持泛型,所以请你自行解决泛型问题,否则编译不通过。
```Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

public class Main {
	static int N, R;

	static class Edge {
		int to, cost;

		Edge (int to, int cost) {
			this.to = to;
			this.cost = cost;
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		StreamTokenizer streamTokenizer = new StreamTokenizer(bufferedReader);

		streamTokenizer.nextToken();
		N = (int)streamTokenizer.nval;
		streamTokenizer.nextToken();
		R = (int)streamTokenizer.nval;
		Map<Integer, List<Edge>> G = new HashMap<Integer, List<Edge>>();
		for (int i = 1; i <= N; i++) {
			G.put(i, new ArrayList<Edge>());
		}
		for (int i = 0; i < R; i++) {
			streamTokenizer.nextToken();
			int from = (int)streamTokenizer.nval;
			streamTokenizer.nextToken();
			int to = (int)streamTokenizer.nval;
			streamTokenizer.nextToken();
			int cost = (int)streamTokenizer.nval;

			G.get(from).add(new Edge(to, cost));
			G.get(to).add(new Edge(from, cost));
		}

		bufferedReader.close();

		System.out.println(solve(G));
	}

	static int solve(Map<Integer, List<Edge>> G) {
		PriorityQueue<Main.Edge> queue = new PriorityQueue<Main.Edge>(new Comparator<Main.Edge>(){
			public int compare(Edge o1, Edge o2) {
				return o1.cost < o2.cost ? -1 : 1;
			}
		});
		int[] costs = new int[N + 1];
        int[] costs2 = new int[N + 1];

        Arrays.fill(costs, Integer.MAX_VALUE);
        Arrays.fill(costs2, Integer.MAX_VALUE);
		costs[1] = 0;
		queue.add(new Edge(1, 0));

		while (!queue.isEmpty()) {
			Edge node = queue.poll();
			int v = node.to;
			int cost = node.cost;
			if (costs2[v] < cost) {
				continue;
			}
			for (Edge edge : G.get(v)) {
				int new_cost = cost + edge.cost;
				if (new_cost < costs[edge.to]) {
					int temp = costs[edge.to];
					costs[edge.to] = new_cost;
					new_cost = temp;
					queue.add(new Edge(edge.to, costs[edge.to]));
				}
				if (new_cost < costs2[edge.to] && new_cost > costs[edge.to]) {
					costs2[edge.to] = new_cost;
					queue.add(new Edge(edge.to, costs2[edge.to]));
				}
			}
		}

		return costs2[N];
	}
}
```

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator