結果

問題 No.119 旅行のツアーの問題
ユーザー schwarzahl
提出日時 2017-12-23 00:09:02
言語 Java
(openjdk 23)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,346 bytes
コンパイル時間 3,364 ms
コンパイル使用メモリ 79,688 KB
実行使用メモリ 58,808 KB
最終ジャッジ日時 2024-12-21 11:03:36
合計ジャッジ時間 7,567 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 17 WA * 2
権限があれば一括ダウンロードができます

ソースコード

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 = 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;
		}
		int sum_flow = 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;
					sum_flow += tmpFlow;
					current_flow -= tmpFlow;
				}
			}
		}
		return sum_flow;
	}
}
0