Original question:
A skyscraper have no more than 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 T I (1≤t i≤100) seconds to travel between any and adjacent floors ( Going up or down).
Elevators don't necessarily stop at every floor. What's worse, not every floor is necessarily accessible by an elevator.
You is on the floor 0 and would as quickly as possible. Assume that you don't need to wait to board the first elevator your step into and (for simplicity) the operation of Switch ing an elevator on some floor always takes exactly a minute. Of course, both elevators has a to stop at this floor. You is forbiden from using the staircase. No one else is on the elevator with you and 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 would consist of a number of test cases. Each test case would begin with a numbers, N and K,
On a line. The next line would contain the numbers T 1, t 2, ... T N.
Finally, the next n lines would contain sorted lists of integers–the first line would list the floors visited
By elevator number 1, the next one would list the floors visited by elevator number 2, etc.
Output
For each test case, the 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.
Explanation of examples
In the first example, take elevator 1-to-floor (seconds), wait for seconds to switch-to-elevator 2 and ride it to FL Oor (seconds) for a total of 275 seconds.
In the second example, take elevator 1-to-floor, switch-to-elevator 2 and ride it until floor 25.
There, switch back to elevator 1 and get off at the "th floor." The total time is 10∗10 + 60 + 15∗1 + 60 + 5∗10 = 285 seconds.
In Example 3, take elevator 1-to-floor, then elevator 2-to-floor and then elevator 3-floor 50.
In the last example, the one elevator does not stop at floor 1.
Sample Input
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
Sample Output
275
285
3920
Impossible
Effect:
(From Lucky Cat)
There is a Nicholas skyscraper, its floor will not exceed 100 layers (number from 0~99). The Nicholas has a n (1 <= n <= 5) Ladder that moves at different speeds at certain layers. Part I of the ladder to move in the adjacent layer requires ti seconds (1 <= ti <= 100), not every elevator has a stop, so there is a possibility that there is no ladder can reach.
Now that you are on the No. 0 level, you want to go as fast as you could to the K-th layer. Here are a few things to note:
Suppose you don't have to wait for the ladder (that is, when you are on a certain level, if the ladder has a stop, it is already open and waiting for you).
It is a minute to switch from this ladder to another in one of the stairs (and of course all 2 elevators have a stop). You mustn't walk the stairs, you must take the ladder.
You're the only one who rides the ladder, so you can keep some layers if you don't want to.
Your task is to figure out the minimum amount of time from the No. 0 layer to the K-layer. (referred to to the K-layer, refers to the ladder has stopped on the K-layer.) If only in the ladder, through the K-layer without stopping, it is not to reach the K-layer.
Input
The input contains multiple measurements, and each input is stated as follows:
The first column has 2 integers, N and K.
The next column has n integers representing T1, T2, ..., Tn.
Then there are n columns, each of which represents a staircase with a stop. Please refer to sample Input.
Output
The minimum amount of time is spent on each measurement from the No. 0 to the K-th layer. If not, please input impossible.
Sample Input
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
Sample Output
275
285
3920
Impossible
The
Input example says:
The first measurement: Take the ladder 1 to 13 floor (130 seconds), spend 60 seconds to the ladder 2, and then sit to 30 f (85 seconds), so total: 130+60+85=275 seconds.
Second measurement: Take the ladder 1 to 10 floor, switch to the ladder 2, and then sit to 25 floor, and then switch to the ladder 1, and then sit to 30 floor, so total: 10*10+60+15*1+60+5*10=285 seconds.
Third measure: Take the ladder 1 to 30 floor, switch to the ladder 2, and then sit to 20 floor, and then switch to the ladder 3, sit 50 floor so total: 10*30+60+10*50+60+30*100=3920 seconds.
Fourth measurement: You can't sit on the 1th floor.
#include <bits/stdc++.h> using namespace std;
const int max_v=501;
struct Edge {int to,cost;};
typedef pair<int,int> P;
int V;
Vector<edge> G[max_v];
int D[max_v];
BOOL Used[max_v];
int speed[6];
Vector<int> Vi[5];
void Dijkstra (int s) {priority_queue<p,vector<p>,greater<p> > que;
memset (used,false,sizeof (used));
Fill (d, D+v,int_max);
d[s]=0;
Que.push (P (0,s));
while (!que.empty ()) {P p=que.top ();
Que.pop ();
int V=p.second; if (d[v]<p.first| |
USED[V]) continue;
Used[v]=true;
for (int i=0;i<g[v].size (); i++) {Edge e=g[v][i];
if (d[e.to]>d[v]+e.cost) {d[e.to]=d[v]+e.cost;
Que.push (P (d[e.to],e.to));
}}}} void Ini () {for (int i=0;i<5;i++) vi[i].clear ();
for (int i=0;i<=v;i++) g[i].clear ();
} int n,k;
StringStream SS; INT Mark[6];
int main () {Ios::sync_with_stdio (false);
mark[0]=0;
for (int i=1;i<6;i++) mark[i]=mark[i-1]+100;
while (cin>>n>>k) {v=mark[n];
INI ();
for (int i=0;i<n;i++) cin>>speed[i];
String T;
Getline (cin,t);
for (int i=0;i<n;i++) {ss.clear ();
string S;
Getline (cin,s);
ss<<s;
int tmp;
while (SS>>TMP) vi[i].push_back (TMP);
} for (int i=0;i<n;i++) {int len=vi[i].size (); for (int j=0;j<len-1;j++) {G[vi[i][j]+mark[i]].push_back (edge{vi[i][j+1]+mark[i],speed[i]* (
VI[I][J+1]-VI[I][J]);
G[vi[i][j+1]+mark[i]].push_back (edge{vi[i][j]+mark[i],speed[i]* (Vi[i][j+1]-vi[i][j])});
}} for (int i=0;i<n;i++) {for (int j=0;j<i;j++) {for (int x=0;x<vi[i].size (); x + +) {for (int y=0;y<vi[j].s
Ize (); y++) {if (Vi[i][x]==vi[j][y]) {
G[vi[i][x]+mark[i]].push_back (edge{vi[j][y]+mark[j],60});
G[vi[j][y]+mark[j]].push_back (edge{vi[i][x]+mark[i],60});
}}}}} int ans=int_max;
for (int i=0;i<n;i++) {if (vi[i][0]%100==0) {Dijkstra (0+mark[i]);
for (int j=0;j<n;j++) ans=min (Ans,d[k+mark[j]);
}} if (Ans==int_max) cout<< "Impossible" <<endl;
else cout<<ans<<endl;
} return 0; }
Answer:
is a very obvious shortest path problem, can be used wide search can also use Dijkstra algorithm.
Here I choose Dijkstra.
The topic description can be transferred to the elevator to the same floor, so to solve the problem is in this figure how to connect all the elevators into a diagram, and then the shortest path to solve. As the highest floor is only 100 floors, and up to 5 elevators, so you can use the idea of the hash, the first elevator floor is 0-99, the second elevator floor is 100 to 199, and so on, the same floor between the weight is the transfer elevator 60 seconds time, For example, the 30th floor from Elevator 1th to 2nd elevator is from node 30 to node 130. After building a map with the shortest path to find out d[n] to the node n the shortest path is how much, the same floor of the different elevators to take the lowest value, if the minimum value is Int_max then unreachable.