-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliftHopping_UVA10801OJ.cpp
113 lines (91 loc) · 2.61 KB
/
liftHopping_UVA10801OJ.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
111
112
113
#include <bits/stdc++.h>
using namespace std;
int _abs(int x)
{
if (x > 0) return x;
else return -x;
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
while (true)
{
int n = 0, k = 0; cin >> n >> k;
if (n + k == 0) break;
vector<int> t(n + 1, 0);
for (int i = 1; i <= n; ++i)
{
cin >> t[i];
}
string in; getline(cin, in);
vector<vector<bool>> g(n + 1, vector<bool>(100, false));
vector<vector<int>> a(n + 1);
for (int i = 1; i <= n; ++i)
{
getline(cin, in);
int size = in.size();
string temp = "";
for (int j = 0; j <= size; ++j)
{
if (in[j] == ' ' || j == size)
{
if (temp == "") continue;
int num = stoi(temp);
a[i].push_back(num);
g[i][num] = true;
temp = "";
}
else
{
temp += in[j];
}
}
}
vector<int> dp(100, INT_MAX / 2);
for (int i = 1; i <= n; ++i)
{
if (g[i][0] == false) continue;
int sizeI = a[i].size();
for (int q = 0; q <= sizeI - 1; ++q)
{
int f = a[i][q];
dp[f] = min(dp[f], f * t[i]);
}
}
for (int c = 1; c <= n; ++c)
{
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
int sizeI = a[i].size();
for (int p = 0; p <= sizeI - 1; ++p)
{
int fl = a[i][p];
if (g[j][fl] == false) continue;
for (int q = 0; q <= sizeI - 1; ++q)
{
int f = a[i][q];
if (fl == 25 && f == 30)
{
for (int __s = 0; __s == 0; ++__s);
}
dp[f] = min(dp[f], dp[fl] + _abs(f - fl) * t[i] + 60);
}
}
}
}
}
if (dp[k] == INT_MAX / 2)
{
cout << "IMPOSSIBLE\n";
}
else
{
cout << dp[k] << '\n';
}
}
cout.flush();
return 0;
}