Description
Solution
根本就不是网络流哇
直接状压DP即可,用SPFA实现。
Code
/************************************************
* Au: Hany01
* Date: Jul 12th, 2018
* Prob: LOJ6009
* Inst: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
static int _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 1 << 20, maxm = 105;
int n, m, w[maxm], mustwa[maxm], mustac[maxm], cor[maxm], mis[maxm], ALL, dp[maxn], vis[maxn];
char s1[maxm], s2[maxm];
queue<int> q;
int main()
{
#ifdef hany01
File("loj6009");
#endif
n = read(), m = read();
For(j, 1, m) {
w[j] = read(), scanf("%s%s", s1, s2);
rep(i, n)
if (s1[i] == '+') mustwa[j] |= (1 << i);
else if (s1[i] == '-') mustac[j] |= (1 << i);
rep(i, n)
if (s2[i] == '+') mis[j] |= (1 << i);
else if (s2[i] == '-') cor[j] |= (1 << i);
}
Set(dp, 63), dp[0] = 0, q.push(0), ALL = (1 << n) - 1;
while (!q.empty()) {
int st = q.front(), nst; q.pop(), vis[st] = 0;
For(i, 1, m) if ((mustwa[i] & st) == 0 && (mustac[i] & st) == mustac[i]) {
nst = (st | cor[i]) & (ALL ^ mis[i]);
if (chkmin(dp[nst], dp[st] + w[i]) && !vis[nst]) vis[nst] = 1, q.push(nst);
}
}
if (dp[ALL] == 0x3f3f3f3f) puts("0"); else printf("%d\n", dp[ALL]);
return 0;
}
//杨柳东风树,青青夹御河。
// -- 王之涣《送别》