hdu4396

/*
分析:
    每条边可重复通过,求至少经过K次边的最短路。


                                    2012-10-14
*/








#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"queue"
using namespace std;
int s,e,LIMIT;
struct node
{
	int index;
};
struct Eage
{
	int from,to,len;
	int next;
}eage[200011];
int tot,head[5011];
int dis[5011][55];
void add(int a,int b,int len)
{
	eage[tot].from=a;
	eage[tot].to=b;
	eage[tot].len=len;
	eage[tot].next=head[a];
	head[a]=tot++;
}
void SPFA()
{
	int l,j;
	int flag;
	int hash[5011];
	queue<node>q;
	node now,next;

	memset(hash,0,sizeof(hash));
	memset(dis,127,sizeof(dis));
	now.index=s;
	dis[s][0]=0;
	q.push(now);
	hash[s]=1;

	while(!q.empty())
	{
		now=q.front();
		q.pop();
		hash[now.index]=0;
		for(j=head[now.index];j!=-1;j=eage[j].next)
		{
			flag=0;
			dis[now.index][LIMIT]=dis[now.index][LIMIT]>dis[now.index][LIMIT+1]?dis[now.index][LIMIT+1]:dis[now.index][LIMIT];
			for(l=0;l<=LIMIT;l++)
			{
				if(dis[now.index][l]+eage[j].len<dis[eage[j].to][l+1])
				{
					flag=1;
					dis[eage[j].to][l+1]=dis[now.index][l]+eage[j].len;
				}
			}
			if(flag && !hash[eage[j].to])
			{
				next.index=eage[j].to;
				q.push(next);
				hash[next.index]=1;
			}
		}
	}
}
int main()
{
	int n,m;
	int a,b,c;
	int ans;
	while(scanf("%d%d",&n,&m)!=-1)
	{
		memset(head,-1,sizeof(head));
		tot=0;
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&c);
			add(a,b,c);
			add(b,a,c);
		}
		scanf("%d%d%d",&s,&e,&LIMIT);
		LIMIT=LIMIT/10+(LIMIT%10==0?0:1);

		SPFA();

		ans=1111111111;
		ans=ans>dis[e][LIMIT]?dis[e][LIMIT]:ans;
		if(ans==1111111111)	printf("-1\n");
		else				printf("%d\n",ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值