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

HDU6007-Mr. Panda and Crystal

程序员文章站 2022-05-22 11:37:45
...

Mr. Panda and Crystal

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


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
 

题意:有m魔力,有n种魔法水晶,还有k种合成水晶的方式,若一种水晶不能直接造出来,则告诉你卖它能获得的钱,若能直接造出来,告诉你它的,造它需要花费的魔法和卖他能获得的钱。k中合成水晶的方式是告诉你能合成的水晶和需要哪些水晶,每种水晶需要几个。问m的魔力最多能获得多少钱

解题思路:求出每种水晶的最小消耗魔力,那么问题就转换为求在不超过魔力m的情况下,选择尽可能多水晶来卖更多的钱。显然,是一个完全背包。求每种水晶的最小消耗的魔力可以用最短路的方法


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <vector>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int n, m, k;
int p[209];
int dp[10009],dis[209],vis[205];
vector<int>gg[205];

struct node
{
	int id, dis;
	friend bool operator < (node a,node b)
	{
		return a.dis > b.dis;
	}
}pre,nt1;
priority_queue<node>q;

struct Edge
{
	int id,x;
	pair<int,int>a[205];
}g[205];

int get_sum(int x)
{
	int sum = 0;
	for (int i = 0; i <g[x].x; i++)
		sum += dis[g[x].a[i].first]*g[x].a[i].second;
	return sum;
}

void Dijkstra()
{
	while (!q.empty())
	{
		pre = q.top();
		q.pop();
		vis[pre.id] = 1;
		int Size=gg[pre.id].size();
		for (int i = 0; i<Size; i++)
		{
			int ee=gg[pre.id][i];
			if(vis[g[ee].id]) continue;
			int tmp = get_sum(ee);
			if (dis[g[ee].id] > tmp)
			{
				dis[g[ee].id] = tmp;
				nt1.id = g[ee].id;
				nt1.dis = dis[g[ee].id];
				q.push(nt1);
			}
		}
	}
}

int main()
{
	int t,cas=0;
	scanf("%d", &t);
	while (t--)
	{
		memset(vis, 0, sizeof vis);
		scanf("%d %d %d", &m, &n, &k);
		for (int i = 1; i <= n; i++)
		{
			gg[i].clear();
			int x, y;
			scanf("%d", &x);
			if (x == 0) scanf("%d", &p[i]),dis[i]=m+1;
			else scanf("%d%d", &dis[i], &p[i]);
		}
		for (int i = 0; i < k; i++)
		{
			int kk, x;
			scanf("%d%d",&kk,&x);
			g[i].id = kk;
			g[i].x=x;
			for (int j = 0; j < x; j++)
			{
				int u,v;
				scanf("%d%d", &u, &v);
				g[i].a[j]=make_pair(u, v);
				gg[u].push_back(i);
			}
		}
		for (int i = 1; i <= n; i++)
			if (dis[i] <=m )
			{
				pre.id = i, pre.dis = dis[i];
				q.push(pre);
			}
		Dijkstra();
		memset(dp, 0, sizeof dp);
		for (int i = 1; i <= n; i++)
			for (int j = dis[i]; j <= m; j++)
				 dp[j] = max(dp[j], dp[j - dis[i]] + p[i]);
		printf("Case #%d: %d\n", ++cas, dp[m]);
	}
	return 0;
}