总体来说还是一道挺水的题,设想一个假想的0点,0点到个商品的距离即直接购买商品的价格,这样就转化为了搜索最短路径的问题,再根据等级制度进行枚举即可~~
//It is a simple problem about searching
#include<iostream>
#include<string.h>
//Have to include this file to use the function memset()
const int MAX=999999;
using namespace std;
int Dij(int down,int up);
int m,n,path[105][105],level[105],price[105];
bool visited[105];
int main(){
cin>>m>>n;
//Initialization
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++){
if(i==j)
path[i][j]=0;
else
path[i][j]=MAX;
}
for(int i=0;i<=n;i++)
level[i]=-1;
int p,l,x,tmp_num,tmp_price,Down,Up,Min=MAX;
for(int i=1;i<=n;i++){
cin>>p>>l>>x;
path[0][i]=p; path[i][i]=0;
level[i]=l;
for(int j=0;j<x;j++){
cin>>tmp_num>>tmp_price;
path[tmp_num][i]=tmp_price;
//Indicate the extra cost from the point tmp_num to the point i
}
}
if(level[1]-m<0)//very important
Down=0;
else
Down=level[1]-m;
Up=level[1]+m;
for(int i=Down;i<=level[1];i++){
//To enumerate every possible circumstance
Dij(i,i+m);
if(price[1]<Min)//Update the minimum
Min=price[1];
}
cout<<Min<<endl;
system("pause");
return 0;
}
//The process of searching the minimum price
//An imaginary point 0 is necessary to solve the problem
int Dij(int down,int up){
memset(visited,false,sizeof(visited));
for(int i=1;i<=n;i++)
price[i]=path[0][i];
int via;
for(int i=0;i<n;i++){
int tmp_min=MAX;
for(int j=1;j<=n;j++){
//To hunt for the minimum price at the moment
if(visited[j]==false&&price[j]<tmp_min&&level[j]>=down&&level[j]<=up){
tmp_min=price[j];
via=j;
}
}
visited[via]=true;
for(int j=1;j<=n;j++){
if(price[j]>price[via]+path[via][j]&&level[j]>=down&&level[j]<=up)
//Update the minimum price
price[j]=price[via]+path[via][j];
}
}
return 0;
}