結果

問題 No.845 最長の切符
ユーザー hitonanode
提出日時 2019-06-29 01:40:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 60 ms / 3,000 ms
コード長 996 bytes
コンパイル時間 649 ms
コンパイル使用メモリ 72,716 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2024-07-02 05:21:08
合計ジャッジ時間 1,851 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }

int main()
{
    int N, M;
    cin >> N >> M;
    vector<int> D(N * N);
    REP(_, M)
    {
        int A, B, C;
        cin >> A >> B >> C;
        A--;
        B--;
        mmax(D[A * N + B], C);
        mmax(D[B * N + A], C);
    }

    vector<vector<int>> dp(N, vector<int>(1 << N, -1));
    REP(i, N) dp[i][1 << i] = 0;
    int ret = 0;
    REP(S, 1 << N) REP(d, N) if (((1 << d) & S) and dp[d][S] >= 0)
    {
        REP(dd, N) if (!((1 << dd) & S) and D[d * N + dd])
        {
            mmax(dp[dd][S ^ (1 << dd)], dp[d][S] + D[d * N + dd]);
            mmax(ret, dp[dd][S ^ (1 << dd)]);
        }
    }
    cout << ret << endl;
}
0