SGU 248. Integer Linear Programming( 背包dp )
生活随笔
收集整理的這篇文章主要介紹了
SGU 248. Integer Linear Programming( 背包dp )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
看了半天...發現就是個背包...然后就不打算敲了. 看了一眼forum..頓時嚇傻..其他人用了gcd啊什么的各種奇怪的東西..然后還是敲了個背包結果就AC了= =既然寫了代碼就扔上來吧...
------------------------------------------------------------------------
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 1000009;const int INF = 0X3F3F3F3F;int V[maxn];int n, N, c[3];int main() {scanf("%d", &n);for(int i = 0; i < n; i++) scanf("%d", c + i);scanf("%d", &N);memset(V, INF, sizeof V); V[0] = 0;for(int i = 0; i < n; i++)for(int v = c[i]; v <= N; v++)V[v] = min(V[v], V[v - c[i]] + 1);if(V[N] != INF)printf("%d\n", V[N]);elseputs("-1");return 0;}------------------------------------------------------------------------
?
248. Integer Linear Programming
?
time limit per test: 0.25 sec.memory limit per test: 65536 KBinput: standard
output: standard
You are to solve some problem of integer linear programming. It is posed in the following way. Let x[i] be a variable which is required to be a non-negative integer (for any i from [1..N]). The goal is to minimize the function f(x[1], x[2],..., x[N])=x[1]+x[2]+...+x[N] (objective function) satisfying the constraint c[1]*x[1]+c[2]*x[2]+...+c[N]*x[N]=V.?
The point X=(x[1], x[2],..., x[N]) that satisfies the constraint is called "feasible". All feasible points form a feasible set.?
To make things clear, let us consider the following example N=2, c[1]=2, c[2]=4, V=6. There are only two feasible points: (1, 1) and (3, 0).?
Clearly, the point (1, 1) is the optimal solution, because f(1, 1)<f(3, 0).
InputThe first line of input contains a single positive integer N (0<N<=3). The second line contains N positive integers c[i] separated by whitespaces (0<c[i]<=10^6). The last line contains positive integer V (0<V<=10^6).
OutputOn the first line of the output file print the minimal possible value of the function f, or "-1" (without quotes) if the problem has no solution.
Sample test(s)
InputTest #1?
2?
2 4?
6?
Test #2?
2?
7 4?
9
OutputTest #1?
2?
Test #2?
-1
NoteSee picture:?
[submit][forum]
| Author: | Dmitry Filippov (DEF) |
| Resource: | Petrozavodsk Summer Training Sessions 2004 |
| Date: | August 25, 2004 |
轉載于:https://www.cnblogs.com/JSZX11556/p/5046698.html
總結
以上是生活随笔為你收集整理的SGU 248. Integer Linear Programming( 背包dp )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在线预览word,excel文档
- 下一篇: 结对项目——Subway