結果

問題 No.519 アイドルユニット
ユーザー uafr_csuafr_cs
提出日時 2017-11-08 21:51:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 232 ms / 1,000 ms
コード長 2,169 bytes
コンパイル時間 2,551 ms
コンパイル使用メモリ 77,552 KB
実行使用メモリ 171,300 KB
最終ジャッジ日時 2024-05-03 07:53:22
合計ジャッジ時間 6,938 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 202 ms
171,120 KB
testcase_01 AC 199 ms
171,196 KB
testcase_02 AC 107 ms
72,444 KB
testcase_03 AC 94 ms
40,032 KB
testcase_04 AC 55 ms
37,216 KB
testcase_05 AC 56 ms
37,164 KB
testcase_06 AC 56 ms
37,360 KB
testcase_07 AC 57 ms
37,228 KB
testcase_08 AC 57 ms
37,472 KB
testcase_09 AC 57 ms
36,936 KB
testcase_10 AC 60 ms
37,740 KB
testcase_11 AC 93 ms
40,940 KB
testcase_12 AC 54 ms
37,364 KB
testcase_13 AC 93 ms
40,620 KB
testcase_14 AC 94 ms
40,344 KB
testcase_15 AC 93 ms
40,428 KB
testcase_16 AC 93 ms
40,664 KB
testcase_17 AC 94 ms
40,684 KB
testcase_18 AC 94 ms
40,440 KB
testcase_19 AC 96 ms
40,520 KB
testcase_20 AC 94 ms
40,032 KB
testcase_21 AC 93 ms
40,388 KB
testcase_22 AC 95 ms
40,028 KB
testcase_23 AC 93 ms
40,740 KB
testcase_24 AC 91 ms
46,828 KB
testcase_25 AC 95 ms
46,704 KB
testcase_26 AC 95 ms
46,708 KB
testcase_27 AC 97 ms
46,696 KB
testcase_28 AC 91 ms
46,552 KB
testcase_29 AC 199 ms
171,300 KB
testcase_30 AC 199 ms
171,024 KB
testcase_31 AC 232 ms
171,196 KB
testcase_32 AC 199 ms
171,148 KB
testcase_33 AC 55 ms
36,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package jp.monyone.verify.yukicoder.No519.MinimumGeneralMatchingDP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static final long INF = Long.MAX_VALUE / 2 - 1;

	public static long MinimumGeneralMatching(long[][] adj){
		final int n = adj.length;

		long[] DP = new long[1 << n];
		final int INF = Integer.MAX_VALUE / 2 - 1;

		for(int i = 0; i < 1 << n; i++){ DP[i] = INF; }
		DP[0] = 0;

		for(int bit = 0; bit < (1 << n); bit++){
			if(DP[bit] >= INF){ continue; }

			// 立ってない最初の bit だけ立ててて O(N) ループ節約
			int i = 0; for(; (bit & (1 << i)) != 0; i++);
			final int fst = 1 << i;

			for(int j = i + 1; j < n; j++){
				if((bit & (1 << j)) != 0){ continue; }

				final int snd = 1 << j;
				final int next_bit = bit | fst | snd;
				DP[next_bit] = Math.min(DP[next_bit], DP[bit] + adj[i][j]);
			}
		}

		return DP[(1 << n) - 1];
	}

	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			final int n = sc.nextInt();
			
			final long max = 1000;
			long[][] adj = new long[n][n];
			for(int i = 0; i < n; i++){
				for(int j = 0; j < n; j++){
					adj[i][j] = max - sc.nextInt();
				}
			}
		
			final long solve = MinimumGeneralMatching(adj);
			System.out.println(max * (n / 2) - solve);
		}
	}
	
	public static class Scanner implements AutoCloseable {
		private BufferedReader br;
		private StringTokenizer tok;

		public Scanner(InputStream is) {
			br = new BufferedReader(new InputStreamReader(is));
		}

		private void getLine() {
			try {
				while (!hasNext()) {tok = new StringTokenizer(br.readLine());}
			} catch(IOException e){ /* ignore */ }
		}

		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}

		public String next() {
			getLine(); return tok.nextToken();
		}

		public int nextInt(){
			return Integer.parseInt(next());
		}
		// 他のnextXXXもXXX.parseXXX()メソッドを使って作れるので省略

		public void close() {
			try{ br.close(); } catch (IOException e){ /*ignore*/ }
		}
	}
	
}
0