题目链接 http://acm.hust.edu.cn/vjudge/problem/22172
题目大意:有一栋100层的大楼(标号为0~99),
给n个电梯(不超过5个), 和要到的终点楼层
然后是每个电梯走一层楼所需的时间(电梯的速度)
然后给每个电梯所能到达的楼层(数量不定 1行对应1个电梯的)
然后每换一次电梯需要等待60秒,
问,最快能多快到达目标层数。
先存图 坐电梯从i楼到j楼的最小距离 mp[i][j] (INF表示从i不可达到j)
我是这样想的 如果当前值要更新
那么肯定是从其他电梯过来的 使得dis[i]变小
然后使得dis[v] > dis[i]+mp[i][v]
因为原本存的图 已经使得dis[v] = dis[i]+mp[i][v](在同一个电梯上来的)
因此判断的应该是 dis[v] > dis[i]+mp[i][v]+60
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int, int> P;
const int maxn = 1e5 + 5;
int mp[105][105];
int elv[6];
int dis[105], vis[105];
vector<int>a[105];
void init()
{
memset(mp, INF, sizeof(mp));
for (int i = 0; i < 100; i++)
a[i].clear();
}
void dijkstra(int s)
{
int i, j, v, mni;
for (i = 0; i < 100; i++){
dis[i] = mp[s][i];
vis[i] = 0;
}
dis[s] = 0;
vis[s] = 1;
for (i = 0; i < 100; i++){
mni = INF;
for (j = 0; j < 100; j++){
if (!vis[j] && mni > dis[j]){
mni = dis[j];
v = j;
}
}
if (mni == INF) break;
vis[v] = 1;
for (j = 0; j < 100; j++){
if (!vis[j] && dis[j] > dis[v] + mp[v][j] + 60)
dis[j] = dis[v] + mp[v][j] + 60;
}
}
}
int main(void)
{
// freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
int n, to, i, j, k, t, u, v, w;
while (cin >> n >> to){
init();
for (i = 1; i <= n; i++)
scanf("%d", &elv[i]);
for (i = 1; i <= n; i++){
while (1){
scanf("%d", &t);
a[i].push_back(t);
char ch = getchar();
if (ch == '\n')
break;
}
}
for (i = 1; i <= n; i++){
for (j = 0; j < a[i].size(); j++){
for (k = j+1; k < a[i].size(); k++){
u = a[i][j];
v = a[i][k];
w = (a[i][k] - a[i][j]) * elv[i];
if (mp[u][v] > w)
mp[u][v] = mp[v][u] = w;
}
}
}
dijkstra(0);
if (dis[to] == INF)
printf("IMPOSSIBLE\n");
else printf("%d\n", dis[to]);
}
return 0;
}