Beispiel #1
0
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;
}
void test_bellmanford(){
  Dijs solver;
  BellmanFord bell;
  generate_graph();
  int n = 20;
  while(n--){
    int s = rand()%g.size();
    int t = rand()%g.size();
    int r1 = bell.bellmanford(s,t,g);
    int r2 = solver.dijs(s,t,g);

    cout<<"Bellmanford="<<r1<<" HeapDijs="<<r2<<' '<<(r1==r2?"TRUE":"FALSE")<<endl;
  }
	      
}
Beispiel #3
0
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;
}
Beispiel #4
0
bool test(int m)
{
    for (int i=0;i<solver.m;i++)
        solver.edges[i].dist-=m;
    bool ret=solver.negativeCycle();
    for (int i=0;i<solver.m;i++)
        solver.edges[i].dist+=m;
    return ret;
}
Beispiel #5
0
main()
{
    int V,E;
    while (scanf("%d%d",&V,&E)==2)
    {
        int u,v,w;
        int max_w=0;
        solver.init(V);
        for (int i=0;i<E;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            u--,v--;
            solver.AddEdge(u,v,w);
            max_w=max(w,max_w);
        }
        if (test(max_w+1))
        {
            printf("Infinite\n");
            continue;
        }
        else if (!test(1))
        {
            printf("No Solution\n");
            continue;
        }
        int L=0,R=max_w;
        while (L<=R)
        {
            int mid=(L+R)/2;
            if (!test(mid)) R=mid-1;
            else L=mid+1;
        }
        if (!test(L)) L--;
        printf("%d\n",L);
    }
    return 0;
}