在环中,白书P333UVa11090(bellman—ford负圈判定,二分查找)

知识总结:
1.通过本题详细理解了bellman—ford算法与dijkstra算法的原理以及具体操作细节差异。
2.本题是bellman—ford判断负圈的特殊形式。因为要判负圈,所以初始化d[]数组时的操作与原模板略有不同:

 queue<int> Q;
    memset(inq, 0, sizeof(inq));
    memset(cnt, 0, sizeof(cnt));
    for(int i = 0; i < n; i++) { d[i] = 0; inq[0] = true; Q.push(i); }

直接把所有节点的d值初始化为0,然后全入队。没在负圈上的点几乎不会松弛或者松弛后收敛。只有在负圈上的点才会无法收敛。这么处理与原模板相比省去了不少麻烦,提高了效率。
3.二分查找算法应用(注意数据类型)。

if(!test(ub+1)) printf("No cycle found.\n");
    else {
      double L = 0, R = ub;
      while(R - L > 1e-3) {
        double M = L + (R-L)/2;
        if(test(M)) R = M; else L = M;
      }
// UVa11090 Going in Cycle!!
// Rujia Liu
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int INF = 1000000000;
const int maxn = 1000;

struct Edge {
  int from, to;
  double dist;
};

struct BellmanFord {
  int n, m;
  vector<Edge> edges;
  vector<int> G[maxn];
  bool inq[maxn];     // 是否在队列中
  double d[maxn];     // s到各个点的距离
  int p[maxn];        // 最短路中的上一条弧
  int cnt[maxn];      // 进队次数

  void init(int n) {
    this->n = n;
    for(int i = 0; i < n; i++) G[i].clear();
    edges.clear();
  }

  void AddEdge(int from, int to, double dist) {
    edges.push_back((Edge){from, to, dist});
    m = edges.size();
    G[from].push_back(m-1);
  }

  bool negativeCycle() {
    queue<int> Q;
    memset(inq, 0, sizeof(inq));
    memset(cnt, 0, sizeof(cnt));
    for(int i = 0; i < n; i++) { d[i] = 0; inq[0] = true; Q.push(i); }

    while(!Q.empty()) {
      int u = Q.front(); Q.pop();
      inq[u] = false;
      for(int i = 0; i < G[u].size(); i++) {
        Edge& e = edges[G[u][i]];
        if(d[e.to] > d[u] + e.dist) {
          d[e.to] = d[u] + e.dist;
          p[e.to] = G[u][i];
          if(!inq[e.to]) { Q.push(e.to); inq[e.to] = true; if(++cnt[e.to] > n) return true; }
        }
      }
    }
    return false;
  }
};

BellmanFord solver;

bool test(double x) {
  for(int i = 0; i < solver.m; i++)
    solver.edges[i].dist -= x;
  bool ret = solver.negativeCycle();
  for(int i = 0; i < solver.m; i++)
    solver.edges[i].dist += x;
  return ret;
}

int main() {
  int T;
  scanf("%d", &T);
  for(int kase = 1; kase <= T; kase++) {
    int n, m;
    scanf("%d%d", &n, &m);
    solver.init(n);
    int ub = 0;
    while(m--) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w); u--; v--; ub = max(ub, w);
      solver.AddEdge(u, v, w);
    }
    printf("Case #%d: ", kase);

    if(!test(ub+1)) printf("No cycle found.\n");
    else {
      double L = 0, R = ub;
      while(R - L > 1e-3) {
        double M = L + (R-L)/2;
        if(test(M)) R = M; else L = M;
      }
      printf("%.2lf\n", L);
    }
  }
  return 0;
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/NCUscienceZ/article/details/78595894
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