J. Grammy and Jewelry

蒟蒻小声嘀咕:来水一篇题解

题意:

给一个有n个顶点的无向图, 顶点2到n上每个顶点均放有无限的珠宝,单个顶点上的所有珠宝具有相同的价值,Grammy从顶点1出发,经过每条边耗时均为1,当把珠宝从第i个顶点拿回放到第一个顶点就可以得到该珠宝。给定一个时间限制T,枚举时间1-T每一个时间限制下能获取的最大珠宝的价值。

比赛时没读懂题意,后来分析以一下,是一个单源最短路+完全背包问题,背包容量即为T,先迪杰斯特拉求出dist数组,获取某一顶点珠宝所用时间为dist[i]*2。(其实是dist[i]*2*t,因为经过每条边耗时为1,所以t=1)。

以下为ac代码 

#include <iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define TLE std::ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
int n, m, k;
int a[N], dist[N], f[N];
bool st[N];
int e[2 * N], w[2 * N], ne[2 * N], h[N], cnt;
void add(int a, int b, int c)
{
    e[cnt] = b, w[cnt] = c, ne[cnt] = h[a], h[a] = cnt++;
}
void dijstra()//迪杰斯特拉模板
{
    priority_queue<PII, vector<PII>, greater<PII>>q;
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    q.push({ 0,1 });
    while (q.size())
    {
        PII t = q.top();
        q.pop();
        int v = t.second;
        if (st[v])continue;
        st[v] = true;
        for (int i = h[v]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[v] + w[i])
            {
                dist[j] = dist[v] + w[i];
                q.push({ dist[j],j });
            }
        }
    }
}

int main()
{
    cin >> n >> m >> k;
    for (int i = 2; i <= n; i++)cin >> a[i];
    memset(h, -1, sizeof h);
    while (m--)
    {
        int a, b, c;
        cin >> a >> b;
        add(a, b, 1);
        add(b, a, 1);
    }
    dijstra();
    for (int i = 1; i <= n; i++)//完全背包模板
    {
        for (int j = dist[i] * 2; j <= k; j++)
            f[j] = max(f[j], f[j - dist[i] * 2] + a[i]);
    }
    for (int t = 1; t <= k; t++)
        cout << f[t] << (t == k ? "\n" : " ");

    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值