#A7. SPFA算法-填空

SPFA算法-填空

  1. int n, m;
  2. vector<pair<int, int>> a[100005];
  3. queue<pair<int, int>> q;
  4. int d[100005];
  5. bool v[100005]; // 无需填空注释
  6. int cnt[100005];
  7. bool spfa() {
  8. memset(d, 0x3f, sizeof(d));
  9. d[1] = {{ input(1) }}; // 填空1:起点初始化
  10. v[1] = {{ input(2) }}; // 填空2:标记起点状态
  11. q.push({{ input(3) }}); // 填空3:初始入队
  12. while (!q.empty()) {
  13. int u = q.front().first;
  14. q.pop();
  15. v[u] = 0;
  16. for (int i = 0; i < a[u].size(); i++) {
  17. int to = a[u][i].{{ input(4) }}; // 填空4:邻接节点
  18. int w = a[u][i].{{ input(5) }}; // 填空5:边权值
  19. if ({{ input(6) }}) { // 填空6:松弛条件
  20. d[to] = {{ input(7) }}; // 填空7:更新距离
  21. cnt[to] = cnt[u] + 1;
  22. if ({{ input(8) }}) // 填空8:负环判断
  23. return true;
  24. if (!v[to]) {
  25. v[to] = 1;
  26. q.push({{ input(9) }}); // 填空9:入队操作
  27. }
  28. }
  29. }
  30. }
  31. return {{ input(10) }}; // 填空10:最终返回
  32. }