結果
問題 | No.357 品物の並び替え (Middle) |
ユーザー |
|
提出日時 | 2025-03-25 13:36:38 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 7 ms / 5,000 ms |
コード長 | 965 bytes |
コンパイル時間 | 2,718 ms |
コンパイル使用メモリ | 196,604 KB |
実行使用メモリ | 5,888 KB |
最終ジャッジ日時 | 2025-03-25 13:36:42 |
合計ジャッジ時間 | 3,664 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 |
ソースコード
#include <bits/stdc++.h> using namespace std; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main(){ int n,m,a,b,c; cin >> n >> m; vector<vector<int>> score(n,vector<int>(n,0)); for(int i = 0; i < m; i++){ cin >> a >> b >> c; score[a][b] = c; } vector<int> dp(1<<n,0);//選んだ品物集合mskに対して獲得できる得点の最大値 for(int msk = 0; msk < (1<<n); msk++){ for(int i = 0; i < n; i++){ if(!(msk&(1<<i))){ int s = 0; for(int j = 0; j < n; j++){ if(msk&(1<<j)){ s += score[i][j]; } } chmax(dp[msk+(1<<i)],dp[msk]+s); } } } cout << dp[(1<<n)-1] << endl; return 0; }