Problem:
Max Score: 1150 Points

Task statement was updated.

Problem Statement

There is a grid which size is $H \times W$. the upper-left cell is $(1,1)$ and the lower-right cell is $(H,W)$.

There is $N$ arrows. Arrow which start point is $(a_i,b_i)$ of direction is $c_i$, and size is $d_i$. ($d_i$ may be negative)

It is guaranteed that there are no two arrow which start point is same.

Sothe want to move from cell $(sx,sy)$ to cell $(gx,gy)$ with arrows.

But it may not possible to move to goal in initial grid.

So, Snuke decided to change some arrows.

Sothe can change each arrow as follows:

He can't change the start point of this arrow.

It costs $e_i$ if he change the direction of this arrow.

It costs $f \times |d_i-G|$ if he change d_i to $G$.

He can't add or erase arrows.

Please calculate the minimum cost that he can move to $(gx,gy)$.

If he can't move to goal, please output '-1'.

Note: Arrows are directed, and he can't turn in the middle of the arrow.

Input

The input is given from standard input in the following format.

H W N f
sx sy gx gy
a_1 b_1 c_1 d_1 e_1
a_2 b_2 c_2 d_2 e_2
: : :
a_N b_N c_N d_N e_N

Output

Please output a single integer: The minimum cost to clear this puzzle.

If you can't achieve the objective, print -1.

Print \n (line break) in the end.

Constraints

$1 \le H,W \le 100000$

$1 \le N \le 70000$

$1 \le f,e_i \le 1000000$

$1 \le d_i \le 100000$

$1 \le a_i,sx,tx \le H$

$1 \le b_i,sy,ty \le W$

$c_i$ is N, E, S, or W, which means North, East, South, West.

Subtasks

Subtask 1 [ 190 points ]

$H=1$

$W \le 600$

Subtask 2 [ 170 points ]

$H,W \le 80$

Subtask 3 [ 360 points ]

$H,W \le 600$

Subtask 4 [ 430 points ]

There is no additional constraints.

Sample Input 1

4 4 2 2
1 1 2 2
1 1 E 1 1
1 2 E 2 2

Sample Output 1

4

Sample Input 2

1 4 2 10
1 1 1 4
1 1 E 1 4
1 3 W 1 4

Sample Output 2

14

Sample Input 3

1 8 4 9
1 3 1 6
1 1 E 7 2
1 8 W 7 5
1 3 W 2 5
1 6 E 2 8

Sample Output 3

14

Sample Input 4

5 5 7 10
1 2 4 5
1 2 E 2 6
2 3 S 2 7
3 1 N 1 8
3 2 W 1 10
4 1 E 4 12
5 5 N 3 13
5 1 E 2 14

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)


import java.io.*;
import java.math.*;
import java.util.*;

import static java.util.Arrays.*;

public class Main {
	private static final int mod = (int)1e9+7;

	final Random random = new Random(0);
	final IOFast io = new IOFast();

	/// MAIN CODE
	public void run() throws IOException {
//		int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim());
		int TEST_CASE = 1;
		while(TEST_CASE-- != 0) {
			int h = io.nextInt();
			int w = io.nextInt();
			int n = io.nextInt();
			f = io.nextInt();
			
			int sy = io.nextInt();
			int sx = io.nextInt();
			int gy = io.nextInt();
			int gx = io.nextInt();
			
			HashMap<Long, Arrow> mp = new HashMap<>();
			for (int i = 0; i < n; i++) {
				int y = io.nextInt();
				int x = io.nextInt();
				char dir = io.nextChar();
				int d = io.nextInt();
				int e = io.nextInt();
				Arrow a = new Arrow(i, x, y, "ENWS".indexOf(dir), d, e);
				mp.put((long)x<<32|y, a);
				setX.add(a);
				setY.add(a);
			}
			
			Arrow a = mp.get((long)sx<<32|sy);
			if (a == null) {
				io.out.println(-1);
				return;
			}
			
			long ans = Long.MAX_VALUE;
			PriorityQueue<State> q = new PriorityQueue<>();
			q.add(new State(2, a, 0));
			long[] memo = new long[n * 3];
			Arrays.fill(memo, Long.MAX_VALUE - 1);
			memo[a.idx * 3 + 2] = 0;
			while (!q.isEmpty()) {
				State s = q.poll();

//				dump(s.a.x, s.a.y, s.cost, s.cost(gx, gy), gx, gy);
				if (memo[s.a.idx * 3 + s.prevDir] < s.cost) continue;
				memo[s.a.idx * 3 + s.prevDir] = s.cost;
//				if (s.cost + s.mincost(gx, gy) >= ans) continue;
				
//				dump(s.a.x, s.a.y, s.cost, s.cost(gx, gy), gx, gy);
				ans = Math.min(ans, s.cost(gx, gy));
				
				Arrow x0 = setY.lower(s.a);
				Arrow x1 = setY.higher(s.a);
				Arrow y0 = setX.lower(s.a);
				Arrow y1 = setX.higher(s.a);
				if (x0 != null && x0.y == s.a.y) q.add(s.move(x0));
				if (x1 != null && x1.y == s.a.y) q.add(s.move(x1));
				if (y0 != null && y0.x == s.a.x) q.add(s.move(y0));
				if (y1 != null && y1.x == s.a.x) q.add(s.move(y1));
			}
			
			io.out.println(ans);
		}
	}
	
