思路:
建图:
1.以S = 0 为超级源点,所有的点到由S到该点的距离等于其本身的价格,
2.每一个物品可以由替代还品 + 优惠价得到,也就是替代品->原物品的距离为优惠价
最后再枚举满足长等级的所有区间跑dijkstra取最小值
代码:
#include <iostream>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <climits>
#include <iomanip>
#include <queue>
#include <vector>
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define debug(a) cout << "debug : " << (#a) << " = " << a << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 110;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 998244353;
int m, n, S; //S为超级源点
int lv[N], dis[N];
vector<PII> a[N];
bool vis[N];
int dijkstra(int l, int r)
{
memset(dis, INF, sizeof dis);
memset(vis, false, sizeof vis);
priority_queue<PII, vector<PII>, greater<PII>> pq;
dis[S] = 0;
pq.push({0, S});
while (pq.size())
{
PII t = pq.top();
pq.pop();
int idx = t.second, distance = t.first;
if (vis[idx])
continue;
vis[idx] = true;
for (int i = 0; i < a[idx].size(); i++)
{
int j = a[idx][i].second, w = a[idx][i].first;
if (lv[j] >= l && lv[j] <= r) //判断此时加入点是否符合等级区间
{
if (dis[j] > distance + w)
{
dis[j] = distance + w;
pq.push(PII(dis[j], j));
}
}
}
}
return dis[1];
}
int main()
{
cin >> m >> n;
for (int i = 1; i <= n; i++)
{
int p, l, x;
cin >> p >> l >> x;
a[S].push_back({p, i});
lv[i] = l;
for (int j = 1; j <= x; j++)
{
int l, w;
cin >> l >> w;
a[l].push_back(PII(w, i)); //替代物品到原物品 i 的距离为优惠价w
}
}
int ans = INF;
for (int i = lv[1] - m; i <= lv[1]; i++) //枚举满足酋长等级的所有等级区间
{
int l = i, r = i + m;
ans = min(ans, dijkstra(l, r));
}
cout << ans << endl;
return 0;
}