-
Notifications
You must be signed in to change notification settings - Fork 1
/
UVA_11284.cpp
72 lines (61 loc) · 1.69 KB
/
UVA_11284.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
#include <bits/stdc++.h>
#define getbit(mask, idx) ((mask >> (idx)) & 1)
using namespace std;
const int INF = 1E9;
int dist[55][55];
int dp[55][1 << 15];
int amazonDiff[15];
int shops[15];
int N, M, k;
int finalState;
int TSP (int here, int state) {
if(state == finalState) return dist[here][0];
int & ret = dp[here][state];
if(ret != -1) return ret;
ret = INF;
for(int i = 0; i < k; ++i) {
if(getbit(state, i)) continue;
int nextState = state | (1 << i);
ret = min(ret, min(amazonDiff[i] + TSP(here, nextState), dist[here][shops[i]] + TSP(shops[i], nextState)));
}
return ret;
}
int main() {
#ifndef Home
freopen ("input.txt", "r", stdin);
#endif
int testCases;
scanf("%d", &testCases);
while(testCases--) {
scanf("%d %d", &N, &M);
memset(dp, -1, sizeof dp);
for(int i = 0; i <= N; ++i)
for(int j = 0; j <= N; ++j)
dist[i][j] = (i == j) ? 0 : INF;
int from, to, dollars, cents;
char dot;
for(int i = 0; i < M; ++i) {
scanf("%d %d %d %c %d", &from, &to, &dollars, &dot, ¢s);
dist[to][from] = dist[from][to] = min(dist[from][to], dollars*100 + cents);
}
for(int i = 0; i <= N; ++i) {
for(int u = 0; u <= N; ++u) {
for(int v = 0; v <= N; ++v) {
dist[u][v] = min(dist[u][v], dist[u][i] + dist[i][v]);
}
}
}
scanf("%d", &k);
int amazonTotal = 0;
for(int i = 0; i < k; ++i) {
scanf("%d %d %c %d", &shops[i], &dollars, &dot, ¢s);
amazonDiff[i] = dollars*100 + cents;
amazonTotal += amazonDiff[i];
}
finalState = (1 << k) - 1;
int answer = amazonTotal - TSP(0, 0);
if(answer > 0) printf("Daniel can save $%d.%02d\n", answer/100, answer%100);
else printf("Don't leave the house\n");
}
return 0;
}