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

101 篇文章 1 订阅
17 篇文章 0 订阅

题意:

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

思路:

很复杂的一道题目。

但是分析后,很简单:

整体思路:

我们求出每种水晶的最小消耗魔力,那么问题就转换为 求在不超过魔力n 的情况下,让你选择尽可能多水晶来卖更多的钱。显然,是一个完全背包。

那么问题又进一步转换为求每种水晶的最小消耗魔力。

因为合成是能传递的。

所以我们按照类似dijkstra的方式写就行,把处理最小的魔力消耗 水晶,来更新能合成的水晶的最小魔力消耗。

分析完是不是觉的很水。=_=~

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;


int T, n, m, k, ks;


const int inf = 0x3f3f3f3f;
const int maxn = 10000 + 10;
const int maxm = 200 + 10;

struct Thing{
    int money;
    int xh;
}tg[maxn];

struct Edge{
    int goal;
    vector<pair<int,int> >sub;
}p[maxm];


int dp[maxn];

struct Node{
    int u,d;
    Node(int u = 0,int d = 0):u(u),d(d){}

    bool operator < (const Node& rhs) const {
        return d > rhs.d;
    }
};

vector<int>g[maxm];
int d[maxm];
bool vis[maxm];
priority_queue<Node>q;

int get_sum(Edge& e){
    int sum = 0;
    for (int i = 0; i < e.sub.size(); ++i){
        sum += e.sub[i].second * d[e.sub[i].first];
    }
    return sum;
}

void dij(){
    while(!q.empty()){
        Node nod = q.top(); q.pop();
        int u = nod.u, dis = nod.d;
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = 0; i < g[u].size(); ++i){
            Edge& e = p[g[u][i]];
            int tmp = get_sum(e);
            if (d[e.goal ] > tmp ){
                d[e.goal] = tmp;
                q.push(Node(e.goal, d[e.goal]));
            }
        }
    }
}





int main(){
    scanf("%d", &T);
    while(T--){
        memset(vis,0,sizeof vis);
        while(!q.empty())q.pop();
        scanf("%d %d %d",&n, &m, &k);

        for (int i = 1; i <= m; ++i){
            g[i].clear();
            Thing& t = tg[i];
            int x,y;
            scanf("%d",&x);
            if (x == 0){
                t.xh = n+1;
                d[i] = n+1;
                scanf("%d",&t.money);
            }
            else{
                scanf("%d%d",&y,&t.money);
                t.xh = y;
                d[i] = y;
            }
        }
        for (int i = 0; i < k; ++i){
            Edge& e = p[i];
            e.sub.clear();
            scanf("%d",&e.goal);
            int num;
            scanf("%d",&num);
            for (int j = 0; j < num; ++j){
                int x,y;
                scanf("%d%d",&x, &y);
                e.sub.push_back(make_pair(x,y));
                g[x].push_back(i);
            }
        }


        for (int i = 1; i <= m; ++i){
            if (d[i] <= n){
                q.push(Node(i, d[i]));
            }
        }

        dij();


        memset(dp,0,sizeof dp);
        for (int i = 1; i <= m; ++i){
            for (int j = 0; j <= n; ++j){
                if (j-d[i] >= 0) dp[j] = max(dp[j],dp[j-d[i] ] + tg[i].money);
            }
        }
        printf("Case #%d: %d\n",++ks, dp[n]);
    }


    return 0;
}

Mr. Panda and Crystal

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


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  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.

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
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值