遍历+dijsktra,注意每次可以使用的更新点的筛选
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
int m,n,t,v;
int p[105],l[105],x[105],vis[105],ans,d[105];
struct edge{ int to, cost; };
vector<edge>G[105];
typedef pair<int,int> P;
int dijsktra(){
priority_queue<P,vector<P>,greater<P> >q;
for(int i=1; i<=n; i++){
d[i]=p[i];
if(!vis[i])q.push(P(d[i],i));
}
while(!q.empty()){
P p= q.top(); q.pop();
int v=p.second;
for(int i=0; i<G[v].size(); i++){
edge e=G[v][i];
if(!vis[e.to]&&d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
q.push(P(d[e.to],e.to));
}
}
}
return d[1];
}
int main(){
while(cin>>m>>n){
memset(p,-1,sizeof(p));
for(int i=1; i<=n; i++)G[i].clear();
for(int i=1; i<=n; i++){
cin>>p[i]>>l[i]>>x[i];
for(int j=1; j<=x[i]; j++){
cin>>t>>v;
edge e;
e.to=i; e.cost=v;
G[t].push_back(e);
}
}
ans=INF;
for(int i=1; i<=n; i++){
int maxn=l[i];//假定该等级为最高等级筛选可交换的
for(int j=1; j<=n; j++){
if(maxn<l[j]||maxn-l[j]>m)
vis[j]=1;
else
vis[j]=0;
}
ans=min(ans,dijsktra());
}
cout<<ans<<endl;
}
return 0;
}