Home AtCoder. ABC 237 E Skiing
Post
Cancel

AtCoder. ABC 237 E Skiing

[Link] https://AtCoder.jp/contests/abc237/tasks/abc237_e
Consider the following apth: Space 0 => Space x Path: upper, lower distance: U, D => happiness: D - 2 * U => To maximize happiness : minimize U => PriorityQue: sort with upper distance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static StringBuilder sb = new StringBuilder();
	static int[] h;
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		int[] arr = getArr();
		int n = arr[0], m = arr[1];
		ArrayList<ArrayList<Integer>> al = new ArrayList<>();
		for(int i = 0; i < n; i++) al.add(new ArrayList<Integer>());

		h = getArr();
		for(int i = 0; i < m; i++) {
			arr = getArr();
			int a = arr[0] - 1, b = arr[1] - 1;
			al.get(a).add(b);
			al.get(b).add(a);
		}

		PriorityQueue<Edge> pq = new PriorityQueue<>((l, r) -> Long.compare(l.elevate, r.elevate));
		long max = 0;
		boolean[] visit = new boolean[n];
		long[] dijk = new long[n];
		Arrays.fill(dijk, Long.MIN_VALUE);
		dijk[0] = 0;
		pq.add(new Edge(0, 0, 0));

		while(!pq.isEmpty()) {
			Edge edge = pq.poll();
			if(visit[edge.to]) continue;
			visit[edge.to] = true;

			for(int e: al.get(edge.to)) {
				if(visit[e]) continue;

				if(h[e] >= h[edge.to]) {
					if(dijk[e] > dijk[edge.to] + getVal(edge.to, e)) continue;
					dijk[e] = dijk[edge.to] + getVal(edge.to, e);
					pq.add(new Edge(edge.to, e, edge.elevate + h[e] - h[edge.to]));
					if(dijk[e] > max) max = dijk[edge.to];
				} else {
					if(dijk[e] > dijk[edge.to] + getVal(edge.to, e)) continue;
					dijk[e] = dijk[edge.to] + getVal(edge.to, e);
					pq.add(new Edge(e, e, edge.elevate));
					if(dijk[e] > max) max = dijk[e];
				}
			}
		}

		print(max);
	}

	static int getVal(int from, int to) {
		if(h[from] == h[to]) return 0;
		if(h[from] > h[to]) return h[from] - h[to];
		return 2 * (h[from] - h[to]);
	}

	static class Edge{
		int from;
		int to;
		long elevate;

		Edge(int from, int to, long elevate) {
			this.from = from;
			this.to =to;
			this.elevate = elevate;
		}
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.