这个题很有意思啊。。。
其实是道最大流板子题,只连byx会赢的边,S向byx连,另一个连T。。。
注意有长者时连的边加上同方mogician的个数。。。
还要注意mogician可以无限续命,也就是他使长者+1s,自己不会-1s 那太棒了
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
# define Copy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1010), __(1e6 + 10), INF(2147483647);
IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int n, m, w[6][6], lif[2][_], _1a, _1b;
int g[__], fst[_], nxt[__], to[__], cnt, S, T, lev[_], cur[_], max_flow;
queue <int> Q;
map <string, int> M;
string name[2][_];
IL void Add(RG int u, RG int v, RG int f, RG int _f){
g[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;
g[cnt] = _f; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;
}
IL int Dfs(RG int u, RG int maxf){
if(u == T) return maxf;
RG int ret = 0;
for(RG int &e = cur[u]; e != -1; e = nxt[e]){
if(lev[to[e]] != lev[u] + 1 || !g[e]) continue;
RG int f = Dfs(to[e], min(g[e], maxf - ret));
ret += f; g[e ^ 1] += f; g[e] -= f;
if(ret == maxf) break;
}
return ret;
}
IL bool Bfs(){
Fill(lev, 0); lev[S] = 1; Q.push(S);
while(!Q.empty()){
RG int u = Q.front(); Q.pop();
for(RG int e = fst[u]; e != -1; e = nxt[e]){
if(lev[to[e]] || !g[e]) continue;
lev[to[e]] = lev[u] + 1;
Q.push(to[e]);
}
}
return lev[T];
}
int main(RG int argc, RG char* argv[]){
M["W"] = 1; M["HK"] = 2; M["J"] = 3; M["E"] = 4; M["YYY"] = 5;
w[1][4] = w[1][5] = w[2][1] = w[2][4] = w[3][1] = w[3][2] = w[4][3] = w[4][5] = w[5][2] = w[5][3] = 1;
n = Read(); m = Read(); Fill(fst, -1); T = n + n + 1;
for(RG int i = 1; i <= n; ++i){ cin >> name[0][i]; if(M[name[0][i]] == 5) ++_1a; }
for(RG int i = 1; i <= n; ++i){ cin >> name[1][i]; if(M[name[1][i]] == 5) ++_1b; }
for(RG int i = 1; i <= n; ++i) lif[0][i] = Read();
for(RG int i = 1; i <= n; ++i) lif[1][i] = Read();
for(RG int i = 1; i <= n; ++i){
RG int u1 = M[name[0][i]], u2 = M[name[1][i]];
Add(S, i, lif[0][i] + (u1 == 3 ? _1a : 0), 0); Add(i + n, T, lif[1][i] + (u2 == 3 ? _1b : 0), 0);
for(RG int j = 1; j <= n; ++j){
RG int v = M[name[1][j]];
if(!w[u1][v]) continue;
Add(i, j + n, 1, 0);
}
}
while(Bfs()) Copy(cur, fst), max_flow += Dfs(S, INF);
printf("%d
", min(m, max_flow));
return 0;
}