結果

問題 No.519 アイドルユニット
ユーザー uafr_csuafr_cs
提出日時 2017-11-08 15:21:33
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,600 bytes
コンパイル時間 2,337 ms
コンパイル使用メモリ 76,712 KB
実行使用メモリ 68,584 KB
最終ジャッジ日時 2023-08-15 21:12:59
合計ジャッジ時間 6,780 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static class Edge implements Comparable<Edge> {
		int from, to, cost;
		
		public Edge(int from, int to, int cost){
			this.from = from;
			this.to = to;
			this.cost = cost;
		}

		@Override
		public int compareTo(Edge o) {
			return Integer.compare(this.cost, o.cost);
		}
	}
	
	public static int answer = Integer.MAX_VALUE;
	
	public static void rec(int[] match, int prev, int matched, int n, ArrayList<ArrayList<Edge>> adj, int opt){
		if(opt >= answer){ return; }
		//System.out.println(answer);
		
		int from = prev + 1;
		while(from < n && match[from] >= 0){ from++; }
		//System.out.println(from);
		
		for(final Edge edge : adj.get(from)){
			final int to = edge.to;
			if(match[to] >= 0){ continue; }
			
			final int next_opt = opt + edge.cost;
			final int next_matched = matched + 2;
			
			match[to] = from; match[from] = 	to;
			
			if(next_matched < n){ rec(match, from, next_matched, n, adj, next_opt); }
			else{ answer = Math.min(answer, next_opt); }
			
			match[to] = match[from] = -1;
		}
	}
	
	public static int solve_dfs(int n, int[][] adj){		
		ArrayList<ArrayList<Edge>> sorted = new ArrayList<ArrayList<Edge>>();
		
		for(int i = 0; i < n; i++){
			ArrayList<Edge> inner = new ArrayList<Edge>();
			
			for(int j = i + 1; j < n; j++){ inner.add(new Edge(i, j, adj[i][j])); }
			
			Collections.sort(inner);
			sorted.add(inner);
		}
		
		int[] match = new int[n];
		Arrays.fill(match, -1);
		
		rec(match, -1, 0, n, sorted, 0);
		return answer;
	}
	
	public static int solve_DP(int n, int[][] adj){
		int[] DP = new int[1 << n];
		Arrays.fill(DP, Integer.MAX_VALUE / 2 - 1);
		DP[0] = 0;
		
		for(int bit = 0; bit < (1 << n); bit++){
			//System.out.println(bit);
			int i = 0;
			for (; (bit & (1 << i)) != 0; ++i);
			
			final int fst = 1 << i;
			for(int j = i + 1; j < n; j++){
				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) {
		Scanner sc = new Scanner(System.in);
		
		final int n = sc.nextInt();
		
		final int max = 1000;
		int[][] adj = new int[n][n];
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				adj[i][j] = max - sc.nextInt();
			}
		}
		
		//final int solve = solve_DP(n, adj);
		final int solve = solve_dfs(n, adj);
		
		System.out.println(max * (n / 2) - solve);
	}
}
0