結果

問題 No.845 最長の切符
ユーザー chocorusk
提出日時 2019-06-28 21:57:28
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 52 ms / 3,000 ms
コード長 1,191 bytes
コンパイル時間 861 ms
コンパイル使用メモリ 98,252 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2024-07-02 04:42:10
合計ジャッジ時間 1,988 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, ll> P;

int main()
{
    int n, m;
    cin>>n>>m;
    int c[17][17]={};
    for(int i=0; i<m; i++){
        int a, b, ci; cin>>a>>b>>ci;
        a--; b--;
        c[a][b]=max(c[a][b], ci);
        c[b][a]=c[a][b];
    }
    int dp[16][1<<16];
    for(int i=0; i<n; i++) fill(dp[i], dp[i]+(1<<n), -1e9);
    for(int i=0; i<n; i++) dp[i][1<<i]=0;
    int ans=0;
    for(int i=0; i<(1<<n); i++){
        for(int j=0; j<n; j++){
            if(dp[j][i]<0) continue;
            for(int k=0; k<n; k++){
                if(i&(1<<k)) continue;
                if(c[j][k]==0) continue;
                dp[k][i^(1<<k)]=max(dp[k][i^(1<<k)], dp[j][i]+c[j][k]);
            }
            ans=max(ans, dp[j][i]);
        }
    }
    cout<<ans<<endl;
    return 0;
}
0