zoukankan      html  css  js  c++  java
  • 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 ", V[N]);
    else
    puts("-1");
    return 0;
    }

    ------------------------------------------------------------------------

    248. Integer Linear Programming

    time limit per test: 0.25 sec.
    memory limit per test: 65536 KB
    input: 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).

    Input
    The 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).

    Output
    On 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)

    Input
    Test #1 

    2 4 


    Test #2 

    7 4 
    9

    Output
    Test #1 


    Test #2 
    -1

    Note
    See picture: 

    Author:Dmitry Filippov (DEF)
    Resource:Petrozavodsk Summer Training Sessions 2004
    Date:August 25, 2004







  • 相关阅读:
    git
    sublime text
    WIX安装图文并茂简易说明
    C#利用Lambda和Expression实现数据的动态绑定
    Ubuntu/CentOS下如何将普通用户提升到root权限
    虚拟机安装CentOS6.4用“桥接:直接连接到物理网线”不能上网的原因及解决方法
    C#利用lambda表达式将函数作为参数或属性跨类传递
    直接将XML存入到SQL中(SQL2008)
    C# 类中继承接口的属性
    提高开发效率的Visual Studio 2010使用技巧
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/5046698.html
Copyright © 2011-2022 走看看