Http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 510

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.