The meaning is not interpreted in Chinese ..
Idea: Use each item as the vertex in the graph, take the preferential price as the edge right, create a graph, and here let the minimum gold coins needed, so it can be converted into the shortest-circuit problem, here we introduce a super source point 0, which can be seen as that each item can be exchanged with itself, but there is no preferential price. When you cannot find the items that can be exchanged (you can only exchange them with yourself ), returns the current shortest path length, which is the minimum gold coin required.
#include<iostream>#include<vector>#include<algorithm>#include<string.h>#include<queue>#include<cstdio>#define N 105using namespace std;struct Gnode{Gnode() {}Gnode(int len,int num):len(len),num(num) {}int len,num;};struct Gnode1{Gnode1() {}Gnode1(int num,int dis,int maxlev,int minlev):num(num),dis(dis),maxlev(maxlev),minlev(minlev) {}int num,dis,maxlev,minlev;bool operator< (const Gnode1 &now) const{return now.dis<dis;}};int lev[N],dlev;int dijkstra(vector<vector<Gnode> >&Graph){priority_queue<Gnode1> Q;Q.push(Gnode1(1,0,lev[1],lev[1]));while(1){Gnode1 cur=Q.top();if(cur.num==0) return cur.dis;for(int i=0;i<Graph[cur.num].size();++i){if(Graph[cur.num][i].num==0||(cur.maxlev-lev[Graph[cur.num][i].num]<=dlev&&lev[Graph[cur.num][i].num]-cur.minlev<=dlev)){int maxx=max(cur.maxlev,lev[Graph[cur.num][i].num]);int minx=min(cur.minlev,lev[Graph[cur.num][i].num]);Q.push(Gnode1(Graph[cur.num][i].num,cur.dis+Graph[cur.num][i].len,maxx,minx));}}Q.pop();}}int main(){int n;while(cin>>dlev>>n&&dlev&&n){vector<vector<Gnode> >Graph (n+1);memset(lev,0,sizeof(lev));for(int i=1;i<=n;++i){int price,m;cin>>price>>lev[i]>>m;Graph[i].push_back(Gnode(price,0));for(int j=0;j!=m;++j){int a,b;cin>>a>>b;Graph[i].push_back(Gnode(b,a));}}cout<<dijkstra(Graph)<<endl;}return 0;}