|Vijos|图论最短路|P1082 丛林冒险

http://vijos.org/p/1082

非常有代表性的题目,在SPFA时多加一个判断即可

此题用SPFA有反例,正解搜索,此题解是错误的 (2016.11.27更改)

此问题可解所有体力+权值的最短路问题

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#define ms(i,j) memset(i, j, sizeof(i));
using namespace std;
struct node{
	int u;
	int v;
	int p;
	int t;
	node(int u1,int v1, int p1,int t1) : u(u1),v(v1),p(p1),t(t1) {}
};
const int maxn = 5000 + 2, maxm = 40000 + 2;
vector<node> a[maxm];
int dis[maxn];
int ex[maxn];
int p[maxn];
int n,m,s,t,k;
queue<int> q;
int main () 
{ 
	scanf("%d%d", &n, &m);
	for (int i=1;i<=m;i++)
	{
		int x,y,c,d;
		scanf("%d%d%d%d", &x,&y,&c,&d);
		a[x].push_back(node(x,y,c,d));
		a[y].push_back(node(y,x,c,d));
	} 
	scanf("%d%d%d", &s, &t, &k);
	//SPFA
	p[s] = k;
	ms(ex,false); ex[s]=true;
	ms(dis,27); dis[s]=0; dis[t]=100000000;
	q.push(s);
	while(!q.empty())
	{
		int u = q.front();q.pop();ex[u]=false;
		for (int i=0;i<a[u].size();i++)
		{
			node pn = a[u][i];
			if (dis[pn.v]>dis[u]+pn.t&&p[u]>=pn.p)
			{
				dis[pn.v]=dis[u]+pn.t;
				p[pn.v] = p[u]-pn.p;
				if (!ex[pn.v])
				{
					ex[pn.v] = true;
					q.push(pn.v);
				}
			}
		}
	}	
	printf("%d\n", (dis[t]==100000000) ? -1 : dis[t]);
	return 0;
}


转载于:https://www.cnblogs.com/flyinthesky1/p/6384335.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值