hdu1114 Piggy-Bank ——完全背包

August 15, 2013
Hdu DP

link:http://acm.hdu.edu.cn/showproblem.php?pid=1114 只不过求得是最小值。没什么可说的,连我都会做……o(╯□╰)o


/*
ID: zypz4571
LANG: C++
TASK: pig.cpp
 */

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
using namespace std;
int f[11111],c[555],w[555], V, n;
void completepack(int c,int w) {
    for(int v=c;v<=V;++v) 
            f[v]=min(f[v],f[v-c]+w);
}
int main ( int argc, char *argv[] )
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    int t; cin>>t;
    while (t--) {
        int e, F; cin>>e>>F; V=F-e; cin>>n;
        for(int i=0;i<n;++i) cin>>c[i]>>w[i];
        memset(f,INF,sizeof(f)); f[0]=0;
        for (int i=0;i<n;++i) completepack(w[i],c[i]);
        if(f[V]==INF) cout<<"This is impossible.\n";
        else cout<<"The minimum amount of money in the piggy-bank is "<<
            f[V]<<".\n";
    }
        return EXIT_SUCCESS;
}                /* ----------  end of function main  ---------- */

o(╯□╰)o

comments powered by Disqus