結果
| 問題 | No.845 最長の切符 | 
| コンテスト | |
| ユーザー |  tarattata1 | 
| 提出日時 | 2019-06-29 14:18:24 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 59 ms / 3,000 ms | 
| コード長 | 1,760 bytes | 
| コンパイル時間 | 514 ms | 
| コンパイル使用メモリ | 69,972 KB | 
| 実行使用メモリ | 9,728 KB | 
| 最終ジャッジ日時 | 2024-07-02 05:32:24 | 
| 合計ジャッジ時間 | 1,747 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 27 | 
コンパイルメッセージ
main.cpp: In function ‘int main(int, char**)’:
main.cpp:29:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   29 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
main.cpp:40:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   40 |         scanf("%d%d%d", &a, &b, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~
            
            ソースコード
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;
ll dp[1<<16][16];
int main(int argc, char* argv[])
{
    int n,m;
    scanf("%d%d", &n, &m);
    int g[16][16]={0};
    int i,j;
    for(i=0; i<16; i++) {
        for(j=0; j<16; j++) {
            g[i][j]=INF;
        }
    }
    for(i=0; i<m; i++) {
        int a,b,c;
        scanf("%d%d%d", &a, &b, &c);
        g[a-1][b-1]=(g[a-1][b-1]!=INF? MAX(g[a-1][b-1],c): c);
        g[b-1][a-1]=g[a-1][b-1];
    }
    int k;
    for(i=0; i<(1<<n); i++) {
        for(k=0; k<n; k++) {
            dp[i][k] = -1;
        }
    }
    for(i=1; i<(1<<n); i++) {
        for(k=0; k<n; k++) {
            if(i & (1<<k)) {
                int prev = (i & ~(1<<k));
                if(prev==0) {
                    dp[i][k] = 0;
                }
                else {
                    int j;
                    for(j=0; j<n; j++) {
                        if(dp[prev][j]>=0 && g[j][k]!=INF) {
                            dp[i][k] = MAX(dp[i][k], dp[prev][j] + g[j][k]);
                        }
                    }
                }
            }
        }
    }
    ll ans=0;
    for(i=1; i<(1<<n); i++) {
        for(k=0; k<n; k++) {
            if(dp[i][k]>=0) {
                ans = MAX(ans, dp[i][k]);
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}
            
            
            
        