From c0d8b1dd6379e5833b904cb331b416a875a34dbc Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 13 Sep 2022 20:47:21 +0800 Subject: [PATCH] =?UTF-8?q?P2761=20=E8=BD=AF=E4=BB=B6=E8=A1=A5=E4=B8=81?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/86560789 --- Luogu/P2761/P2761.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Luogu/P2761/P2761.cpp diff --git a/Luogu/P2761/P2761.cpp b/Luogu/P2761/P2761.cpp new file mode 100644 index 00000000..4a996b82 --- /dev/null +++ b/Luogu/P2761/P2761.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 22, + M = 105; +const int INF = 0x3f3f3f3f; + +int n, m; +std::array a, b1, b2, f1, f2; +std::array dist; +std::array vis; + +bool check(int s, int i) { + if ((b1[i] | s) != s) return false; + if (b2[i] & s) return false; + + return true; +} + +void spfa(int s) { + std::fill(dist.begin(), dist.end(), INF); + + std::queue q; + q.push(s); + dist[s] = 0; + vis[s] = true; + + while (!q.empty()) { + int u = q.front(); + q.pop(); + + for (int i = 1; i <= m; i++) { + if (!check(u, i)) continue; + + int v = u ^ (u & f1[i]) | f2[i]; + + if (dist[u] + a[i] < dist[v]) { + dist[v] = dist[u] + a[i]; + + if (!vis[v]) { + q.push(v); + vis[v] = true; + } + } + } + + vis[u] = false; + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1; i <= m; i++) { + std::string b, f; + + cin >> a[i] >> b >> f; + + for (int j = 0; j < b.size(); j++) { + if (b[j] == '+') { + b1[i] = b1[i] | 1 << j; + } else if (b[j] == '-') { + b2[i] = b2[i] | 1 << j; + } + } + + for (int j = 0; j < f.size(); j++) { + if (f[j] == '-') { + f1[i] = f1[i] | 1 << j; + } else if (f[j] == '+') { + f2[i] = f2[i] | 1 << j; + } + } + } + + spfa((1 << n) - 1); + + cout << (dist[0] == INF ? 0 : dist[0]) << endl; + + return 0; +}