結果
問題 | No.357 品物の並び替え (Middle) |
ユーザー |
![]() |
提出日時 | 2019-04-06 10:45:38 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 6 ms / 5,000 ms |
コード長 | 905 bytes |
コンパイル時間 | 365 ms |
コンパイル使用メモリ | 52,112 KB |
最終ジャッジ日時 | 2025-01-07 01:25:14 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:46:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 46 | scanf("%d %d", &N, &M); | ~~~~~^~~~~~~~~~~~~~~~~ main.cpp:48:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 48 | scanf("%d %d %d", &item1[i], &item2[i], &score[i]); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int MAX_N = 14, MAX_M = MAX_N * (MAX_N - 1);int G[MAX_N][MAX_N];void add_edge(int from, int to, int cost) {G[from][to] = cost;}int N, M;int item1[MAX_M], item2[MAX_M], score[MAX_M];int dp[1 << MAX_N];void solve() {sizeof(G, 0, sizeof(G));sizeof(dp, -1, sizeof(dp));for (int i = 0; i < M; i++) {add_edge(item1[i], item2[i], score[i]);}for (int S = 0; S < 1 << N; S++) {for (int t = 0; t < N; t++) {if (!(S >> t & 1)) {int s = 0;for (int f = 0; f < N; f++) {if (S >> f & 1) {s += G[f][t];}}dp[S | 1 << t] = max(dp[S | 1 << t], dp[S] + s);}}}printf("%d\n", dp[(1 << N) - 1]);}int main() {scanf("%d %d", &N, &M);for (int i = 0; i < M; i++) {scanf("%d %d %d", &item1[i], &item2[i], &score[i]);}solve();}