Submission #4081808


Source Code Expand

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		MyInput in = new MyInput(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskX solver = new TaskX();
		solver.solve(1, in, out);
		out.close();
	}

	static int INF = 1 << 30;
	static long LINF = 1L << 55;
	static int MOD = 1000000007;
	static int[] mh4 = { 0, -1, 1, 0 };
	static int[] mw4 = { -1, 0, 0, 1 };
	static int[] mh8 = { -1, -1, -1, 0, 0, 1, 1, 1 };
	static int[] mw8 = { -1, 0, 1, -1, 1, -1, 0, 1 };

	static class TaskX {

		public void solve(int testNumber, MyInput in, PrintWriter out) {

			int n = in.nextInt(), m = in.nextInt();
			List<T> g = new ArrayList<>();

			long[] cost = new long[2 * n + 1];
			int z = 2 * n;
			for (int i = 0; i < n; i++) {
				long p = in.nextLong();
				g.add(new T(i, z, 0));
				g.add(new T(z, i, p));
			}
			for (int i = 0; i < n; i++) {
				long q = in.nextLong();
				g.add(new T(i + n, z, q));
				g.add(new T(z, i + n, 0));
			}
			for (int i = 0; i < m; i++) {
				int x = in.nextInt()-1, y = in.nextInt()-1;
				long a = in.nextLong(), b = in.nextLong();
				g.add(new T(x, y + n, -a));
				g.add(new T(y + n, x, b));
			}

			Arrays.fill(cost, LINF);
			cost[z] = 0;

			int cnt = 0;
			boolean update = true;
			while (update) {
				update = false;
				for (int i = 0; i < g.size(); i++) {
					if (cost[g.get(i).s] != LINF && cost[g.get(i).s] + g.get(i).d < cost[g.get(i).t]) {
						cost[g.get(i).t] = cost[g.get(i).s] + g.get(i).d;
						update = true;
					}
				}

				if (update) {
					cnt++;
					if (cnt == 2 * n) {
						out.println("no");
						return;
					}
				}

				if (cost[z] < 0) {
					out.println("no");
					return;
				}
			}

			out.println("yes");

		}

		class T {
			int s, t;
			long d;
			public T(int s, int t, long d) {
				super();
				this.s = s;
				this.t = t;
				this.d = d;
			}
		}
	}

	static class MyInput {
		private final BufferedReader in;
		private static int pos;
		private static int 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 MyInput(InputStream is) {
			in = new BufferedReader(new InputStreamReader(is));
		}

		public int read() {
			if (pos >= readLen) {
				pos = 0;
				try {
					readLen = in.read(buffer);
				} catch (IOException e) {
					throw new RuntimeException();
				}
				if (readLen <= 0) {
					throw new MyInput.EndOfFileRuntimeException();
				}
			}
			return buffer[pos++];
		}

		public int nextInt() {
			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() {
			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() {
			while (true) {
				final int c = read();
				if (!isSpace[c]) {
					return (char) c;
				}
			}
		}

		public String nextString() {
			return new String(nextChars());
		}

		public char[] nextChars() {
			int len = 0;
			str[len++] = nextChar();
			len = reads(len, isSpace);
			return Arrays.copyOf(str, len);
		}

		public char[][] next2DChars(int h, int w) {
			char[][] s = new char[h][w];
			for (int i = 0; i < h; i++) {
				s[i] = nextChars();
			}
			return s;
		}

		int reads(int len, boolean[] accept) {
			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 (MyInput.EndOfFileRuntimeException e) {
			}
			return len;
		}

		public int[] nextIntArray(final int n) {
			final int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public int[] nextIntArray1Index(final int n) {
			final int[] res = new int[n + 1];
			for (int i = 1; i < n + 1; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public int[] nextIntArrayDec(final int n) {
			final int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt() - 1;
			}
			return res;
		}

		public long[] nextLongArray(final int n) {
			final long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong();
			}
			return res;
		}

		public long[] nextLongArray1Index(final int n) {
			final long[] res = new long[n + 1];
			for (int i = 1; i < n + 1; i++) {
				res[i] = nextLong();
			}
			return res;
		}

		public long[] nextLongArrayDec(final int n) {
			final long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong() - 1;
			}
			return res;
		}

		public double nextDouble() {
			return Double.parseDouble(nextString());
		}

		public double[] nextDoubleArray(int n) {
			double[] res = new double[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextDouble();
			}
			return res;
		}

		static class EndOfFileRuntimeException extends RuntimeException {
		}

	}
}

Submission Info

Submission Time
Task H - Asteroids2
User tutuz
Language Java8 (OpenJDK 1.8.0)
Score 200
Code Size 6315 Byte
Status AC
Exec Time 238 ms
Memory 36624 KiB

Judge Result

Set Name All
Score / Max Score 200 / 200
Status
AC × 41
Set Name Test Cases
All 00-sample-00, 00-sample-01, 10-small_yes-00, 10-small_yes-01, 10-small_yes-02, 10-small_yes-03, 10-small_yes-04, 10-small_yes-05, 10-small_yes-06, 10-small_yes-07, 10-small_yes-08, 20-small_disturb-00, 20-small_disturb-01, 20-small_disturb-02, 20-small_disturb-03, 20-small_disturb-04, 20-small_disturb-05, 20-small_disturb-06, 20-small_disturb-07, 20-small_disturb-08, 30-large_yes-00, 30-large_yes-01, 30-large_yes-02, 30-large_yes-03, 30-large_yes-04, 40-large_disturb-00, 40-large_disturb-01, 40-large_disturb-02, 40-large_disturb-03, 40-large_disturb-04, 40-large_disturb-05, 40-large_disturb-06, 40-large_disturb-07, 40-large_disturb-08, 40-large_disturb-09, 40-large_disturb-10, 40-large_disturb-11, 40-large_disturb-12, 40-large_disturb-13, 40-large_disturb-14, 40-large_disturb-15
Case Name Status Exec Time Memory
00-sample-00 AC 74 ms 20820 KiB
00-sample-01 AC 72 ms 17492 KiB
10-small_yes-00 AC 74 ms 21460 KiB
10-small_yes-01 AC 85 ms 18388 KiB
10-small_yes-02 AC 87 ms 19028 KiB
10-small_yes-03 AC 75 ms 20564 KiB
10-small_yes-04 AC 87 ms 21460 KiB
10-small_yes-05 AC 77 ms 19156 KiB
10-small_yes-06 AC 125 ms 19284 KiB
10-small_yes-07 AC 125 ms 23508 KiB
10-small_yes-08 AC 113 ms 24144 KiB
20-small_disturb-00 AC 83 ms 18900 KiB
20-small_disturb-01 AC 84 ms 17876 KiB
20-small_disturb-02 AC 82 ms 19796 KiB
20-small_disturb-03 AC 78 ms 18772 KiB
20-small_disturb-04 AC 77 ms 17492 KiB
20-small_disturb-05 AC 75 ms 21460 KiB
20-small_disturb-06 AC 120 ms 21332 KiB
20-small_disturb-07 AC 118 ms 22996 KiB
20-small_disturb-08 AC 130 ms 21204 KiB
30-large_yes-00 AC 195 ms 28860 KiB
30-large_yes-01 AC 198 ms 31060 KiB
30-large_yes-02 AC 185 ms 32444 KiB
30-large_yes-03 AC 200 ms 29908 KiB
30-large_yes-04 AC 238 ms 34528 KiB
40-large_disturb-00 AC 183 ms 30036 KiB
40-large_disturb-01 AC 184 ms 31956 KiB
40-large_disturb-02 AC 222 ms 36612 KiB
40-large_disturb-03 AC 214 ms 35336 KiB
40-large_disturb-04 AC 218 ms 36624 KiB
40-large_disturb-05 AC 183 ms 35792 KiB
40-large_disturb-06 AC 187 ms 29760 KiB
40-large_disturb-07 AC 218 ms 34856 KiB
40-large_disturb-08 AC 184 ms 31700 KiB
40-large_disturb-09 AC 215 ms 32788 KiB
40-large_disturb-10 AC 218 ms 35976 KiB
40-large_disturb-11 AC 220 ms 36108 KiB
40-large_disturb-12 AC 218 ms 35836 KiB
40-large_disturb-13 AC 223 ms 35460 KiB
40-large_disturb-14 AC 224 ms 36520 KiB
40-large_disturb-15 AC 218 ms 36092 KiB