-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAnother_Shortest_Paths_Problem.cpp
More file actions
71 lines (65 loc) · 1.33 KB
/
Another_Shortest_Paths_Problem.cpp
File metadata and controls
71 lines (65 loc) · 1.33 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
//jai bholenath
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define float double
#define pb push_back
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define endl "\n"
#define inf 3e18
const int N = 1e5 + 5;
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
char dir[] = {'U', 'R', 'D', 'L'};
void solve()
{
int n, m, x, y;
cin >> n >> m >> x >> y;
if (n > m)
swap(n, m);
int ans = 0;
ans = min((m + n - 2) * x, ((n - 1) * y + (m - n) * x));
if (n == 1 || (n == m))
{
cout << ans;
return;
}
else
{
ans = min((n - 1) * y, (2 * n - 2) * x);
m -= n;
// if (m == 1)
// {
// ans += x;
// }
// else
// {
// // cout << m << endl;
if (m % 2 == 0)
{
ans += min(m * x, m * y);
}
else
ans += min((m - 1) * y + x, m * x);
// }
cout << ans << endl;
}
return;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q = 1;
cin >> q;
for (int i = 0; i < q; i++)
{
solve();
// cout << "\n";
}
return 0;
}