Codeforces 101206 I & HDU 6007 Mr. Panda and Crystal

题意:给定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);
    }
}
©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值