HDU3790——最短路径问题(spfa)

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#define inf 10000000
using namespace std;

struct node
{
	int to,weight,next,ww;
}road[100010];

int head[1500];int mark[1500];int dis[1500];int cost[1500];

int n,m,cut;

void init()
{
	memset(road,0,sizeof(road));
	memset(head,-1,sizeof(head));
	cut=0;
	for(int i=0;i<1500;i++)
	{
		mark[i]=0;
		dis[i]=inf;
		cost[i]=inf;
	}
}
void add(int x,int y,int z,int w)
{
	road[cut].weight=z;
	road[cut].ww=w;
	road[cut].to=y;
	road[cut].next=head[x];
	head[x]=cut++;
}
void spfa_bfs(int s)
{
	queue<int>q;
	int l,i;
	mark[s]=1;
	dis[s]=0;
	cost[s]=0;
	while(!q.empty()) q.pop();
	q.push(s);
	while(!q.empty())
	{
		l=q.front();
		q.pop();
		mark[l]=0;
		for(i=head[l];i+1;i=road[i].next)
		{
			if(road[i].weight+dis[l]<dis[road[i].to])
			{
				dis[road[i].to]=road[i].weight+dis[l];
				cost[road[i].to]=road[i].ww+cost[l];
				if(mark[road[i].to]==0)
				{
					q.push(road[i].to);
					mark[road[i].to]=1;
				}
			}
			else if(road[i].weight +dis[l]==dis[road[i].to]&&cost[l]+road[i].ww<dis[road[i].to])
			{
			//	dis[road[i].to ]=road[i].weight +dis[l];
				cost[road[i].to]=road[i].ww+cost[l];
				if(mark[road[i].to]==0)
				{
					q.push(road[i].to);
					mark[road[i].to]=1;
				}
			}
		}
	}
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(n==0||m==0) break;
		init();
		int a,b,p,d;
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d%d",&a,&b,&d,&p);
			add(a,b,d,p);
			add(b,a,d,p);
		}
		int start,end;
		scanf("%d%d",&start,&end);
		spfa_bfs(start);
		printf("%d %d\n",dis[end],cost[end]);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值