結果

問題 No.417 チューリップバブル
ユーザー 37zigen37zigen
提出日時 2016-08-11 03:23:43
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,777 bytes
コンパイル時間 3,932 ms
コンパイル使用メモリ 83,072 KB
実行使用メモリ 346,040 KB
最終ジャッジ日時 2024-04-25 07:20:18
合計ジャッジ時間 8,173 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 45 ms
36,776 KB
testcase_02 WA -
testcase_03 AC 48 ms
37,072 KB
testcase_04 AC 46 ms
36,696 KB
testcase_05 AC 47 ms
36,576 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class チューリップバブル {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";

	static void solver() {
		int n = ni();// 村の数
		int m = ni();// 残り時間
		long[] u = new long[n];
		for (int i = 0; i < n; i++) {
			u[i] = nl();// 税収
		}
		ArrayList<Edge>[] e = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			e[i] = new ArrayList<>();
		}
		for (int i = 0; i < n - 1; i++) {
			int a = ni();
			int b = ni();
			int c = ni();
			e[a].add(new Edge(c, a, b));
			e[b].add(new Edge(c, b, a));
		}
		ArrayList<State> list = dfs(-1, 0, e, u, m);
		list.sort(new Comparator<State>() {
			@Override
			public int compare(State s1, State s2) {
				return -Long.compare(s1.u, s2.u);
			}
		});
		System.out.println(list.get(0).u);
	}

	static ArrayList<State> dfs(int p, int cur, ArrayList<Edge>[] e, long[] u, int m) {
		ArrayList<ArrayList<State>> tmpList = new ArrayList<>();
		for (Edge v : e[cur]) {
			if (v.to != p) {
				ArrayList<State> d = dfs(cur, v.to, e, u, m-2*v.time);;
				ArrayList<State> tmpList2 = new ArrayList<>();
				for (State tmp : d) {
					tmp.t += 2 * v.time;
					tmp.u += u[cur];
					if (m >= tmp.t) {
						tmpList2.add(tmp);
					}
				}
				tmpList.add(tmpList2);
			}
		}
		ArrayList<State> ret = new ArrayList<>();

		for (int i = 0; i < tmpList.size(); i++) {
			ArrayList<State> lst = tmpList.get(i);
			int n = ret.size();
			for (State s : lst) {
				for (int j = 0; j < n; j++) {
					State tmp = new チューリップバブル ().new State(s.u + ret.get(j).u, s.t + ret.get(j).t);
					if (m >= tmp.t) {
						ret.add(tmp);
					}
				}
			}
			ret.addAll(lst);
		}

		ret.add(new チューリップバブル().new State(u[cur], 0));
		return ret;
	}

	static class Edge {
		int time;
		int from;
		int to;

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

	public class State {
		long u;
		int t;

		State(long u, int t) {
			this.u = u;
			this.t = t;
		}
	}

	public static void main(String[] args) throws Exception {
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		solver();
		out.flush();
	}

	static long nl() {
		try {
			long num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	static char nc() {
		try {
			int b = skip();
			if (b == -1)
				return 0;
			return (char) b;
		} catch (IOException e) {
		}
		return 0;
	}

	static double nd() {
		try {
			return Double.parseDouble(ns());
		} catch (Exception e) {
		}
		return 0;
	}

	static String ns() {
		try {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			if (b == -1)
				return "";
			sb.append((char) b);
			while (true) {
				b = is.read();
				if (b == -1)
					return sb.toString();
				if (b <= ' ')
					return sb.toString();
				sb.append((char) b);
			}
		} catch (IOException e) {
		}
		return "";
	}

	public static char[] ns(int n) {
		char[] buf = new char[n];
		try {
			int b = skip(), p = 0;
			if (b == -1)
				return null;
			buf[p++] = (char) b;
			while (p < n) {
				b = is.read();
				if (b == -1 || b <= ' ')
					break;
				buf[p++] = (char) b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}

	public static byte[] nse(int n) {
		byte[] buf = new byte[n];
		try {
			int b = skip();
			if (b == -1)
				return null;
			is.read(buf);
			return buf;
		} catch (IOException e) {
		}
		return null;
	}

	static int skip() throws IOException {
		int b;
		while ((b = is.read()) != -1 && !(b >= 33 && b <= 126))
			;
		return b;
	}

	static boolean eof() {
		try {
			is.mark(1000);
			int b = skip();
			is.reset();
			return b == -1;
		} catch (IOException e) {
			return true;
		}
	}

	static int ni() {
		try {
			int num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}
}
0