結果

問題 No.119 旅行のツアーの問題
ユーザー schwarzahlschwarzahl
提出日時 2017-12-23 11:44:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,927 ms / 5,000 ms
コード長 5,238 bytes
コンパイル時間 2,242 ms
コンパイル使用メモリ 76,268 KB
実行使用メモリ 59,600 KB
最終ジャッジ日時 2023-08-23 09:26:20
合計ジャッジ時間 15,702 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3,927 ms
59,600 KB
testcase_01 AC 622 ms
59,040 KB
testcase_02 AC 129 ms
55,760 KB
testcase_03 AC 126 ms
55,956 KB
testcase_04 AC 127 ms
55,892 KB
testcase_05 AC 181 ms
57,168 KB
testcase_06 AC 303 ms
58,864 KB
testcase_07 AC 127 ms
55,668 KB
testcase_08 AC 128 ms
55,792 KB
testcase_09 AC 129 ms
56,112 KB
testcase_10 AC 131 ms
55,544 KB
testcase_11 AC 131 ms
55,276 KB
testcase_12 AC 131 ms
55,468 KB
testcase_13 AC 132 ms
55,840 KB
testcase_14 AC 142 ms
55,596 KB
testcase_15 AC 171 ms
56,932 KB
testcase_16 AC 201 ms
59,388 KB
testcase_17 AC 135 ms
55,756 KB
testcase_18 AC 172 ms
56,972 KB
testcase_19 AC 166 ms
56,760 KB
testcase_20 AC 253 ms
58,764 KB
testcase_21 AC 229 ms
58,780 KB
testcase_22 AC 232 ms
59,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Optional;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Main main = new Main();
		main.solveB();
	}

	private void solveB() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] B = new int[N];
		int[] C = new int[N];
		for (int i = 0; i < N; i++) {
			B[i] = sc.nextInt();
			C[i] = sc.nextInt();
		}
		int M = sc.nextInt();
		int[] D = new int[M];
		int[] E = new int[M];
		for (int i = 0; i < M; i++) {
			D[i] = sc.nextInt();
			E[i] = sc.nextInt();
		}

		final int VERTEX_NUM = 2 * N + 2;
		final int SOURCE = 0;
		final int TARGET = VERTEX_NUM - 1;
		int ans = 0;
		Graph graph = new ArrayGraph(VERTEX_NUM);
		for (int i = 0; i < N; i++) {
			graph.link(SOURCE, i+1, B[i]);
			graph.link(i+1, N+i+1, B[i] + C[i]);
			graph.link(N+i+1, TARGET, C[i]);
			ans += B[i] + C[i];
		}
		for (int i = 0; i < M; i++) {
			graph.link(N+1+D[i], 1+E[i], Integer.MAX_VALUE / 3);
		}

		int limitDepth = 0;
		while (isExistFlow(graph, SOURCE, TARGET)) {
			int currentFlow = flow(graph, SOURCE, TARGET, Integer.MAX_VALUE / 3, 0, limitDepth);
			ans -= currentFlow;
			if (currentFlow == 0) {
				limitDepth++;
			}
		}
		System.out.println(ans);
	}

	/*
	private void solveA() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] B = new int[N];
		int[] C = new int[N];
		for (int i = 0; i < N; i++) {
			B[i] = sc.nextInt();
			C[i] = sc.nextInt();
		}
		int M = sc.nextInt();
		int[] D = new int[M];
		int[] E = new int[M];
		for (int i = 0; i < M; i++) {
			D[i] = sc.nextInt();
			E[i] = sc.nextInt();
		}
		int ans = 0;

		// 0=s 2*N+1=t
		Integer[][] costGraph = new Integer[2*N+2][];
		for (int i = 0; i < 2*N+2; i++) {
			costGraph[i] = new Integer[2*N+2];
		}
		for (int i = 0; i < N; i++) {
			costGraph[0][i+1] = B[i];
			costGraph[i+1][N+i+1] = B[i] + C[i];
			costGraph[N+i+1][2*N+1] = C[i];
			ans += B[i] + C[i];
		}
		for (int i = 0; i < M; i++) {
			costGraph[N+1+D[i]][1+E[i]] = Integer.MAX_VALUE / 3;
		}
		int current;
		int limitDepth = 3;
		int[] ansArray = new int[13];
		ansArray[0] = ansArray[1] = ansArray[2] = ans;
		do {
			ansArray[limitDepth] = ans;
			//current = flow(costGraph, 0, Integer.MAX_VALUE / 3, 2 * N + 2, 0, limitDepth, 0);
			ans -= current;
			if (current == 0) {
				limitDepth++;
			}
			System.err.println(limitDepth + ":" + ans);
		} while (limitDepth <= 12 && (ansArray[0] == ans || (ansArray[limitDepth - 3] != ans)));
		// 12 > log(1^10) / log(N * 2)

		System.out.println(ans);
	}
	*/

	/*
	private int flow(Integer[][] graph, int current_id, int current_flow, int max_id, int depth, int limitDepth, int prev) {
		if (current_id == max_id - 1) {
			return current_flow;
		}
		if (depth >= limitDepth) {
			return 0;
		}
		for (int id = max_id - 1; id >= 0; id--) {
			if (graph[current_id][id] != null && graph[current_id][id] > 0 && prev != id) {
				int nextFlow = current_flow < graph[current_id][id] ? current_flow : graph[current_id][id];
				int tmpFlow = flow(graph, id, nextFlow, max_id, depth+1, limitDepth, current_id);
				if (tmpFlow > 0) {
					graph[current_id][id] -= tmpFlow;
					if (graph[id][current_id] == null) {
						graph[id][current_id] = 0;
					}
					graph[id][current_id] += tmpFlow;
					return tmpFlow;
				}
			}
		}
		return 0;
	}
	*/

	interface Graph {
		void link(int from, int to, int cost);
		Optional<Integer> getCost(int from, int to);
		int getVertexNum();
	}

	class ArrayGraph implements Graph {
		private Integer[][] costArray;
		private int vertexNum;

		public ArrayGraph(int n) {
			costArray = new Integer[n][];
			for (int i = 0; i < n; i++) {
				costArray[i] = new Integer[n];
			}
			vertexNum = n;
		}

		@Override
		public void link(int from, int to, int cost) {
			costArray[from][to] = new Integer(cost);
		}

		@Override
		public Optional<Integer> getCost(int from, int to) {
			return Optional.ofNullable(costArray[from][to]);
		}

		@Override
		public int getVertexNum() {
			return vertexNum;
		}
	}

	private int maxFlow(Graph graph) {
		return 0;
	}

	private int flow(Graph graph, int from, int to, int current_flow, int depth, int limitDepth) {
		if (from == to) {
			return current_flow;
		}
		if (depth >= limitDepth) {
			return 0;
		}
		for (int id = 0; id < graph.getVertexNum(); id++) {
			Optional<Integer> cost = graph.getCost(from, id);
			if (cost.orElse(0) > 0) {
				int nextFlow = current_flow < cost.get() ? current_flow : cost.get();
				int returnFlow = flow(graph, id, to, nextFlow, depth+1, limitDepth);
				if (returnFlow > 0) {
					graph.link(from, id, cost.get() - returnFlow);
					graph.link(id, from, graph.getCost(id, from).orElse(0) + returnFlow);
					return returnFlow;
				}
			}
		}
		return 0;
	}

	private boolean isExistFlow(Graph graph, int from, int to) {
		boolean[] passed = new boolean[graph.getVertexNum()];
		return search(graph, from, to, passed);
	}

	private boolean search(Graph graph, int from, int to, boolean[] passed) {
		if (from == to) {
			return true;
		}
		passed[from] = true;
		for (int id = 0; id < graph.getVertexNum(); id++) {
			if (!passed[id] && graph.getCost(from, id).orElse(0) > 0 && search(graph, id, to, passed)) {
				return true;
			}
		}
		return false;
	}
}
0