hdu 6007 Mr. Panda and Crystal(最短路+完全背包)
程序员文章站
2022-05-22 11:37:51
...
Mr. Panda and Crystal
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 446 Accepted Submission(s): 143
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.
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 yi pair 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.
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 yi pair 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.
∙1≤T≤100.
∙1≤M≤10000.
∙1≤N≤200.
∙1≤K≤200.
∙1≤xi,uj≤N.
∙foreachcrystalsynthesisequation,allujaredifferent.
∙1≤vj≤100.
∙1≤ci,pi≤10000.
limits
∙1≤T≤100.
∙1≤M≤10000.
∙1≤N≤200.
∙1≤K≤200.
∙1≤xi,uj≤N.
∙foreachcrystalsynthesisequation,allujaredifferent.
∙1≤vj≤100.
∙1≤ci,pi≤10000.
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
题意:有m魔力,有n种魔法水晶,还有k种合成水晶的方式,若一种水晶不能直接造出来,则告诉你卖它能获得的钱,若能直接造出来,告诉你它的,造它需要花费的魔法和卖他能获得的钱。k中合成水晶的方式是告诉你能合成的水晶和需要哪些水晶,每种水晶需要几个。问m的魔力最多能获得多少钱
题解:求出每种水晶的最小消耗魔力,那么问题就转换为求在不超过魔力m的情况下,选择尽可能多水晶来卖更多的钱。显然,是一个完全背包。求每种水晶的最小消耗的魔力可以用最短路的方法
两个vector的含义不一样 我却用了其中一个含义去初始化 结果WA到死
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
#include <bits/stdc++.h>
using namespace std;
const int N =210;
typedef long long LL;
typedef pair<int,int>pi;
const int inf = 0x3f3f3f3f;
vector<pi>p[N],g[N];
int dist[N], val[N];
int vis[N];
priority_queue<pi,vector<pi>, greater<pi> >q;
void dij(int n,int m)
{
while(!q.empty()) q.pop();
for(int i=1; i<=n; i++)
if(dist[i]<=m) q.push(make_pair(dist[i],i));
memset(vis,0,sizeof(vis));
while(!q.empty())
{
int u=q.top().second;
q.pop();
if(vis[u]) continue;
int sum=0;
vis[u]=1;
for(int i=0; i<g[u].size(); i++)
{
int v=g[u][i].second,h=g[u][i].first;
if(vis[v]) continue;
sum=0;
for(int j=0; j<p[h].size(); j++)
{
int x=p[h][j].first,y=p[h][j].second;
sum+=dist[x]*y;
}
if(sum<dist[v])
{
dist[v]=sum;
q.push(make_pair(dist[v],v));
}
}
}
return ;
}
int dp[10000+10];
int main()
{
int t, ncase=1;
scanf("%d", &t);
while(t--)
{
int m, n, k, c;
scanf("%d %d %d", &m, &n, &k);
for(int i=0; i<=max(k,n); i++) p[i].clear(), g[i].clear();
for(int i=1; i<=n; i++)
{
scanf("%d", &c);
if(c==0) scanf("%d", &val[i]),dist[i]=m+1;
else scanf("%d %d", &dist[i],&val[i]);
}
for(int i=0; i<k; i++)
{
int x, y;
scanf("%d %d", &x, &y);
for(int j=0; j<y; j++)
{
int u, v;
scanf("%d %d", &u, &v);
p[i].push_back(make_pair(u,v));
g[u].push_back(make_pair(i,x));
}
}
dij(n,m);
memset(dp,0,sizeof(dp));
for(int i=1; i<=n; i++)
for(int j=dist[i]; j<=m; j++)
dp[j]=max(dp[j],dp[j-dist[i]]+val[i]);
printf("Case #%d: %d\n",ncase++,dp[m]);
}
return 0;
}
推荐阅读
-
Codeforces 101206 I & HDU 6007 Mr. Panda and Crystal
-
HDU 6007 - Mr. Panda and Crystal ( 最短路+完全背包 )
-
[HDU - 6007] Mr. Panda and Crystal
-
Hdu 6007 Mr. Panda and Crystal 最短路+完全背包
-
hdu6007 Mr. Panda and Crystal (最短路+完全背包)
-
hdu 6007 Mr. Panda and Crystal(最短路+完全背包)
-
HDU 6007 Mr. Panda and Crystal(dijkstra变形+完全背包)