結果

問題 No.90 品物の並び替え
ユーザー nomi
提出日時 2019-03-11 21:28:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 448 ms / 5,000 ms
コード長 957 bytes
コンパイル時間 1,535 ms
コンパイル使用メモリ 169,632 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-23 15:40:35
合計ジャッジ時間 2,608 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 9
権限があれば一括ダウンロードができます

ソースコード

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