码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-20 16:28:43      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

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 Ath sawmill and Bth 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);
	}
}


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

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/44492361

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!