Link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 24 & page = show_problem & problem = 1742
Question:
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 to. 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.
Question:
A building with no more than 100 floors and N elevators have different speeds. In addition, each elevator can only reach the specified floor, and each elevator has its own speed (that is, the time it takes to rise or fall ). If a person walks out of the elevator on a certain floor and wants to take another elevator, then he will wait 60 seconds (whether it is the elevator, or even the elevator that just came out will have to wait 60 seconds ). You do not need to wait when you set up an elevator on the 0th floor.
A person starts from layer 0 and the destination is layer K. Now I want to take these elevators and ask the minimum time required.
Analysis and Summary:
The short-circuit problem adds some conditions, that is, it takes 60 seconds for the transfer.
The key lies in the conversion. First, all the start and end points of the layer that each elevator can reach are saved to the adjacent matrix. If a path already exists, save the least time required.
Then, the shortest path is used to find the minimum time, which can be Dijkstra or spfa.
Note that if the starting point is 0, do not wait for the time. Therefore, do not add 60.
Code:
#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<utility>using namespace std;typedef pair<int,int>pii;priority_queue<pii,vector<pii>,greater<pii> >q;const int N = 105;const int INF = 1000000000;int n, k, T[6], w[N][N], arr[N], d[N];bool vis[N];inline void read_graph(){ for(int i=0; i<N; ++i){ w[i][i] = INF; for(int j=i+1; j<N; ++j) w[i][j]=w[j][i]=INF; } for(int i=0; i<n; ++i) scanf("%d",&T[i]); char ch; for(int i=0; i<n; ++i){ int pos=0; do{ scanf("%d",&arr[pos++]); }while(getchar()!='\n'); for(int j=0; j<pos; ++j){ for(int k=j; k<pos; ++k){ int tmp=abs(arr[j]-arr[k])*T[i]; if(tmp<w[arr[j]][arr[k]]){ w[arr[j]][arr[k]] = w[arr[k]][arr[j]] = tmp; } } } }}inline void SPFA(int src){ memset(vis, 0, sizeof(vis)); for(int i=0; i<N; ++i) d[i]=INF; d[src] = 0; queue<int>q; q.push(src); while(!q.empty()){ int u=q.front(); q.pop(); vis[u] = false; for(int i=0; i<N; ++i){ if(u==0){ if(d[i]>d[u]+w[u][i]){ d[i] = d[u]+w[u][i]; if(!vis[i]){ vis[i] = true; q.push(i); } } } else if(d[i]>d[u]+w[u][i]+60){ d[i] = d[u]+w[u][i]+60; if(!vis[i]){ vis[i] = true; q.push(i); } } } }}inline bool Dijkstra(int src){ for(int i=0; i<N; ++i) d[i] = INF; d[src] = 0; q.push(make_pair(d[src], src)); while(!q.empty()){ pii t=q.top(); q.pop(); int u=t.second; if(t.first!=d[u]) continue; for(int v=0; v<N; ++v){ if(u==0){ if(d[v]>d[u]+w[u][v]){ d[v] = d[u]+w[u][v]; q.push(make_pair(d[v],v)); } } else{ if(d[v]>d[u]+w[u][v]+60){ d[v]=d[u]+w[u][v]+60; q.push(make_pair(d[v],v)); } } } }}int main(){ while(~scanf("%d%d",&n,&k)){ read_graph(); // SPFA(0); Dijkstra(0); if(d[k]!=INF) printf("%d\n", d[k]); else printf("IMPOSSIBLE\n"); } return 0;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)