soj 2932:道路

题目描述: http://cs.scu.edu.cn/soj/problem.action?id=2932

解题思路 : 优先队列 + BFS;



#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;



struct edge
{
	int des;
	int len;
	int money;

	bool operator < (const edge &a)const
	{
		if(len == a.len )
             return money > a.money;
		else
			 return len > a.len;
	}
};

vector<edge>vec[100+10];

int bfs(int s, int total, int dest)
{
	priority_queue<edge>que;
	edge temp ;
	temp.des = s;
	temp.len = 0;
	temp.money = 0;

	que.push(temp);

	while(!que.empty())
	{
		temp = que.top();
		que.pop();

		if(temp.des == dest)
			return temp.len;

		int i;
		edge now;

		for(i = 0; i < vec[temp.des].size(); ++i)
		{
			now = vec[temp.des][i];
			now.money += temp.money;

			if(now.money <= total)
			{
				now.len += temp.len;
				que.push(now);
			}
		}
	}

	return -1;
}



int main()
{
	int N,K,R;
//	freopen("in.txt","r",stdin);
	while(scanf("%d %d %d",&K,&N,&R)==3)
	{
		int i;
		int s,d,l,t;

		for(i = 0; i < N; ++i)
			vec[i].clear();

		for(i = 0; i < R; ++i)
		{
			scanf("%d %d %d %d",&s,&d,&l,&t);
			edge temp;
			temp.des = d;
			temp.len = l;
			temp.money = t;
            
			vec[s].push_back(temp);
		}

		int res = bfs(1,K,N);
		printf("%d\n",res);
	}
//	system("pause");
	return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值