Topic Links:[UVA 10801] Lift hopping[dijkstra][Building Map]
Test Instructions Analysis:
Starting from the 0 floor, there are a total of n elevators for you to reach the destination K floor. Each elevator to go up a layer to consume t[i] time, and elevator can only stop on a specific floor, transfer elevator to spend 60s of time, and, you can not use the stairs upstairs, can only take the elevator .... (hentai! Q: What is the fastest time to reach the floor k? Can not reach on the output-1.
Problem Solving Ideas:
This technique is embodied in the construction of the map, the figure is built, run with Dijkstra once.
The concrete plan is to use MP[I][J] to represent the minimum distance from floor I to floor J. This is the first way to build a map without considering a transfer.
For the transfer, we use it when the Dijkstra update node. Specific: D[y] = min (D[y], d[x] + mp[x][y] + 60);
Here is the specific reason for the update to add 60s: Here we default to the first elevator is a transfer, so add 60s. So regardless of the way to the end (each d[i] plus 60, together with no impact), or there is a transfer (originally should add 60s). are not affected.
So when the final result is calculated, minus 60 is the final result (because the default is to enter the elevator for the first time to calculate the transfer).
Personal experience:
It feels so amazing to build a map ~
The specific code is as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <sstream >using namespace Std;typedef long long ll;const int INF = 0x3f3f3f3f, MAXN = 111;int T[MAXN], MP[MAXN][MAXN], Floors[ma XN], Len, VIS[MAXN], D[MAXN], N, k;string s;void build (int node) {for (int i = 0; i < len; ++i) for (int j = i + 1; J < Len; ++J) {int &a = Floors[j], &b = floors[i]; int dis = (A-B) * T[node]; if (Dis < mp[b][a]) mp[a][b] = mp[b][a] = dis; }}void Dijkstra () {memset (Vis, 0, sizeof vis); memset (d, 0x3f, sizeof D); D[0] = 0; for (int i = 0; i <; ++i) {int x, mx = INF; for (int y = 0; y < ++y) if (!vis[y] && d[y] <= mx) mx = d[x = y]; VIS[X] = 1; for (int y = 0; y < ++y) d[y] = min (D[y], d[x] + mp[x][y] + 60); } if (d[k] = = INF) cout << "impossible\n"; else cout << Max (0, D[k]-() << ' \ n ';//Caution}int main () {while (~scanf ("%d%d", &n, &k)) {memset (MP, 0x3f, sizeof MP); for (int i = 0; i < n; ++i) scanf ("%d", &t[i]); GetChar (); for (int i = 0; i < n; ++i) {len = 0; int x; Getline (CIN, s); StringStream SS (s); while (ss >> x) {floors[len++] = x; } build (i); } Dijkstra (); } return 0;}
Copyright NOTICE: Welcome reprint (^ω^) ~ But reprint please indicate source: Http://blog.csdn.net/catglory? (????)
[UVA 10801] Lift hopping[dijkstra][Building Map]