結果

問題 No.90 品物の並び替え
ユーザー リチウムリチウム
提出日時 2014-12-07 19:42:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,563 ms / 5,000 ms
コード長 2,169 bytes
コンパイル時間 5,735 ms
コンパイル使用メモリ 75,140 KB
実行使用メモリ 56,624 KB
最終ジャッジ日時 2023-09-02 09:26:40
合計ジャッジ時間 7,245 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
48,952 KB
testcase_01 AC 219 ms
55,788 KB
testcase_02 AC 42 ms
48,948 KB
testcase_03 AC 91 ms
52,624 KB
testcase_04 AC 105 ms
52,828 KB
testcase_05 AC 230 ms
56,072 KB
testcase_06 AC 201 ms
55,644 KB
testcase_07 AC 52 ms
49,160 KB
testcase_08 AC 43 ms
48,856 KB
testcase_09 AC 1,563 ms
56,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.lang.Math;
public class ppp {
			
  public static void main(String[] args) {
	  MyScanner sc=new MyScanner();
	 int n=sc.nextInt();
	 int m=sc.nextInt();
	 int item1[]=new int[m];
	 int item2[]=new int[m];
	 int score[]=new int[m];
	 for(int i=0;i<m;i++){
		 item1[i]=sc.nextInt();
		 item2[i]=sc.nextInt();
		 score[i]=sc.nextInt();
	 }
	 int k=1;
	 int N=n;
	 while(N>0){
	 	k*=N;
	 	N--;
	 }
	 int b[]=new int[n];
	 int ans=0;
	 for(int i=0;i<n;i++){
		 b[i]=i;
	 }
	 for(int i=0;i<k;i++){
		 int a[]=permutation(b,i,k);
		 int s=0;
		 for(int j=0;j<m;j++){
			 int x=0;
			 int y=0;
			 for(int p=0;p<n;p++){
				 if(a[p]==item1[j])x=p;
				 if(a[p]==item2[j])y=p;
			 }
			 if(x<y)s+=score[j];
			
		 }
		 ans=Math.max(ans,s);
	 }
	 System.out.println(ans);
	 
	  
  }
static int[] permutation(int[] base, int x,int k) {
	int n = base.length;
	int ans[] = new int[n];
	if (n == 1)
		return base;
	else {
		ArrayList<Integer> B = new ArrayList<Integer>();
		for (int i = 0; i < n; i++) {
			B.add(base[i]);
		}
		k /= n;
		int N = n - 1;
		for (int i = 0; i < n - 1; i++) {
			ans[i] = B.get(x / k);
			B.remove(x / k);
			x %= k;
			k /= N;
			N--;
		}
		ans[n - 1] = B.get(0);
	}
	return ans;
}
}

class MyScanner {
	int nextInt() {
		try {
			int c = System.in.read();
			while (c != '-' && (c < '0' || '9' < c))
				c = System.in.read();
			if (c == '-')
				return -nextInt();
			int res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = System.in.read();
			} while ('0' <= c && c <= '9');
			return res;
		} catch (Exception e) {
			return -1;
		}
	}

	double nextDouble() {
		return Double.parseDouble(next());
	}

	long nextLong() {
		return Long.parseLong(next());
	}

	String next() {
		try {
			StringBuilder res = new StringBuilder("");
			int c = System.in.read();
			while (Character.isWhitespace(c))
				c = System.in.read();
			do {
				res.append((char) c);
			} while (!Character.isWhitespace(c = System.in.read()));
			return res.toString();
		} catch (Exception e) {
			return null;
		}
	}
}
0