Mr. Panda and Crystal HDU - 6007

应该注意到假如物品的费用可以被缩小的话
那一定是>=材料中最小的物品的费用

所以可以先对每个物品求最小费用
然后再做一个完全背包

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define pb push_back
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define ans() printf("%d",ans)
#define ansn() printf("%d\n",ans)
#define anss() printf("%d ",ans)
#define lans() printf("%lld",ans)
#define lanss() printf("%lld ",ans)
#define lansn() printf("%lld\n",ans)
#define fansn() printf("%.10f\n",ans)
#define r0(i,n) for(int i=0;i<(n);++i)
#define r1(i,e) for(int i=1;i<=e;++i)
#define rn(i,e) for(int i=e;i>=1;--i)
#define rsz(i,v) for(int i=0;i<(int)v.size();++i)
#define szz(x) ((int)x.size())
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define lowbit(a) (a&(-a))
#define all(a) a.begin(),a.end()
#define pii pair
#define pli pair
#define pll pair
#define mp(aa,bb) make_pair(aa,bb)
#define lrt rt<<1
#define rrt rt<<1|1
#define X first
#define Y second
#define PI (acos(-1.0))
#define sqr(a) ((a)*(a))
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1000000000+7;
const double eps=1e-9;
const int inf=0x3f3f3f3f;
const ll infl = 10000000000000000;
const int maxn=  200+10;
const int maxm = 10000+10;
//Pretests passed
int in(int &ret)
{
    char c;
    int sgn ;
    if(c=getchar(),c==EOF)return -1;
    while(c!='-'&&(c<'0'||c>'9'))c=getchar();
    sgn = (c=='-')?-1:1;
    ret = (c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9')ret = ret*10+(c-'0');
    ret *=sgn;
    return 1;
}
int dis[maxn];
int money[maxn];
bool vis[maxn];
struct node
{
    int x;
    vectorg;
}no[maxn];
vector<int>v[maxn];
int dp[maxm];
int n,m,k;
int cal(int q)
{
    int res = 0;
    rsz(i,no[q].g)
    {
        pii p = no[q].g[i];
        int a = p.X , b = p.Y;
        res += dis[a]*b;
    }
    return res;
}
void dij()
{
    mst(vis,0);
    priority_queueq;
    r1(i,n)if(dis[i]<=m)q.push(mp(-dis[i],i));
    while(!q.empty())
    {
        pii p = q.top();
        q.pop();
        int u = p.Y;
        if(vis[u])continue;
        vis[u] = 1;
        rsz(i,v[u])
        {
            int idx = v[u][i];
            int dist = cal(idx);
            int nt = no[idx].x;
            if(dis[nt] > dist)
            {
                dis[nt] = dist;
                q.push(mp(-dist,nt));
            }
        }
    }
}
int main()
{
#ifdef LOCAL
    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
#endif // LOCAL

    int t;
    sd(t);
    r1(cas,t)
    {
        printf("Case #%d: ",cas);
//        int n,m,k;
        sddd(m,n,k);
        r1(i,n)
        {
            int op;
            sd(op);
            if(op)
            {
                int a,b;
                sdd(a,b);
                money[i] = b;
                dis[i] = a;
            }
            else
            {
                int x;
                sd(x);
                money[i] = x;
                dis[i] = m+1;
            }
            v[i].clear();
        }
        r1(i,k)
        {
            no[i].g.clear();
            int x,y;
            sdd(x,y);
            no[i].x = x;
            while(y--)
            {
                int a,b;
                sdd(a,b);
                no[i].g.pb(mp(a,b));
                v[a].pb(i);
            }
        }
        dij();
        mst(dp,0);
        for(int i=1;i<=n;++i)
        {
            for(int j = dis[i];j<=m;++j)
            {
                dp[j] = max(dp[j],dp[j - dis[i] ] + money[i]);
            }
        }
        int ans = dp[m];
        ansn();
    }

    return 0;
}

你可能感兴趣的:(思维,dp)