欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hdu 6007 Mr. Panda and Crystal 最短路+完全背包

程序员文章站 2022-05-22 11:38:39
...

Mr. Panda and Crystal

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 605    Accepted Submission(s): 202


Problem Description
Long long time ago, there is a magic continent far far away.
There are N types of magic crystals that contain ancient magic powers. Each of the type of magic crystal has its own price for one piece in the market. As the most powerful magician, Mr. Panda could synthesize some types of crystals by collecting some amount of other types of crystals. He could also create some types of crystals by using some number of his magic powers.
Now, Mr Panda can create any number of crystals as he wish by using no more than M magic powers. He want to know the maximum amount of money he can make by sell all the crytals he creates and synthesizes.
 

Input
The first line of the input gives the number of test cases, T. T test cases follow.
Each test case starts with 3 positive intergers, M, N and K represent the amount of magic powers Mr. Panda had, the number of crystal types on the magic continent and the number of crystal synthesis equations.
Then N lines follows, each of them starts with one 0 or 1 which indicates whehter Mr. Panda could create this type of crystal.
If the ith line starts with 0, which means Mr. Panda couldn’t create crystal type i. Then there is one integer pi in this line which is the price for each piece of crystal type i.
If the ith line starts with 1, which means Mr. Panda could create crystal type i. Then there are two positive integers ci and pi in this line, the first is the amout of magic power cost when creates one piece of crystal type i, and the second is is the price for each piece of crystal type i.
The following K lines each start with two interger xi and yi , which means for synthesizing one piece of crystal type xi , yi rules should be satisfied. Then there are yipair of positive intergers uj and vj means for one piece of xthi type cristal, we have to collect vi piece of crystal type ui. Only when all the rules of ui and vi are satisfied, Mr. Panda could synthesize one piece xthi type cristal.
 

Output
For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the maximum amout of money Mr. Panda could make.

limits


1T100.
1M10000.
1N200.
1K200.
1xi,ujN.
foreachcrystalsynthesisequation,allujaredifferent.
1vj100.
1ci,pi10000.

 

Sample Input

2 100 3 2 0 20 1 15 10 1 2 1 1 2 2 1 3 1 2 1 3 2 100 3 2 1 3 1 1 4 1 0 10 3 1 1 3 3 1 2 2
 

Sample Output

Case #1: 330 Case #2: 121
 

Source


n件物品,每件的花费和价值给你。有些物品没有花费,只能通过其他物品合成。

现在给你所有物品的价值,某些物品的花费,初始的钱数,k个合成一件某种物品的方程,问最大价值是多少。


先用最短路求出每件物品的最少花费,再利用完全背包求得答案。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=2005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int d[maxn],dp[10005],p[maxn];
bool inque[maxn];
queue<int> q;
vector<int> v[maxn];
int m,n,k;

struct Edge {
    int to,num;
    vector<pair<int,int> > f;
};
Edge e[maxn];

void spfa() {
	int i,j;
	while (!q.empty()) {
		int now=q.front();
		q.pop();
		inque[now]=0;
		for (i=0;i<v[now].size();i++) {
			ll sum=0,k=v[now][i],to=e[k].to;
			for (j=0;j<e[k].f.size();j++)
				sum+=d[e[k].f[j].first]*e[k].f[j].second;
			if (sum<d[to]) {
				d[to]=sum;
				if (!inque[to]) q.push(to),inque[to]=1;
			}
		}
	}
}

int main() {
	int cas,cnt=0;
	scanf("%d",&cas);
	while (cas--) {
		cnt++;
		int i,j,t,x,y;
		scanf("%d%d%d",&m,&n,&k);
		mem0(inque);
		for (i=1;i<=n;i++) {
			v[i].clear();
			scanf("%d",&t);
			if (t==0) {
				scanf("%d",&p[i]);
				d[i]=m+1;
			} else {
				scanf("%d%d",&d[i],&p[i]);
				q.push(i);
				inque[i]=true;
			}
		}
		for (i=1;i<=k;i++) {
			scanf("%d%d",&e[i].to,&e[i].num);
			e[i].f.clear();
			for (j=1;j<=e[i].num;j++) {
				scanf("%d%d",&x,&y);
				e[i].f.push_back(make_pair(x,y));
				v[x].push_back(i);
			}
		}
		spfa();
		mem0(dp);
		for (i=1;i<=n;i++) {
			for (j=d[i];j<=m;j++) 
				dp[j]=max(dp[j],dp[j-d[i]]+p[i]);
		}
		printf("Case #%d: %d\n",cnt,dp[m]);
	}
	return 0;
}