結果

問題 No.90 品物の並び替え
ユーザー _oba23__oba23_
提出日時 2018-08-23 22:06:24
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 145 ms / 5,000 ms
コード長 1,017 bytes
コンパイル時間 631 ms
コンパイル使用メモリ 76,248 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 09:02:46
合計ジャッジ時間 1,632 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

int max_score=-1;

void proc(int i, int n, vector<int>& buf, unordered_set<int>& selected, vector<vector<int>>& map){
    if(i>=n){
        int score=0;
        for(int j=0; j<n; ++j){
            for(int k=j+1; k<n; ++k){
                score+=map[buf[j]][buf[k]];
            }
        }
        max_score=max(score, max_score);
    } else{
        for(int j=0; j<n; ++j){
            if(selected.find(j)!=selected.end()) continue;
            selected.insert(j);
            buf[i]=j;
            proc(i+1, n, buf, selected, map);
            selected.erase(j);
        }
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> map(n, vector<int>(n, 0));
    for(int i=0; i<m; ++i){
        int i1, i2, s;
        cin >> i1 >> i2 >> s;
        map[i1][i2]=s;
    }
    vector<int> buf(n);
    unordered_set<int> selected;
    proc(0, n, buf, selected, map);
    cout << max_score << endl;
    return 0;
}
0