結果

問題 No.119 旅行のツアーの問題
ユーザー schwarzahlschwarzahl
提出日時 2017-12-23 11:52:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,983 ms / 5,000 ms
コード長 3,201 bytes
コンパイル時間 2,509 ms
コンパイル使用メモリ 81,324 KB
実行使用メモリ 59,628 KB
最終ジャッジ日時 2023-08-23 09:26:43
合計ジャッジ時間 15,883 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3,983 ms
59,628 KB
testcase_01 AC 635 ms
59,136 KB
testcase_02 AC 132 ms
55,924 KB
testcase_03 AC 134 ms
55,672 KB
testcase_04 AC 133 ms
55,480 KB
testcase_05 AC 183 ms
59,288 KB
testcase_06 AC 327 ms
59,192 KB
testcase_07 AC 132 ms
55,744 KB
testcase_08 AC 133 ms
55,812 KB
testcase_09 AC 136 ms
55,924 KB
testcase_10 AC 134 ms
55,668 KB
testcase_11 AC 135 ms
55,428 KB
testcase_12 AC 134 ms
55,676 KB
testcase_13 AC 139 ms
56,016 KB
testcase_14 AC 148 ms
55,740 KB
testcase_15 AC 173 ms
55,000 KB
testcase_16 AC 212 ms
59,368 KB
testcase_17 AC 141 ms
55,476 KB
testcase_18 AC 170 ms
57,248 KB
testcase_19 AC 174 ms
57,244 KB
testcase_20 AC 260 ms
59,600 KB
testcase_21 AC 225 ms
58,992 KB
testcase_22 AC 240 ms
59,276 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 sum = 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]);
			sum += B[i] + C[i];
		}
		for (int i = 0; i < M; i++) {
			graph.link(N+1+D[i], 1+E[i], Integer.MAX_VALUE / 3);
		}

		System.out.println(sum - maxFlow(graph, SOURCE, TARGET));
	}

	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, int from, int to) {
		int sum = 0;
		int limitDepth = 0;
		while (isExistFlow(graph, from, to)) {
			int currentFlow = flow(graph, from, to,Integer.MAX_VALUE / 3, 0, limitDepth);
			sum += currentFlow;
			if (currentFlow == 0) {
				limitDepth++;
			}
		}
		return sum;
	}

	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