結果
問題 |
No.527 ナップサック容量問題
|
ユーザー |
![]() |
提出日時 | 2019-04-06 10:57:54 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 32 ms / 2,000 ms |
コード長 | 1,324 bytes |
コンパイル時間 | 1,047 ms |
コンパイル使用メモリ | 102,672 KB |
実行使用メモリ | 42,896 KB |
最終ジャッジ日時 | 2024-06-23 16:58:19 |
合計ジャッジ時間 | 2,487 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <list> #include <set> #include <map> #include <numeric> #include <regex> #include <tuple> #include<iomanip> using namespace std; typedef long long ll; typedef pair<int, int> P; #define MOD 1000000007 // 10^9 + 7 #define INF 1000000000 // 10^9 #define LLINF 1LL<<60 int v[101], w[101]; int dp[101][100001]; // dp[i][j] : i個目までの商品で価値jを作るときの最小の重さ int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; for (int i = 1; i <= N; i++) cin >> v[i] >> w[i]; int V; cin >> V; for (int i = 0; i <= N; i++) { for (int j = 0; j <= 100000;j++) dp[i][j] = INF; } dp[0][0] = 0; for (int i = 0; i < N; i++) { for (int j = 0; j <= 100000; j++) { if (j + v[i + 1] <= 100000) dp[i + 1][j + v[i + 1]] = min(dp[i + 1][j + v[i + 1]], dp[i][j] + w[i + 1]); dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); } } int ansmin = dp[N][V]; // dp[N][V+1]~dp[N][100000]までの最小値を求める int ans = INF; for (int j = V + 1; j <= 100000; j++) { ans = min(ans, dp[N][j]); } if (ansmin == 0) cout << 1 << endl; else cout << ansmin << endl; if (ans < INF) cout << ans - 1 << endl; else cout << "inf" << endl; return 0; }