-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_profile_dp.cpp
108 lines (84 loc) · 2.47 KB
/
connection_profile_dp.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
// BOJ 1144 싼 비용
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
bool is_valid(string &state) {
set<char> s(state.begin(), state.end());
s.erase('0');
return s.sz <= 1;
}
void normalize(string &state) {
unordered_map<char, char> mp;
char idx = '1';
for (char &c : state) {
if (c == '0')
continue;
if (mp.count(c)) {
c = mp[c];
continue;
}
mp[c] = idx;
c = idx++;
}
}
bool pass(int i, int j, string &state, int m) {
if (state[j] == '0')
return true;
for (int k = 0; k < m; k++)
if (k != j && state[k] == state[j])
return true;
return false;
}
int f(int i, int j, string state, vector<vector<int>> &v, vector<vector<unordered_map<string, int>>> &dp, int n, int m) {
if (i == n)
return is_valid(state) ? 0 : 1e9;
normalize(state);
auto it = dp[i][j].find(state);
if (it != dp[i][j].end())
return it->second;
int ii = i + (j == m - 1);
int jj = (j + 1) % m;
int ret = 1e9;
string original = state;
if (pass(i, j, state, m)) {
state[j] = '0';
ret = min(ret, f(ii, jj, state, v, dp, n, m));
state = original;
}
if (state[j] == '0' && (j == 0 || state[j - 1] == '0')) {
state[j] = '9';
ret = min(ret, v[i][j] + f(ii, jj, state, v, dp, n, m));
} else if (state[j] == '0' && j > 0) {
state[j] = state[j - 1];
ret = min(ret, v[i][j] + f(ii, jj, state, v, dp, n, m));
} else if (j == 0 || state[j - 1] == '0' || state[j - 1] == state[j])
ret = min(ret, v[i][j] + f(ii, jj, state, v, dp, n, m));
else {
char change = state[j - 1];
for (int k = 0; k < m; k++)
if (state[k] == change)
state[k] = state[j];
ret = min(ret, v[i][j] + f(ii, jj, state, v, dp, n, m));
}
if (is_valid(original))
ret = min(ret, 0);
return dp[i][j][original] = ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> v(10, vector<int>(10));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> v[i][j];
vector<vector<unordered_map<string, int>>> dp(10, vector<unordered_map<string, int>>(10));
cout << f(0, 0, string(m, '0'), v, dp, n, m);
}