結果

問題 No.90 品物の並び替え
ユーザー nominomi
提出日時 2019-03-11 21:28:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 428 ms / 5,000 ms
コード長 957 bytes
コンパイル時間 1,550 ms
コンパイル使用メモリ 168,380 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 20:15:56
合計ジャッジ時間 2,712 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 31 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 35 ms
4,380 KB
testcase_06 AC 25 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 428 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define int long long
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }

int N, M;
int item1[100];
int item2[100];
int score[100];

signed main() {
	cin >> N >> M;
	for (int i = 0; i < M; i++) {
		cin >> item1[i] >> item2[i] >> score[i];
	}

	// 並び方を定義する配列
	vector<int> v(N);
	iota(v.begin(), v.end(), 0);

	int ans = 0;
	do {
		int sum = 0; // この並びの時の合計スコア

		// その並びになってるかを全て調べる
		for (int i = 0; i < M; i++) {
			int cnt = 0;
			for (int j = 0; j < N; j++) {
				if (cnt == 0 and v[j] == item1[i]) {
					cnt++;
				}
				if (cnt == 1 and v[j] == item2[i]) {
					cnt++;
				}
			}
			// その並びになってたらスコアを足す
			if (cnt == 2) {
				sum += score[i];
			}
		}

		chmax(ans, sum);
	} while (next_permutation(v.begin(), v.end()));

	cout << ans << endl;

	return 0;
}
0