結果

問題 No.845 最長の切符
ユーザー mamekinmamekin
提出日時 2019-06-28 21:56:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 329 ms / 3,000 ms
コード長 1,633 bytes
コンパイル時間 1,191 ms
コンパイル使用メモリ 124,056 KB
実行使用メモリ 9,924 KB
最終ジャッジ日時 2023-09-14 21:55:53
合計ジャッジ時間 3,355 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 15 ms
4,376 KB
testcase_16 AC 329 ms
9,924 KB
testcase_17 AC 79 ms
4,516 KB
testcase_18 AC 53 ms
6,520 KB
testcase_19 AC 15 ms
4,376 KB
testcase_20 AC 79 ms
9,580 KB
testcase_21 AC 45 ms
9,696 KB
testcase_22 AC 69 ms
4,392 KB
testcase_23 AC 16 ms
4,376 KB
testcase_24 AC 273 ms
9,628 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 11 ms
9,924 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 11 ms
9,624 KB
testcase_29 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

class Edge
{
public:
    int to, cost;
    Edge(int to, int cost){
        this->to = to;
        this->cost = cost;
    }
};

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<Edge> > edges(n);
    for(int i=0; i<m; ++i){
        int a, b, c;
        cin >> a >> b >> c;
        -- a;
        -- b;
        edges[a].push_back(Edge(b, c));
        edges[b].push_back(Edge(a, c));
    }

    vector<vector<int> > dp(1<<n, vector<int>(n, -1));
    for(int i=0; i<n; ++i)
        dp[1<<i][i] = 0;
    for(int i=0; i<(1<<n); ++i){
        bitset<32> bs(i);
        for(int j=0; j<n; ++j){
            if(dp[i][j] == -1)
                continue;
            for(const Edge& e : edges[j]){
                if(bs[e.to])
                    continue;
                bitset<32> bs2 = bs;
                bs2[e.to] = true;
                dp[bs2.to_ulong()][e.to] = max(dp[bs2.to_ulong()][e.to], dp[i][j] + e.cost);
            }
        }
    }

    int ans = 0;
    for(int i=1; i<(1<<n); ++i){
        for(int j=0; j<n; ++j){
            ans = max(ans, dp[i][j]);
        }
    }
    cout << ans << endl;

    return 0;
}
0