题意:你生活在一个魔法大陆上,你有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\n", kase, dp[m]); }
return 0;
}

  

HDU 6007 Mr. Panda and Crystal (背包+spfa)的更多相关文章

  1. hdu6007 Mr. Panda and Crystal 最短路+完全背包

    /** 题目:hdu6007 Mr. Panda and Crystal 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6007 题意:魔法师有m能量,有n ...

  2. Mr. Panda and Crystal HDU - 6007 最短路+完全背包

    题目:题目链接 思路:不难看出,合成每个宝石需要消耗一定的魔力值,每个宝石有一定的收益,所以只要我们知道每个宝石合成的最小花费,该题就可以转化为一个背包容量为初始魔力值的完全背包问题,每个宝石的最小花 ...

  3. Mr. Panda and Crystal(最短路+完全背包)

    http://codeforces.com/gym/101206/attachments 题意: T组输入,每组给出m,n,k,m为能量总数,n为水晶种类数,k为合成方案数.有的水晶可以用能量制造,有 ...

  4. HDU 5234 Happy birthday --- 三维01背包

    HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...

  5. hdu 1874 畅通工程续(模板题 spfa floyd)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 spfa 模板 #include<iostream> #include<stdio ...

  6. HDU 1284 钱币兑换问题(全然背包:入门题)

    HDU 1284 钱币兑换问题(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1284 题意: 在一个国家仅有1分,2分.3分硬币,将钱N ( ...

  7. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  8. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  9. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

随机推荐

  1. Linux学习笔记 - Shell 输出命令

    1. echo 命令 echo 是基本的shell输出命令,她的语法是: echo string 我们也可以使用她来定制一些输出的格式,具体如下: 输出普通字符串 echo "it is a ...

  2. 浏览器发起Get,Post请求时候传递的参数编码问题

    浏览器发起Get,Post请求时候传递的参数编码问题 最近开发一个网站的时候,用了很多ajax方法,在页面发起Get,post请求,中间自然捎带有很多参数,有中文,有英文,英文一般是不存在编码问题的, ...

  3. Git版本控制:Github的使用之 多人协作及参与项目

    版权声明:本文为博主皮皮http://blog.csdn.net/pipisorry原创文章,未经博主允许不得转载.   目录(?)[-] Git多人协作 从远程库克隆 使用GitHub参与开源项目- ...

  4. js控制手机震动

    js控制手机震动 <button onclick="vibrate()">震动</button> <script> function vibra ...

  5. find命令中的print0和xargs -0

    看到命令find . -name checkout-cache -f -- 不明白其中-print0和 xargs -0的用法.查了一下,转载一篇备忘. xargs命令的作用是将参数列表转换成小块分段 ...

  6. 第八篇 实例化Flask的参数 及 对app的配置

    Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? 它能给我们带来怎么样的方便呢? 首先展示一下: from ...

  7. HTTP断点续传下载的原理

    frombegintoend原文HTTP断点续传下载的原理 要实现断点续传下载文件,首先要了解断点续传的原理.断点续传其实就是在上一次下载断开的位置开始继续下载,HTTP协议中,可以在请求报文头中加入 ...

  8. Halcon学习(三)赋值与数组操作

    assign : 对数据赋值,对数组的初始化.但不能对数组中的某一个值进行赋值. 举例:Tuple1 := [1,0,3,4,5,6,7,8,9]    // 对数组进行初始化 Val := sin( ...

  9. 「小程序JAVA实战」swagger2的使用与接口测试(34)

    转自:https://idig8.com/2018/08/31/xiaochengxujavashizhanswagger2deshiyongyujiekouceshi34/ 我们已经开发完了一个用户 ...

  10. sortingOrder,sortingLayer

    sortingOrder 是sortingLayer内的分级 sortingLayer是抽象的分层,用于决定2D物体绘制的先后顺序. 2D物体分两类:sprite和UI. sprite虽是2D,却可以 ...