結果

問題 No.119 旅行のツアーの問題
ユーザー schwarzahlschwarzahl
提出日時 2017-12-23 11:52:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 3,939 ms / 5,000 ms
コード長 3,201 bytes
コンパイル時間 2,416 ms
コンパイル使用メモリ 80,124 KB
実行使用メモリ 57,764 KB
最終ジャッジ日時 2024-06-01 07:02:20
合計ジャッジ時間 14,768 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3,939 ms
57,764 KB
testcase_01 AC 565 ms
56,968 KB
testcase_02 AC 138 ms
54,112 KB
testcase_03 AC 137 ms
54,112 KB
testcase_04 AC 138 ms
54,368 KB
testcase_05 AC 198 ms
57,536 KB
testcase_06 AC 322 ms
57,148 KB
testcase_07 AC 138 ms
54,056 KB
testcase_08 AC 135 ms
54,012 KB
testcase_09 AC 138 ms
53,924 KB
testcase_10 AC 140 ms
53,920 KB
testcase_11 AC 137 ms
53,868 KB
testcase_12 AC 138 ms
53,888 KB
testcase_13 AC 139 ms
54,076 KB
testcase_14 AC 153 ms
54,288 KB
testcase_15 AC 181 ms
54,944 KB
testcase_16 AC 216 ms
56,424 KB
testcase_17 AC 143 ms
54,032 KB
testcase_18 AC 173 ms
54,496 KB
testcase_19 AC 175 ms
54,768 KB
testcase_20 AC 262 ms
57,160 KB
testcase_21 AC 238 ms
56,648 KB
testcase_22 AC 259 ms
56,656 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