結果

問題 No.845 最長の切符
ユーザー hitonanodehitonanode
提出日時 2019-06-29 01:37:24
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 61 ms / 3,000 ms
コード長 1,004 bytes
コンパイル時間 655 ms
コンパイル使用メモリ 72,088 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2024-07-02 05:21:03
合計ジャッジ時間 1,943 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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<vector<int>> D(N, vector<int>(N, -1));
    REP(_, M)
    {
        int A, B, C;
        cin >> A >> B >> C;
        A--;
        B--;
        mmax(D[A][B], C);
        mmax(D[B][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][dd] > 0)
        {
            mmax(dp[dd][S ^ (1 << dd)], dp[d][S] + D[d][dd]);
            mmax(ret, dp[dd][S ^ (1 << dd)]);
        }
    }
    cout << ret << endl;
}
0