結果
| 問題 | No.845 最長の切符 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-06-28 21:50:54 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 153 ms / 3,000 ms | 
| コード長 | 1,723 bytes | 
| コンパイル時間 | 1,504 ms | 
| コンパイル使用メモリ | 167,340 KB | 
| 実行使用メモリ | 167,424 KB | 
| 最終ジャッジ日時 | 2024-07-02 04:39:22 | 
| 合計ジャッジ時間 | 5,533 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 | 
ソースコード
#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];
}
            
            
            
        