HDOJ 题目4396 More lumber is required(至少经过k条边的最短路)

More lumber is required

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 1392    Accepted Submission(s): 538


Problem Description
“More lumber is required” When the famous warcrafts player Sky wants to build a Central Town, he finds there is not enough lumber to build his Central Town. So he needs to collect enough lumber. He lets farmer John to do this work.
  There are several Sawmills have already been built in the world, around them are large forests. Sawmills are connected by bidirectional roads (a sawmill can be connected to itself). When he passes a road, he will get 10 lumber and consume a certain time. Sky needs K lumber. So John needs collect as least K lumber.
  Sawmills are labeled from 1 to N. John initiates at Sawmill S. When he finishes his work, Sky gives him another work: arrive at Sawmill T, and build the Central Town. John needs to design his route carefully because Sky wants to build this Central Town as early as possible. He turns you for help. Please help him calculate the minimum time he needs to finish this work (collect enough lumber and build the Central Town). If impossible just print -1.
  You can read the Sample Input and Output for more information.
 

Input
There are multiply test cases, in each test case:
The first line is two integers N (1<=N<=5000), M (0<=M<=100000) represent the number of sawmills and the number of the roads.
The next M line is three integers A B C (1<=A, B<=N; 1<=C<=100), means there exists a road connected A th sawmill and B th sawmill, and pass this road will cost C time.(The sawmills are labeled from 1 to N).
The last line is three integers S T K (1<=S, T<=N; 0<=K<=500), as mentioned as description.
 

Output
For each test case, print the result in a single line.
 

Sample Input
  
  
4 4 1 2 1 2 3 2 1 3 100 3 4 1 1 3 50
 

Sample Output
  
  
7
 

Author
Wanghang----School of Software Technology, Dalian University of Technology
 

Source
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   4390  4398  4397  4395  4394 
 
ac代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<string>
#include<iostream>
#define min(a,b) (a>b?b:a)
#define INF 0xfffffff
using namespace std;
int dis[5050][550],vis[5050][550],n,m,cnt,head[5050];
int s,t,k,ans;
struct s
{
	int u,v,w,next;
}edge[100010*2];
struct node
{
	int x,y;
}a,temp;
void add(int u,int v,int w)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
void spfa()
{
	int i,j;
	for(i=0;i<=n;i++)
	{
		for(j=0;j<=k;j++)
		{
			dis[i][j]=INF;
		}
	}
	memset(vis,0,sizeof(vis));
	dis[s][0]=0;
	vis[s][0]=1;
	queue<node>q;
	a.x=s;
	a.y=0;
	q.push(a);
	while(!q.empty())
	{
		a=q.front();
		q.pop();
		vis[a.x][a.y]=0;
		if(a.x==t&&a.y==k)
		{
			ans=min(ans,dis[a.x][a.y]);
		}
		for(i=head[a.x];i!=-1;i=edge[i].next)
		{
			temp.x=edge[i].v;
			temp.y=a.y+1;
			if(temp.y>=k)
				temp.y=k;
			if(dis[temp.x][temp.y]>dis[a.x][a.y]+edge[i].w)
			{
				dis[temp.x][temp.y]=dis[a.x][a.y]+edge[i].w;
				if(!vis[temp.x][temp.y])
				{
					vis[temp.x][temp.y]=1;
					q.push(temp);
				}
			}
		}
	}
}
int main()
{
	//int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		cnt=0;
		memset(head,-1,sizeof(head));
		int i;
		for(i=0;i<m;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w);
			add(v,u,w);
		}
		scanf("%d%d%d",&s,&t,&k);
		if(k%10)
			k=k/10+1;
		else
			k=k/10;
		ans=INF;
		spfa();
		if(ans==INF)
			printf("-1\n");
		else
			printf("%d\n",ans);
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值