-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.cpp
More file actions
132 lines (106 loc) · 2.71 KB
/
D.cpp
File metadata and controls
132 lines (106 loc) · 2.71 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include<bits/stdc++.h>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i,a,b) for(int i=a;i<=b;i++)
#define g(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout<<"Case #"<<x<<": "
#define op1(x) cout<<"Scenario #"<<x<<": "
#define endl "\n"
using namespace std;
const ll mod = 1000000007;
const ll INF = 2000000000000000000;//LLONG_MAX;
const ll NEGINF = LLONG_MIN;
const int N = 18;
const ll MAXN = 1000001;
ll xx, yy;
ll c1, c2, c3, c4, c5, c6;
ll foo(ll x, ll y, ll mask)
{
if (x == xx and y == yy) return 0;
ll res = INF;
ll a = INF, b = INF, c = INF, d = INF, e = INF, ff = INF;
ll aa = INF, aaa = INF, dd = INF, ddd = INF;
if (xx >= x and yy >= y and (mask & (1ll << 0)) == 0)
{
a = c1 * min(xx - x, yy - y) + foo(x + min(xx - x, yy - y), y + min(xx - x, yy - y), mask | (1ll << 0));
res = min(res, a);
}
if (yy >= y and (mask & (1ll << 0)) == 0)
{
aa = c1 * (yy - y) + foo(x + (yy - y), y + (yy - y), mask | (1ll << 0));
res = min(res, aa);
}
if (xx >= x and (mask & (1ll << 0)) == 0)
{
aaa = c1 * (xx - x) + foo(x + (xx - x), y + (xx - x), mask | (1ll << 0));
res = min(res, aaa);
}
if (yy >= y and (mask & (1ll << 1)) == 0)
{
b = c2 * (yy - y) + foo(x, yy, mask | (1ll << 1));
res = min(res, b);
}
if (xx <= x and (mask & (1ll << 2)) == 0)
{
c = c3 * (x - xx) + foo(xx, y, mask | (1ll << 2));
res = min(res, c);
}
if (xx <= x and yy <= y and (mask & (1ll << 3)) == 0 )
{
d = c4 * min(x - xx, y - yy) + foo(x - min(x - xx, y - yy), y - min(x - xx, y - yy), mask | (1ll << 3));
res = min(res, d);
}
if (xx <= x and (mask & (1ll << 3)) == 0 )
{
dd = c4 * (x - xx) + foo(x - (x - xx), y - (x - xx), mask | (1ll << 3));
res = min(res, dd);
}
if (yy <= y and (mask & (1ll << 3)) == 0 )
{
ddd = c4 * (y - yy) + foo(x - (y - yy), y - ( y - yy), mask | (1ll << 3));
res = min(res, ddd);
}
if (yy <= y and (mask & (1ll << 4)) == 0)
{
e = c5 * (y - yy) + foo(x, yy, mask | (1ll << 4));
res = min(res, e);
}
if (xx >= x and (mask & (1ll << 5)) == 0)
{
ff = c6 * (xx - x) + foo(xx, y, mask | (1ll << 5) );
res = min(res, ff);
}
//res = min({res, a, b, c, d, e, ff, aa, dd, aaa, ddd});
return res;
}
void solve(int t)
{
cin >> xx >> yy;
cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6;
//cout << INF << endl;
cout << foo(0, 0, 0) << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++)
{
solve(i);
}
}