题意:有n个电梯,目的地是第K层(起点是第0层),给出每个电梯的速度,以及每个电梯能到达的层数,如果中途需要换电梯的话,时间需要+60,求到达目的地的最短时间;
思路:Dij求最短路。如果是另一条路比较短的话,相当于乘别的电梯,也就是再+60,自己不可能和自己比,即d[e.v] 和 d[e.u] + d + 60比较即可;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <sstream>
#include <queue>
#include <vector>
#define repu(i,a,b) for(int i=a;i<b;i++)
using namespace std;
string s;
#define N 110
int v[N],t[N];
const int maxn=;
const int INF=0x3f3f3f3f;
int rode[maxn];
int st = ,ed,k; struct Edge
{
int u, v,d;
Edge(int u, int v, int d):u(u), v(v), d(d) {}
}; struct qnode
{
int u;
int d;
qnode(int u, int d):u(u), d(d) {}
bool operator < (const qnode a)const
{
return d > a.d;
}
}; struct Dijkstra
{
int n;
vector<int> G[maxn];
vector<Edge> edge;
int d[maxn];
bool vis[maxn];
void init(int n)
{
this->n=n;
for(int i=; i<=n; i++)
{
G[i].clear();
vis[i]=;
d[i]=INF;
}
edge.clear();
}
void AddEdge(int u, int v, int d)
{
G[u].push_back(edge.size());
edge.push_back(Edge(u, v, d));
}
int dijkstra(int k)
{
memset(vis, , sizeof vis);
priority_queue<qnode> q;
d[]=;
q.push(qnode(, ));
while(!q.empty())
{
qnode x = q.top();
q.pop();
if(vis[x.u])
continue ;
vis[x.u]=true;
if(x.u == k)
return d[k] - ;
if(d[x.u] == INF) return -;
for(int i=; i<G[x.u].size(); i++)
{
Edge e=edge[G[x.u][i]];
int tep = d[e.u] + e.d + ;
if(d[e.v] > tep)
{
d[e.v] = tep;
q.push(qnode(e.v, d[e.v]));
}
}
}
return -;
}
} dij; int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
int maxn = ;
memset(v,,sizeof(v));
st = ;
ed = k;
repu(i,,n)
scanf("%d",&v[i]);
int m=;
getchar();
dij.init(N);
repu(i,,n)
{
getline(cin,s);
int l = s.length(),last = ;
m = ;
memset(t,,sizeof(t));
repu(j,,l)
{
if(s[j] == ' ')
{
string c = s.substr(last,j-last);
last = j;
stringstream ss1(c);
ss1 >> t[m];
m++;
}
}
string c = s.substr(last,l-last);
stringstream ss1(c);
ss1 >> t[m];
m++; // repu(j,0,m)
// cout<<t[j]<<" ";
// cout<<endl; repu(j,,m)
repu(p,j+,m)
{
dij.AddEdge(t[j], t[p], (t[p]-t[j])*v[i]);
dij.AddEdge(t[p], t[j], (t[p]-t[j])*v[i]);
}
}
if(k == )
{
printf("0\n");
continue;
}
int ans = dij.dijkstra(ed);
if(ans == -)
printf("IMPOSSIBLE\n");
else
printf("%d\n",ans);
}
return ;
}