题目链接:https://vjudge.net/problem/UVALive-7015
题意:n个电梯,每个电梯只能在特定的楼层停,若不换乘电梯,则从楼层x到楼层y所花费时间为abs(x-y),在某一楼层换乘电梯不消耗时间。求从当前所在楼层到目的楼层所需最小时间。
思路:输入中给的同一电梯相邻楼层建边,floyd求一次最短路,然后直接输出即可。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<sstream>
#include<deque>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 2000 + 20;
const int maxt = 300 + 10;
const int mod = 10;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int Dis[] = {-1, 1, -5, 5};
const int inf = 0x3f3f3f3f;
const int MOD = 1000;
int n, m, k;
int dis[maxn][maxn];
int num[maxn];
int maxnnum;
int floyd(){
for(int k = 0; k <= 150; ++k){
for(int i = 0; i <= 150; ++i){
for(int j = 0; j <= 150; ++j){
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
}
int main(){
int a, b;
while(~scanf("%d%d%d", &n, &a, &b)){
if(!n && !a && !b) break;
for(int i = 0; i <= 150; ++i){
dis[i][i] = 0;
for(int j = i + 1; j <= 150; ++j){
dis[i][j] = dis[j][i] = inf;
}
}
memset(num, 0, sizeof num);
int x, u, v;
for(int i = 0; i < n; ++i){
scanf("%d", &x);
scanf("%d", &num[0]);
for(int j = 1; j < x; ++j){
scanf("%d", &num[j]);
dis[num[j - 1]][num[j]] = dis[num[j]][num[j - 1]] = abs(num[j] - num[j - 1]);
}
}
floyd();
printf("%d\n", dis[a][b]);
}
return 0;
}