就该用邻接矩阵写的,,,非要手欠写邻接表
#include <bits/stdc++.h>
#define please return
#define Accept 0
#define orz ;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 200100;
struct node // 存这件东西的钱、地位、替代品总数
{
int w, rank, q;
int a[100], b[100]; // 替代品的编号、优惠价格
}s[N];
int m, n;
int dist[N]; // 最小花费
bool st[N]; // 标记数组
int w[N], e[N], ne[N], h[N], idx;
void add(int a, int b, int c) // 常规默写邻接表
{
w[idx] = c, e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
int dijkstra(int l, int r) // 求地位在[l, r]内的最小交易价格
{
memset(st, false, sizeof(st));
memset(dist, 0x3f, sizeof(dist));
priority_queue<PII, vector<PII>, greater<PII> > q;
dist[0] = 0; // 将0点作为虚拟的源点,这样就可以把问题转化为求0到1的最短路
q.push({0, 0});
while (!q.empty())
{
auto t = q.top();
q.pop();
int distance = t.first, ver = t.second;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
if (s[j].rank < l || s[j].rank > r) continue; // 如果交易人的地位不在区间内是不能交易的
if (dist[j] > dist[ver] + w[i])
{
dist[j] = dist[ver] + w[i];
q.push({dist[j], j});
}
}
}
return dist[1];
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
memset(h, -1, sizeof(h));
cin >> m >> n;
for (int i = 1; i <= n; i ++) // 存信息
{
cin >> s[i].w >> s[i].rank >> s[i].q;
for (int j = 1; j <= s[i].q; j ++) cin >> s[i].a[j] >> s[i].b[j];
}
s[0] = {s[1].w, s[1].rank}; // 建一个虚拟源点
for (int i = 1; i <= n; i ++) add(0, i, s[i].w); // 建图
for (int i = 1; i <= n; i ++) // 建图
{
for (int j = 1; j <= s[i].q; j ++)
{
add(s[i].a[j], i, s[i].b[j]);
}
}
int Min = 2e9;
for (int i = s[1].rank - m; i <= s[1].rank; i ++) Min = min(Min, dijkstra(i, i + m)); // 枚举所有可交易区间,取最小值
cout << Min << '\n';
please Accept orz
}