結果

問題 No.845 最長の切符
ユーザー もりをもりを
提出日時 2019-06-28 21:50:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 104 ms / 3,000 ms
コード長 1,723 bytes
コンパイル時間 1,637 ms
コンパイル使用メモリ 165,252 KB
実行使用メモリ 167,476 KB
最終ジャッジ日時 2023-09-14 21:54:00
合計ジャッジ時間 4,455 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
167,248 KB
testcase_01 AC 46 ms
167,224 KB
testcase_02 AC 45 ms
167,192 KB
testcase_03 AC 45 ms
167,304 KB
testcase_04 AC 46 ms
167,244 KB
testcase_05 AC 46 ms
167,248 KB
testcase_06 AC 46 ms
167,288 KB
testcase_07 AC 46 ms
167,472 KB
testcase_08 AC 46 ms
167,232 KB
testcase_09 AC 47 ms
167,292 KB
testcase_10 AC 48 ms
167,188 KB
testcase_11 AC 47 ms
167,188 KB
testcase_12 AC 47 ms
167,436 KB
testcase_13 AC 47 ms
167,192 KB
testcase_14 AC 47 ms
167,188 KB
testcase_15 AC 57 ms
167,308 KB
testcase_16 AC 94 ms
167,180 KB
testcase_17 AC 56 ms
167,184 KB
testcase_18 AC 67 ms
167,172 KB
testcase_19 AC 50 ms
167,244 KB
testcase_20 AC 99 ms
167,196 KB
testcase_21 AC 104 ms
167,180 KB
testcase_22 AC 56 ms
167,292 KB
testcase_23 AC 48 ms
167,172 KB
testcase_24 AC 95 ms
167,196 KB
testcase_25 AC 48 ms
167,176 KB
testcase_26 AC 59 ms
167,176 KB
testcase_27 AC 46 ms
167,476 KB
testcase_28 AC 60 ms
167,244 KB
testcase_29 AC 46 ms
167,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int (i)=0;(i)<(n);++(i))
#define FOR(i,a,b) for(int (i)=(a);(i)<(b);++(i))
#define EACH(e,v) for(auto& e:v)
#define ALL(v) (v).begin(),(v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define PERM(v) SORT(v);for(bool c##p=1;c##p;c##p=next_permutation(ALL(v)))
#define UNIQUE(v) SORT(v);(v).erase(unique(ALL(v)),(v).end())
template<typename A,typename B> inline bool chmax(A &a,const B &b){if(a<b){a=b;return 1;}return 0;}
template<typename A,typename B> inline bool chmin(A &a,const B &b){if(a>b){a=b;return 1;}return 0;}

const int MOD = (int)1e9 + 7;
const int INF = 1 << 30;
const ll INFF = 1LL << 62;

// 下にy, 右にx
enum {R, U, L, D};
const int dx[] = {1,  0, -1, 0};
const int dy[] = {0, -1,  0, 1};

int N, M;
int graph[20][20];
ll dp[1 << 20][20];

signed main() {

    cin >> N >> M;

    REP(i, 20) REP(j, 20) graph[i][j] = -1;
    REP(i, 1 << 20) REP(j, 20) dp[i][j] = -INF;
    REP(i, 20) dp[1 << i][i] = 0;

    REP(i, M) {
        int A, B, C;
        cin >> A >> B >> C;
        --A, --B;
        chmax(graph[A][B], C);
        chmax(graph[B][A], C);
    }

    REP(mask, 1 << N) REP(nxt, N) {
        if (mask & (1 << nxt)) continue;
        int nxt_mask = mask | (1 << nxt);
        REP(cur, N) {
            if (dp[mask][cur] == -INF) continue;
            if (graph[cur][nxt] == -1) continue;
            chmax(dp[nxt_mask][nxt], dp[mask][cur] + graph[cur][nxt]);
        }
    }

    ll res = 0;
    REP(i, 1 << N) REP(j, N) chmax(res, dp[i][j]);
    cout << res << endl;

    // REP(i, 1 << N) REP(j, N) cout << (dp[i][j] == -INF ? -1 : dp[i][j]) << " \n"[j == N - 1];

}
0