蒟蒻小声嘀咕:来水一篇题解
题意:
给一个有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;
}