Ultraviolet A 10801 lift hopping (Shortest Path)

Source: Internet
Author: User

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 hasN(1 <=N<= 5) elevators which travel up and down at (possibly) different speeds. For eachIIn {1, 2,... n}, elevator numberITakesTi(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 wowould like to get to floorKAs 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. calculate the minimum number of seconds required to get from Floor 0 to floorK(Passing floorKWhile inside an elevator that does not stop there does not count as "Getting to floorK").

Input
The input will consist of a number of test cases. Each test case will begin with two numbers,NAndK, On a line. The next line will contain in the numbersT1,T2,...TN. Finally, the nextNLines 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 floorKFrom Floor 0. If it is impossible to do, print "impossible" instead.

Sample Input Sample output
2 3010 50 1 3 5 7 9 11 13 15 20 994 13 15 19 20 25 302 3010 10 5 10 12 14 20 25 302 4 6 8 10 12 14 22 25 28 293 5010 50 1000 10 30 400 20 300 20 501 120 2 4 6 8 10
2752853920IMPOSSIBLE

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


Question:

If there is a cadre elevator, it gives the time for each elevator to pass through the first floor and the floor that can be stopped. It takes 60 s to change to the elevator. The elevator can go up or down and find the shortest time from the 0 floor to the target layer.

Analysis:

The shortest path. Assume that you are currently on the J-level of the I-elevator. There are two types of status updates: the other floors of the I-elevator and the other elevators on the J-floor. This is also the basis for creating images. It is easier to create images without explicit questions.


/* * *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 101using 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 FCBRUCEfreopen("/home/fcbruce/code/t","r",stdin);#endif // FCBRUCEchar 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");elseprintf( "%d\n",MIN);}return 0;}


Ultraviolet A 10801 lift hopping (Shortest Path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.