POJ 1724 ROADS 最短路

题目大意:有两个权值的最短路问题,要求满足费用不超过一定限度的情况下的最短路。


思路:正常的SPFA加一个小判断,就是当费用高于预期费用的时候不入队,顺便加一个pq吧。


CODE:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 100005
#define INF 0x3f3f3f3f
using namespace std;

int money,points,edges;
int head[MAX],total;
int next[MAX],aim[MAX],length[MAX],cost[MAX];

struct Complex{
	int pos,len,c;
	
	Complex(int _,int __,int ___):pos(_),len(__),c(___) {}
	bool operator <(const Complex &a)const {
		return len > a.len;
	}
};

inline void Add(int x,int y,int len,int c)
{
	next[++total] = head[x];
	aim[total] = y;
	length[total] = len;
	cost[total] = c;
	head[x] = total;
}

int HeapSPFA()
{
	static priority_queue<Complex> q;
	q.push(Complex(1,0,0));
	while(!q.empty()) {
		Complex now = q.top(); q.pop();
		int x = now.pos,len = now.len,c = now.c;
		if(x == points)	return len;
		for(int i = head[x]; i; i = next[i])
			if(c + cost[i] <= money)
				q.push(Complex(aim[i],len + length[i],c + cost[i]));
	}
	return -1;
}

int main()
{
	cin >> money >> points >> edges;
	for(int x,y,z,l,i = 1; i <= edges; ++i) {
		scanf("%d%d%d%d",&x,&y,&z,&l);
		Add(x,y,z,l);
	}
	cout << HeapSPFA() << endl;
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值