poj1062 昂贵的婚礼

总体来说还是一道挺水的题,设想一个假想的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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值