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!" |
A skyscraper has no more than 100 floors, numbered from 0 to 99.It has n (1<=n<=5) elevators which travelup and down at (possibly) different speeds. For each iin {1, 2,... n}, elevator number i takesTi (1<=Ti<=100) secondsto 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 asquickly as possible. Assume that you do not need to wait toboard the first elevator you step into and (for simplicity) theoperation of switching an elevator on some floor always takesexactly a minute. Of course, both elevators have to stop atthat floor. You are forbiden from using the staircase. No oneelse is in the elevator with you, so you don't have to stop ifyou don't want to. Calculate the minimum number of secondsrequired to get from floor 0 to floor k (passing floork while inside an elevator that does not stop there doesnot count as "getting to floor k").
Input
The input will consist of a number of test cases. Each test casewill begin with two numbers, n and k, on a line. Thenext line will contain the numbers T1,T2,... Tn. Finally, the nextn lines will contain sorted lists of integers - the firstline will list the floors visited by elevator number 1, the nextone will list the floors visited by elevator number 2, etc.
Output
For each test case, output one number on a line by itself - theminimum number of seconds required to get to floor k fromfloor 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 floor30 (85 seconds) for a total of 275 seconds.
In the second example, take elevator 1 to floor 10, switch toelevator 2 and ride it until floor 25. There, switch back toelevator 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 tofloor 20 and then elevator 3 to floor 50.
In the last example, the one elevator does not stop at floor 1.
思路:把他抽象成最短路,楼层看成节点,楼层之间的时间看成边权值,然后SPFA求一次最短路。
下面是代码:
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;
const int MAX=100000;
const int INF=1000000000;
struct node
{
int u,next,time;
}edge[MAX];
int pre[110],speed[10],floors[110],dis[110];
bool vis[110];
int num,N,K;
void init()
{
num=0;
memset(pre,-1,sizeof(pre));
}
void add_edge(int x,int cnt)
{
for(int i=0;i<cnt;i++)
for(int j=i+1;j<cnt;j++)
{
edge[num].next=pre[floors[i]];
edge[num].u=floors[j];
edge[num].time=(floors[j]-floors[i])*speed[x];
pre[floors[i]]=num++;
edge[num].next=pre[floors[j]];
edge[num].u=floors[i];
edge[num].time=(floors[j]-floors[i])*speed[x];
pre[floors[j]]=num++;
}
}
void SPFA()
{
for(int i=0;i<=100;i++)
{
dis[i]=INF;
vis[i]=false;
}
dis[0]=0;
vis[0]=true;
queue<int> q;
q.push(0);
while(!q.empty())
{
int t=q.front();
q.pop();
vis[t]=false;
for(int i=pre[t];i!=-1;i=edge[i].next)
{
int v=edge[i].u;
if(dis[v]>dis[t]+edge[i].time+60)
{
dis[v]=dis[t]+edge[i].time+60;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
if(dis[K] == INF)
cout<<"IMPOSSIBLE"<<endl;
else
{
if(K == 0)
puts("0");
else cout<<dis[K]-60<<endl;//减去初始顶点的60s
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while(scanf("%d%d",&N,&K)!=EOF)
{
init();
for(int i=0;i<N;i++)
scanf("%d",&speed[i]);
for(int i=0;i<N;i++)
{
int num=0;
while(true)
{
scanf("%d",&floors[num++]);
if(getchar()=='\n')
break;
}
add_edge(i,num);
}
SPFA();
}
return 0;
}