C++ 代码
#include <bits/stdc++.h>
using namespace std;
const int N = 110, M = N * N;
typedef pair<int, int> pii;
int h[N], v[M], w[M], ne[M], idx;
int dist[N];
bool vis[N];
int grade[N];
int n, m;
void add(int a, int b, int c){
++idx;v[idx] = b;w[idx] = c;ne[idx] = h[a];h[a] = idx;
}
void dijstra(int l, int r){
memset(dist, 0x3f, sizeof dist);
memset(vis, 0, sizeof vis);
priority_queue<pii, vector<pii>, greater<pii> > pq;
pq.push({0, 0});
dist[0] = 0;
while(pq.size()){
auto t = pq.top();pq.pop();
if(t.second == 1) break;
if(vis[t.second]) continue;
vis[t.second] = true;
for(int i = h[t.second];~i;i = ne[i]){
int j = v[i];
if(l <= grade[j] && grade[j] <= r && dist[j] > dist[t.second] + w[i]){
dist[j] = dist[t.second] + w[i];
pq.push({dist[j], j});
}
}
}
}
int main(){
memset(h, -1, sizeof h);
cin >> m >> n;
for(int i = 1;i <= n;++i){
int p, l, x;cin >> p >> l >> x;
grade[i] = l;
add(0, i, p);
for(int j = 1;j <= x;++j){
int t, v;cin >> t >> v;
add(t, i, v);
}
}
int ans = INT_MAX;
for(int i = grade[1]-m;i <= grade[1];++i){
dijstra(i, i + m);
ans = min(ans, dist[1]);
}
cout << ans << endl;
return 0;
}