結果

問題 No.357 品物の並び替え (Middle)
ユーザー ぴろずぴろず
提出日時 2016-04-15 16:25:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,113 ms / 5,000 ms
コード長 1,681 bytes
コンパイル時間 2,412 ms
コンパイル使用メモリ 78,872 KB
実行使用メモリ 55,252 KB
最終ジャッジ日時 2024-04-15 01:46:35
合計ジャッジ時間 10,344 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
54,356 KB
testcase_01 AC 125 ms
54,128 KB
testcase_02 AC 133 ms
54,236 KB
testcase_03 AC 133 ms
54,256 KB
testcase_04 AC 145 ms
54,076 KB
testcase_05 AC 138 ms
54,268 KB
testcase_06 AC 136 ms
54,160 KB
testcase_07 AC 128 ms
54,368 KB
testcase_08 AC 171 ms
54,100 KB
testcase_09 AC 742 ms
55,064 KB
testcase_10 AC 232 ms
54,868 KB
testcase_11 AC 248 ms
54,960 KB
testcase_12 AC 2,113 ms
55,088 KB
testcase_13 AC 771 ms
55,148 KB
testcase_14 AC 764 ms
54,908 KB
testcase_15 AC 233 ms
55,032 KB
testcase_16 AC 276 ms
55,124 KB
testcase_17 AC 268 ms
55,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no357;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();

		int[][] score = new int[n][n];
		for(int i=0;i<m;i++) {
			int it1 = sc.nextInt();
			int it2 = sc.nextInt();
			score[it1][it2] = sc.nextInt();
		}

		int n1 = n / 2;
		int max = 0;
		for(int i=0;i<1<<n;i++) {
			if (Integer.bitCount(i) != n1) {
				continue;
			}
			int sum = 0;
			for(int j=0;j<n;j++) {
				if ((i >> j & 1) == 1) continue;
				for(int k=0;k<n;k++) {
					if ((i >> k & 1) == 0) continue;
					sum += score[j][k];
				}
			}
			int max1 = max(n,i,0,score);
			int max2 = max(n,i,1,score);
			max = Math.max(max, sum + max1 + max2);
		}
		System.out.println(max);
	}

	static int max(int n,int mask,int use,int[][] score) {
		ArrayList<Integer> al = new ArrayList<>();
		for(int i=0;i<n;i++) {
			if ((mask >> i & 1) == use) {
				al.add(i);
			}
		}
		int n1 = al.size();
		int[] p = new int[n1];
		for(int i=0;i<n1;i++) {
			p[i] = al.get(i);
		}
		int max = 0;
		do {
			int sum = 0;
			for(int i=0;i<n1;i++) {
				for(int j=i+1;j<n1;j++) {
					sum += score[p[i]][p[j]];
				}
			}
			max = Math.max(sum,max);
		} while(nextPermutation(p));
		return max;
	}
	static boolean nextPermutation(int[] p) {
		for(int a=p.length-2;a>=0;--a) {
			if(p[a]<p[a+1]) {
				for(int b=p.length-1;;--b) {
					if(p[b]>p[a]) {
						int t = p[a];
						p[a] = p[b];
						p[b] = t;
						for(++a, b=p.length-1;a<b;++a,--b) {
							t = p[a];
							p[a] = p[b];
							p[b] = t;
						}
						return true;
					}
				}
			}
		}
		return false;
	}
}
0