记录编号 572319 评测结果 AAAAAAAA
题目名称 软件补丁 最终得分 100
用户昵称 GravatarHeSn 是否通过 通过
代码语言 C++ 运行时间 0.013 s
提交时间 2022-06-30 16:46:28 内存使用 19.31 MiB
显示代码纯文本
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=1<<22, M=105, INF=0x3f3f3f3f;
  4. struct pack {
  5. int b1, b2, f1, f2, t;
  6. }p[M];
  7. int n, m, d[N];
  8. bool v[N];
  9. char ch, s[22];
  10. bool SPFA() {
  11. memset(d, 0x3f, sizeof(d));
  12. queue<int> q;
  13. int x = (1 << n) - 1;
  14. d[x] = 0;
  15. q.push(x);
  16. while(!q.empty()) {
  17. int u = q.front();
  18. q.pop();
  19. v[u] = 0;
  20. for(int i = 1; i <= m; i ++) {
  21. if((u & p[i].b1) == p[i].b1 && (u & p[i].b2) == 0) {
  22. int nxt = ((u | p[i].f1) ^ p[i].f1) | p[i].f2;
  23. if(d[nxt] > d[u] + p[i].t) {
  24. d[nxt] = d[u] + p[i].t;
  25. if(v[nxt] == 0) {
  26. v[nxt] = 1;
  27. q.push(nxt);
  28. }
  29. }
  30. }
  31. }
  32. }
  33. return d[0] != INF;
  34. }
  35. int main() {
  36. freopen("bugs.in", "r", stdin);
  37. freopen("bugs.out", "w", stdout);
  38. cin >> n >> m;
  39. char s1[22], s2[22];
  40. for(int i = 1; i <= m; i ++) {
  41. cin >> p[i].t;
  42. scanf("%s%s", s1, s2);
  43. for(int j = 0; j < n; j ++) {
  44. if(s1[j] == '+') {
  45. p[i].b1 |= 1 << j;
  46. }
  47. else if(s1[j]=='-') {
  48. p[i].b2 |= 1 << j;
  49. }
  50. }
  51. for(int j = 0; j < n; j ++) {
  52. if(s2[j] == '+') {
  53. p[i].f2 |= 1 << j;
  54. }
  55. else if(s2[j]=='-') {
  56. p[i].f1 |= 1 << j;
  57. }
  58. }
  59. }
  60. if(SPFA()) {
  61. cout << d[0] << endl;
  62. }
  63. else {
  64. cout << -1 << endl;
  65. }
  66. return 0;
  67. }