现在的位置: 首页 > 综合 > 正文

POJ 1724 ROADS 最短路

2017年11月21日 ⁄ 综合 ⁄ 共 984字 ⁄ 字号 评论关闭

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

思路:正常的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;
}

抱歉!评论已关闭.