	long f;
	class State implements Comparable<State> {
		final int prevDir;
		final Arrow a;
		final long cost;
		
		public State(int pd, Arrow a, long c) {
			this.prevDir = pd;
			this.a = a;
			this.cost = c;
		}

		public State move(Arrow b) {
			long res = Long.MAX_VALUE;
			if (a.y == b.y) {
				if (prevDir != 2 && prevDir == 0) res = Math.min(res, f * Math.abs(a.x - b.x));
				for (int i = 0; i < 4; i++) {
					if (a.dir % 2 == 0) res = Math.min(res, (i==0?0:a.e) + f * Math.abs(a.dstX() - b.x));
					a.rot();
				}
				if (res != Long.MAX_VALUE) res += cost;
				return new State(0, b, res);
			} else if (a.x == b.x) {
				if (prevDir != 2 && prevDir == 1) res = Math.min(res, f * Math.abs(a.y - b.y));
				for (int i = 0; i < 4; i++) {
					if (a.dir % 2 != 0) res = Math.min(res, (i==0?0:a.e) + f * Math.abs(a.dstY() - b.y));
					a.rot();
				}
				if (res != Long.MAX_VALUE) res += cost;
				return new State(1, b, res);
			} else {
				throw new RuntimeException();
			}
		}

		public long mincost(int gx, int gy) {
			return f * (Math.abs(a.x - gx) + Math.abs(a.y - gy));
		}

		public long cost(int gx, int gy) {
			long res = Long.MAX_VALUE;
			if (a.y == gy) {
				if (prevDir != 2 && prevDir == 0) res = Math.min(res, f * Math.abs(a.x - gx));
				for (int i = 0; i < 4; i++) {
					if (a.dir % 2 == 0) {
						res = Math.min(res, (i==0?0:a.e) + f * Math.abs(a.dstX() - gx));
					}
					a.rot();
				}
			} else if (a.x == gx) {
				if (prevDir != 2 && prevDir == 1) res = Math.min(res, f * Math.abs(a.y - gy));
				for (int i = 0; i < 4; i++) {
					if (a.dir % 2 != 0) {
						res = Math.min(res, (i==0?0:a.e) + f * Math.abs(a.dstY() - gy));
//						dump(res, (i==0?0:a.e), f * Math.abs(a.dstY() - gy), a.dir);
					}
					a.rot();
				}
			} else {
				return Long.MAX_VALUE;
			}
			if (res == Long.MAX_VALUE) return res;
			return cost + res;
		}

		@Override
		public int compareTo(State o) {
			return Long.compare(cost, o.cost);
		}
	}

	TreeSet<Arrow> setX = new TreeSet<>((a,b)->{
		if (a.x != b.x) return a.x-b.x;
		return a.y-b.y;
	});
	TreeSet<Arrow> setY = new TreeSet<>((a,b)->{
		if (a.y != b.y) return a.y-b.y;
		return a.x-b.x;
	});
	
	static class Arrow {
		public final int x, y, len, e;
		public int dir;
		public final int idx;
		
		public Arrow(int idx, int x, int y, int dir, int len, int e) {
			this.idx = idx;
			this.x = x;
			this.y = y;
			this.dir = dir;
			this.len = len;
			this.e = e;
		}
		
		static int[] d = new int[]{1,0,-1,0,1,};
		public void rot() { dir = (dir + 1) & 3; }
		public int dstX() { return x + len * d[dir]; }
		public int dstY() { return y + len * d[dir+1]; }
	}
	
	/// TEMPLATE
	static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); }
	static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); }
	
	static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; }
	static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; }

	void printArrayLn(int[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	void printArrayLn(long[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	
	static void dump(Object... o) { System.err.println(Arrays.deepToString(o)); } 
	
	void main() throws IOException {
		//		IOFast.setFileIO("rle-size.in", "rle-size.out");
		try { run(); }
		catch (EndOfFileRuntimeException e) { }
		io.out.flush();
	}
	public static void main(String[] args) throws IOException { new Main().main(); }
	
	static class EndOfFileRuntimeException extends RuntimeException {
		private static final long serialVersionUID = -8565341110209207657L; }

	static
	public class IOFast {
		private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		private PrintWriter out = new PrintWriter(System.out);

		void setFileIn(String ins) throws IOException { in.close(); in = new BufferedReader(new FileReader(ins)); }
		void setFileOut(String outs) throws IOException { out.flush(); out.close(); out = new PrintWriter(new FileWriter(outs)); }
		void setFileIO(String ins, String outs) throws IOException { setFileIn(ins); setFileOut(outs); }

		private static int pos, readLen;
		private static final char[] buffer = new char[1024 * 8];
		private static char[] str = new char[500*8*2];
		private static boolean[] isDigit = new boolean[256];
		private static boolean[] isSpace = new boolean[256];
		private static boolean[] isLineSep = new boolean[256];

		static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; }
		public int read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; }
		public int nextInt() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } }
		int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); }
		public String nextString() throws IOException { return new String(next()); }
		public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); }
//		public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; }
		public double nextDouble() throws IOException { return Double.parseDouble(nextString()); }
		public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; }
		public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); } return res; }
		public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) { res[i] = nextIntArray(k); } return res; }
		public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; }
		public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] = nextDouble(); } return res; }
	}
}