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

Codeforces 101206 I & HDU 6007 Mr. Panda and Crystal

程序员文章站 2022-05-22 11:41:09
...

题意:给定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);
    }
}