-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp10201 - Adventures in Moving - Part IV.cpp
88 lines (68 loc) · 1.77 KB
/
p10201 - Adventures in Moving - Part IV.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
#include <iostream>
#include <algorithm>
#include <climits>
#include <string>
#include <sstream>
#define MAX INT_MAX / 2
using namespace std;
/*
* dp[i][j] -> cost to s[i] where fuel is j
* = dp[i - 1][j + distance - fill] + c[i].price * fill
*/
typedef struct {
int distance;
int price;
} Station;
Station s[102];
int dp[102][201];
int main(void) {
ios::sync_with_stdio(false);
int case_num = 0;
cin >> case_num;
cin.ignore();
string buf;
getline(cin, buf);
for (int cases = 1; cases <= case_num; cases++) {
int t;
cin >> t;
cin.ignore();
int n = 1;
s[0].distance = 0;
s[0].price = 0;
while (getline(cin, buf)) {
if(!buf[0]) break;
stringstream ss(buf);
ss >> s[n].distance >> s[n].price;
n++;
}
// trick: set destination as max gas station
s[n].distance = t;
s[n].price = MAX;
// init array
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= 200; j++) {
dp[i][j] = MAX;
}
}
dp[0][100] = 0;
for (int i = 1; i <= n; i++) {
int d = s[i].distance - s[i - 1].distance;
for (int j = d; j <= 200; j++)
dp[i][j - d] = dp[i - 1][j];
for (int j = 1; j <= 200; j++)
dp[i][j] = min(dp[i][j], dp[i][j - 1] + s[i].price);
}
int ans = MAX;
for (int i = 100; i <= 200; i++)
ans = min(ans, dp[n][i]);
if (ans >= MAX)
cout << "Impossible" << endl;
else
cout << ans << endl;
// newline for next result
if (cases != case_num) {
cout << endl;
}
}
return 0;
}