結果

問題 No.119 旅行のツアーの問題
ユーザー schwarzahlschwarzahl
提出日時 2017-12-22 23:48:55
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,087 bytes
コンパイル時間 2,274 ms
コンパイル使用メモリ 74,144 KB
実行使用メモリ 114,040 KB
最終ジャッジ日時 2023-08-23 09:25:00
合計ジャッジ時間 13,467 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 692 ms
56,944 KB
testcase_02 AC 122 ms
55,864 KB
testcase_03 AC 125 ms
55,592 KB
testcase_04 AC 126 ms
55,440 KB
testcase_05 AC 188 ms
56,176 KB
testcase_06 AC 194 ms
56,500 KB
testcase_07 AC 121 ms
56,472 KB
testcase_08 AC 121 ms
55,844 KB
testcase_09 AC 125 ms
55,608 KB
testcase_10 AC 131 ms
55,800 KB
testcase_11 AC 124 ms
55,876 KB
testcase_12 AC 132 ms
55,768 KB
testcase_13 AC 127 ms
55,692 KB
testcase_14 AC 132 ms
55,484 KB
testcase_15 AC 149 ms
55,984 KB
testcase_16 AC 184 ms
58,548 KB
testcase_17 AC 132 ms
55,668 KB
testcase_18 AC 152 ms
56,064 KB
testcase_19 AC 147 ms
55,904 KB
testcase_20 AC 346 ms
56,596 KB
testcase_21 TLE -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

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

	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++) {
			int small = B[i] < C[i] ? B[i] : C[i];
			costGraph[0][i+1] = B[i] - small;
			costGraph[i+1][N+i+1] = B[i] + C[i] - small;
			costGraph[N+i+1][2*N+1] = C[i] - small;
			ans += B[i] + C[i] - small;
		}
		for (int i = 0; i < M; i++) {
			costGraph[N+1+D[i]][1+E[i]] = Integer.MAX_VALUE / 3;
		}
		int current;
		int limitDepth = 0;
		do {
			current = flow(costGraph, 0, Integer.MAX_VALUE / 3, 2 * N + 2, 0, limitDepth);
			ans -= current;
			if (current == 0) {
				limitDepth++;
			}
		} while (limitDepth <= 12);
		// 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) {
		if (current_id == max_id - 1) {
			return current_flow;
		}
		if (depth >= limitDepth) {
			return 0;
		}
		int sum_flow = 0;
		for (int id = max_id - 1; id >= 0; id--) {
			if (graph[current_id][id] != null && graph[current_id][id] > 0) {
				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);
				if (tmpFlow > 0) {
					graph[current_id][id] -= tmpFlow;
					if (graph[id][current_id] == null) {
						graph[id][current_id] = 0;
					}
					graph[id][current_id] += tmpFlow;
					sum_flow += tmpFlow;
					current_flow -= tmpFlow;
				}
			}
		}
		return sum_flow;
	}
}
0