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

UVA 10801 Lift Hopping (最短路)

时间:2014-09-03 18:09:37      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:algorithm   图论   最短路   acm   

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1742

Problem ?
Lift Hopping
Time Limit: 1 second

Ted the bellhop: "I‘m coming up and if there isn‘t
a dead body by the time I get there, I‘ll make one
myself. You!"

Robert Rodriguez, "Four Rooms."

A skyscraper has no more than 100 floors, numbered from 0 to 99. It has n (1<=n<=5) elevators which travel up and down at (possibly) different speeds. For each i in {1, 2,... n}, elevator number i takes Ti (1<=Ti<=100) seconds to travel between any two adjacent floors (going up or down). Elevators do not necessarily stop at every floor. What‘s worse, not every floor is necessarily accessible by an elevator.

You are on floor 0 and would like to get to floor k as quickly as possible. Assume that you do not need to wait to board the first elevator you step into and (for simplicity) the operation of switching an elevator on some floor always takes exactly a minute. Of course, both elevators have to stop at that floor. You are forbiden from using the staircase. No one else is in the elevator with you, so you don‘t have to stop if you don‘t want to. Calculate the minimum number of seconds required to get from floor 0 to floor k (passing floor k while inside an elevator that does not stop there does not count as "getting to floor k").

Input
The input will consist of a number of test cases. Each test case will begin with two numbers, n and k, on a line. The next line will contain the numbers T1T2,... Tn. Finally, the next n lines will contain sorted lists of integers - the first line will list the floors visited by elevator number 1, the next one will list the floors visited by elevator number 2, etc.

Output

For each test case, output one number on a line by itself - the minimum number of seconds required to get to floor k from floor 0. If it is impossible to do, print "IMPOSSIBLE" instead.

Sample Input Sample Output
2 30
10 5
0 1 3 5 7 9 11 13 15 20 99
4 13 15 19 20 25 30
2 30
10 1
0 5 10 12 14 20 25 30
2 4 6 8 10 12 14 22 25 28 29
3 50
10 50 100
0 10 30 40
0 20 30
0 20 50
1 1
2
0 2 4 6 8 10
275
285
3920
IMPOSSIBLE

Explanation of examples

In the first example, take elevator 1 to floor 13 (130 seconds), wait 60 seconds to switch to elevator 2 and ride it to floor 30 (85 seconds) for a total of 275 seconds.

In the second example, take elevator 1 to floor 10, switch to elevator 2 and ride it until floor 25. There, switch back to elevator 1 and get off at the 30‘th floor. The total time is 
10*10 + 60 + 15*1 + 60 + 5*10 = 285 seconds.

In example 3, take elevator 1 to floor 30, then elevator 2 to floor 20 and then elevator 3 to floor 50.

In the last example, the one elevator does not stop at floor 1.


Problemsetter: Igor Naverniouk
Alternate solutions: Stefan Pochmann, Frank Pok Man Chu


题意:

有若干部电梯,给出每部电梯通过一层的时间和能停的楼层,换乘电梯要用60s,电梯可上可下,求从0层到目标层的最短时间。

分析:

最短路。假设目前在i电梯j层,那么能更新的状态有两种:i电梯的其他楼层和j楼层的其他电梯。这也是建图的依据,感觉这题不显式地建图会方便些。


/*
 *
 *	Author	:	fcbruce
 *
 *	Date	:	2014-09-03 16:31:53 
 *
 */
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10

#ifdef _WIN32
	#define lld "%I64d"
#else
	#define lld "%lld"
#endif

#define maxm 
#define maxn 101

using namespace std;

int n,obj;

int v[5];
vector <int> stop[5];
int G[5][maxn],d[5][maxn];
pair<int,int> q[maxn<<8];
bool inq[5][maxn];


void SPFA()
{
	memset(d,0x3f,sizeof d);
	memset(inq,0,sizeof inq);
	int f=0,r=-1;
	for (int i=0;i<n;i++)
		if (G[i][0])
		{
			q[++r]=make_pair(i,0);
			d[i][0]=0;
		}
		
	while (f<=r)
	{
		pair<int,int> x=q[f++];
		inq[x.first][x.second]=false;
		for (int u:stop[x.first])
		{
			if (d[x.first][u]>d[x.first][x.second]+abs(x.second-u)*v[x.first])
			{
				d[x.first][u]=d[x.first][x.second]+abs(x.second-u)*v[x.first];
				if (!inq[x.first][u])
				{
					inq[x.first][u]=true;
					q[++r]=make_pair(x.first,u);
				}
			}
		}
		for (int i=0;i<n;i++)
		{
			if (x.first==i || G[i][x.second]==false)	continue;
			if (d[i][x.second]>d[x.first][x.second]+60)
			{
				d[i][x.second]=d[x.first][x.second]+60;
				if (!inq[i][x.second])
				{
					inq[i][x.second]=true;
					q[++r]=make_pair(i,x.second);
				}
			}
		}
	}
}

int main()
{
	#ifdef FCBRUCE
		freopen("/home/fcbruce/code/t","r",stdin);
	#endif // FCBRUCE
	
	char str[233];
	
	while (~scanf( "%d%d",&n,&obj))
	{
		memset(G,0,sizeof G);
	
		for (int i=0;i<n;i++)
			scanf( "%d",v+i);
			
		getchar();
		
		for (int i=0;i<n;i++)
		{
			gets(str);
			stringstream s(str);
			int x;
			stop[i].clear();
			while (s>>x)
			{
				stop[i].push_back(x);
				G[i][x]=true;
			}
		}
		
		SPFA();
		
		int MIN=INF;
		
		for (int i=0;i<n;i++)
			if (G[i][obj])	MIN=min(MIN,d[i][obj]);
			
		if (MIN==INF)
			puts( "IMPOSSIBLE");
		else
			printf( "%d\n",MIN);
	}
	
	return 0;
}



UVA 10801 Lift Hopping (最短路)

标签:algorithm   图论   最短路   acm   

原文地址:http://blog.csdn.net/u012965890/article/details/39030639

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