-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathUVa 10801 - Lift Hopping.cpp
110 lines (102 loc) · 2.89 KB
/
UVa 10801 - Lift Hopping.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <queue>
#include <functional>
#include <cmath>
using namespace std;
static const int V = 100;
struct Elevator
{
int id; // Elevator id;
int from;
int to;
int cost;
// For std::priorty_queue.
bool operator>(const Elevator &e) const
{
return this->cost > e.cost;
}
};
int main()
{
int n, k;
while (cin >> n >> k)
{
vector<vector<Elevator> > elevators(V);
vector<int> time(n + 1);
for (int id = 1; id <= n; ++id)
cin >> time[id];
cin.ignore();
for (int id = 1; id <= n; ++id)
{
string s;
vector<int> floors;
int floor;
getline(cin, s);
stringstream ss(s);
while (ss >> floor)
floors.push_back(floor);
// Enumerate all pairs of different floors.
for (size_t j = 0; j < floors.size(); ++j)
for (size_t k = j + 1; k < floors.size(); ++k)
{
Elevator e;
e.id = id;
e.cost = 0;
// floors[j] to floors[k].
e.from = floors[j];
e.to = floors[k];
elevators[floors[j]].push_back(e);
// floors[k] to floors[j].
e.from = floors[k];
e.to = floors[j];
elevators[floors[k]].push_back(e);
}
}
// Dijkstra's algorithm using min-heap.
priority_queue<Elevator, vector<Elevator>, greater<Elevator> > pq;
vector<int> shortest(V, - 1);
vector<int> id(V, -1);
Elevator e;
e.from = 0;
e.to = 0;
e.cost = 0;
shortest[0] = 0;
pq.push(e);
while (!pq.empty())
{
Elevator u = pq.top();
pq.pop();
// Lazy deletion.
if (shortest[u.to] != u.cost)
continue;
// Expand the floor.
for (size_t i = 0; i < elevators[u.to].size(); ++i)
{
Elevator v = elevators[u.to][i];
int alt = shortest[v.from] + abs(v.to - v.from) * time[v.id];
// Switch to another elevator.
if (id[v.from] != -1
&& id[v.from] != v.id)
{
alt += 60;
}
if (shortest[v.to] == -1
|| alt < shortest[v.to])
{
shortest[v.to] = alt;
v.cost = alt;
id[v.to] = v.id;
pq.push(v);
}
}
}
if (shortest[k] == -1)
cout << "IMPOSSIBLE" << endl;
else
cout << shortest[k] << endl;
}
return 0;
}