zoukankan      html  css  js  c++  java
  • HDU 6007 Mr. Panda and Crystal (背包+spfa)

    题意:你生活在一个魔法大陆上,你有n 魔力, 这个大陆上有m 种魔法水晶,还有n 种合成水晶的方式,每种水晶价格告诉你,并且告诉你哪些水晶你能直接造出来,哪些你必须合成才能造出来,问你n魔力最多能卖多少钱的水晶?

    析:首先知道的是,如果每个所消耗的魔法水晶固定,那么这就是一个背包问题,很简单就能搞定,然而并不是,但是我们能够推出,经过一定次数的变换,这个值肯定就是固定的了而且是最小的,这个就可以用spfa进行松弛操作。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #include <list>
    #include <assert.h>
    #include <bitset>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a, b, sizeof a)
    #define sz size()
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define all 1,n,1
    #define FOR(x,n)  for(int i = (x); i < (n); ++i)
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e15;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 200 + 50;
    const LL mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c) {
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    int val[maxn], w[maxn];
    int num[maxn];
    int dp[maxn*50];
    
    vector<P> v[maxn];
    
    void solve(int u){
      int sum = 0;
      for(int i = 0; i < v[u].sz; ++i)
        sum += w[v[u][i].fi] * v[u][i].se;
      if(sum < w[num[u]])  w[num[u]] = sum;
    }
    
    int main(){
      int T;  cin >> T;
      for(int kase = 1; kase <= T; ++kase){
        int k;
        scanf("%d %d %d", &m, &n, &k);
        for(int i = 1; i <= n; ++i){
          int x;
          scanf("%d", &x);
          if(0 == x)  scanf("%d", val + i), w[i] = m + 1;
          else  scanf("%d %d", w+i, val+i);
        }
    
        for(int i = 1; i <= k; ++i){
          v[i].cl;
          scanf("%d", num+i);
          int x;
          scanf("%d", &x);
          for(int j = 0; j < x; ++j){
            int u, vv;
            scanf("%d %d", &u, &vv);
            v[i].pb(P(u, vv));
          }
        }
        for(int i = 1; i <= 5; ++i){
          for(int j = 1; j <= k; ++j)
            solve(j);
        }
    
        ms(dp, 0);
        for(int i = 1; i <= n; ++i)
          for(int j = w[i]; j <= m; ++j)
            dp[j] = max(dp[j], dp[j-w[i]] + val[i]);
        printf("Case #%d: %d
    ", kase, dp[m]);
    
      }
      return 0;
    }
    

      

  • 相关阅读:
    计划任务工具-windows
    [JavaWeb基础] 017.Struts2 和 ajax交互简介
    html5学习之路_007
    [PHP学习教程
    [PHP学习教程
    [注]还原记忆力的真面目
    理解Java对象序列化
    HashTable和HashMap的区别详解
    HDFS NameNode内存全景
    HDFS 原理、架构与特性介绍
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7445975.html
Copyright © 2011-2022 走看看