結果

問題 No.357 品物の並び替え (Middle)
ユーザー htensaihtensai
提出日時 2019-11-21 16:38:59
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,096 bytes
コンパイル時間 2,015 ms
コンパイル使用メモリ 79,180 KB
実行使用メモリ 123,208 KB
最終ジャッジ日時 2024-04-18 11:59:07
合計ジャッジ時間 14,505 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static Point[] points;
 	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		points = new Point[m];
		for (int i = 0; i < m; i++) {
		    points[i] = new Point(sc.nextInt(), sc.nextInt(), sc.nextInt());
		}
	    System.out.println(getCount(0, new ArrayList<Integer>(), new boolean[n], 0));
	}
	
	static int getCount(int idx, ArrayList<Integer> list, boolean[] used, int score) {
	    if (idx >= points.length) {
	        return score;
	    }
	    int ans = getCount(idx + 1, list, used, score);
	    Point p = points[idx];
	    if (used[p.left]) {
	        if (used[p.right]) {
	            boolean flag = false;
	            for (int x : list) {
	                if (x == p.left) {
	                    flag = true;
	                } else if (x == p.right) {
	                    if (flag) {
	                        ans += p.value;
	                    }
	                    break;
	                }
	            }
	        } else {
	            boolean flag = false;
	            for (int i = 0; i <= list.size(); i++) {
	                if (flag) {
	                    used[p.right] = true;
	                    list.add(i, p.right);
	                    ans = Math.max(ans, getCount(idx + 1, list, used, score + p.value));
	                    used[p.right] = false;
	                    list.remove(i);
	                } else {
	                    if (list.get(i) == p.left) {
	                        flag = true;
	                    }
	                }
	            }
	        }
	    } else {
	        if (used[p.right]) {
	            boolean flag = false;
	            for (int i = list.size() - 1; i >= 0; i--) {
	                if (flag) {
	                    used[p.left] = true;
	                    list.add(i, p.left);
	                    ans = Math.max(ans, getCount(idx + 1, list, used, score + p.value));
	                    used[p.left] = false;
	                    list.remove(i);
	                } else {
	                    if (list.get(i) == p.right) {
	                        flag = true;
	                        i--;
	                    }
	                }
	            }
	        } else {
	            for (int i = 0; i <= list.size(); i++) {
                    used[p.left] = true;
                    list.add(i, p.left);
                    for (int j = i + 1; j <= list.size(); j++) {
 	                    used[p.right] = true;
	                    list.add(j, p.right);
	                    ans = Math.max(ans, getCount(idx + 1, list, used, score + p.value));
	                    used[p.right] = false;
	                    list.remove(j);
                   }
                    used[p.left] = false;
                    list.remove(i);
	            }
	        }
	    }
	    return ans;
	}
	
	static class Point {
	    int left;
	    int right;
	    int value;
	    
	    public Point(int left, int right, int value) {
	        this.left = left;
	        this.right = right;
	        this.value = value;
	    }
	}
}
0