Codeforces 101206 I & HDU 6007 Mr. Panda and Crystal

39 篇文章 0 订阅

题意:给定 N 种水晶和M个魔力。你可以选择用 Ci 的魔力创造一个水晶,或者根据公式 Ki 来转化一个水晶,问你能得到的最大值。

思路:首先对水晶的单价进行处理。过程类似dij,每次将最小价值的水晶出队并更新以他左右材料之一的公式生成的水晶价格。最后对得到的最小值做一次背包即可。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<bitset>
#include<vector>
#include <functional>
using namespace std;

#define sp system("pause")

typedef long long ll;
typedef pair<int, ll>pii;
typedef bitset<101> bs;

struct cmp
{
    bool operator()(const pii &x, const pii &y)
    {
        return x.second > y.second;
    }
};

const int MAXN = 250;
const int MAXM = 100000 + 10;
const int INF = 1e8;

int m, n, k;
int p[MAXN], c[MAXN];
vector<pii>K[MAXN];
vector<int>B[MAXN];
ll dist[MAXN];
priority_queue<pii, vector<pii>, cmp>pq;
ll dp[MAXM];

void init()
{
    for (int i = 0; i < MAXN; i++)K[i].clear(),dist[i]=INF,B[i].clear();
    while (pq.size())pq.pop();
    memset(dp, -1, sizeof dp);
}

int main()
{
    int T;
    cin >> T;
    int cas = 1;
    while (T--)
    {
        init();
        scanf("%d%d%d", &m, &n, &k);
        for (int i = 0; i < n; i++)
        {
            int x;
            scanf("%d", &x);
            if (x == 0)scanf("%d", p + i), c[i] = INF;
            else scanf("%d%d", c + i, p + i);
        }
        for (int i = 0; i < k; i++)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            x--;
            K[i].push_back(pii(x, 1));
            for (int j = 0; j < y; j++)
            {
                int xx, yy;
                scanf("%d%d", &xx, &yy);
                xx--;
                K[i].push_back(pii(xx, yy));
                B[xx].push_back(i);
            }
        }
        for (int i = 0; i < n; i++)
        {
            dist[i] = c[i];
            pq.push(pii(i, dist[i]));
        }
        while (pq.size())
        {
            pii t = pq.top();
            pq.pop();
            int u = t.first;
            int d = t.second;
            if (d > dist[u])continue;
            for (int i = 0; i < B[u].size(); i++)
            {
                int tk = B[u][i];
                int v = K[tk][0].first;
                ll up = 0;
                for (int j = 1; j < K[tk].size(); j++)
                {
                    up += dist[K[tk][j].first] * K[tk][j].second;
                }
                if (up < dist[v])
                {
                    dist[v] = up;
                    pq.push(pii(v, dist[v]));
                }
            }
        }
        dp[0] = 0;
        ll maxx = 0;
        for (int i = 0; i <= m; i++)
        {
            if (dp[i] == -1)continue;
            for (int j = 0; j < n; j++)
            {
                if (i + dist[j] <= m)
                {
                    if (dp[i + dist[j]] == -1)dp[i + dist[j]] = dp[i] + p[j];
                    else dp[i + dist[j]] = max(dp[i + dist[j]], dp[i] + p[j]);
                }
            }
            maxx = max(maxx, dp[i]);

        }
        printf("Case #%d: %I64d\n", cas++, maxx);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值