d[i][j]表示从i电梯到j层所用的最短时间。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#define time ttt
using namespace std;
typedef struct node
{
int order,floor;
node(int a=0,int b=0)
{
order=a;
floor=b;
}
} node;
vector<node>g[110];
typedef struct Node
{
int d,t,ele;
Node(int a=0,int b=0,int c=0)
{
d=a,t=b,ele=c;
}
bool operator <(const Node &th) const
{
return th.t<t;
}
} Node;
int time[6];
int dd[6][100];
int main()
{
// freopen("in.txt","r",stdin);
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=0; i<100; i++)
g[i].clear();
for(int i=1; i<=n; i++)
scanf("%d",&time[i]);
for(int i=1; i<=n; i++)
{
char ch;
int a,pre=-1;
while(scanf("%d%c",&a,&ch))
{
if(pre==-1)
{
pre=a;
if(ch=='\n') break;
else
continue;
}
g[pre].push_back(node(i,a));
g[a].push_back(node(i,pre));
pre=a;
if(ch=='\n') break;
}
}
for(int i=1; i<=n; i++)
{
for(int j=0; j<100; j++)
if(j==0) dd[i][j]=0;
else
dd[i][j]=1000000;
}
priority_queue<Node>q;
q.push(Node(0,0,1));
while(!q.empty())
{
Node a=q.top();
q.pop();
int u=a.d;//楼层
int t=a.t;//时间
for(int i=0; i<(int)g[u].size(); i++)
{
node v=g[u][i];
int tt=t,flag=a.ele;
if(v.order!=a.ele&&u!=0)
tt+=60,flag=v.order;
tt+=time[v.order]*(abs(v.floor-a.d));
if(dd[flag][v.floor]>tt)
{
dd[flag][v.floor]=tt;
q.push(Node(v.floor,tt,v.order));
}
}
}
int dis=1000000;
for(int i=1; i<=n; i++)
dis=min(dis,dd[i][k]);
if(dis!=1000000)
printf("%d\n",dis);
else printf("IMPOSSIBLE\n");
}
return 0;
}