当前位置: 首页>>代码示例>>C++>>正文


C++ Digraph::oppositeNode方法代码示例

本文整理汇总了C++中Digraph::oppositeNode方法的典型用法代码示例。如果您正苦于以下问题:C++ Digraph::oppositeNode方法的具体用法?C++ Digraph::oppositeNode怎么用?C++ Digraph::oppositeNode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Digraph的用法示例。


在下文中一共展示了Digraph::oppositeNode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main


//.........这里部分代码省略.........
  if (!found) {cout<<"Could not find target node "<<target_node_name<<endl;exit(0);}
    
  kPaths_Instance T(g,vname,px,py,weight,source,target,k);
  
  //for (DiNodeIt v(g);v!=INVALID;++v){ if(v==T.V[0])vcolor[v]=RED; else vcolor[v]=BLUE;}
  //for (int i=1;i<T.nt;i++) vcolor[T.V[i]] = MAGENTA;
  //for (ArcIt e(g); e != INVALID; ++e) ecolor[e] = BLUE;
  //ViewListDigraph(g,vname,px,py,vcolor,ecolor,"Triangulated graph");
  
  // Generate the binary variables and the objective function
  // Add one binary variable for each edge and set its cost in the objective function
  for (Digraph::ArcIt e(g); e != INVALID; ++e) {
    char name[100];
    sprintf(name,"X_%s_%s",vname[g.source(e)].c_str(),vname[g.target(e)].c_str());
    x[e] = model.addVar(0.0, 1.0, weight[e],GRB_CONTINUOUS,name); }
  model.update(); // run update to use model inserted variables
  try {
    //if (time_limit >= 0) model.getEnv().set(GRB_DoubleParam_TimeLimit,time_limit);
    //model.getEnv().set(GRB_DoubleParam_ImproveStartTime,10); //try better sol. aft. 10s
    // if (cutoff > 0) model.getEnv().set(GRB_DoubleParam_Cutoff, cutoff );
    //model.write("model.lp"); system("cat model.lp");

    // Add degree constraint for each node (sum of solution edges incident to a node is 2)
    for (Digraph::NodeIt v(g); v!=INVALID; ++v) {
      GRBLinExpr exprin, exprout;
      for (Digraph::InArcIt e(g,v); e != INVALID; ++e) exprin += x[e];
      for (Digraph::OutArcIt e(g,v); e != INVALID; ++e) exprout += x[e];

      if (v==source)      model.addConstr(exprout - exprin == k );
      else if (v==target) model.addConstr(exprin - exprout == k );
      else                model.addConstr(exprin - exprout == 0 );
    }

    model.optimize();

    double soma=0.0;
    for (DiNodeIt v(g);v!=INVALID;++v) vcolor[v]=BLUE; // all nodes BLUE
    vcolor[source]=RED; // change the colors of the source node
    vcolor[target]=RED; // and the target node to RED
    for (Digraph::ArcIt e(g); e!=INVALID; ++e) {
      lpvar[e] = x[e].get(GRB_DoubleAttr_X);
      if (lpvar[e] > 1.0 - EPS) soma += weight[e];
    }
    cout << "kPaths Tree Value = " << soma << endl;

    //-----------------------------------------------------------------
    // By Lucas Prado Melo: coloring paths by bfs
    bool ok = true;
    for(int i=0; i < k && ok; i++) {
      queue<DiNode> q;
      Digraph::NodeMap<Arc> pre(g, INVALID);
      q.push(source);
      while (!q.empty()) {
        DiNode cur = q.front();
        q.pop();
        for(Digraph::OutArcIt e(g, cur); e!=INVALID; ++e) {
          DiNode nxt = g.runningNode(e);
          if (pre[nxt] == INVALID && ecolor[e] == NOCOLOR && lpvar[e] > 1.0 - EPS) {
            pre[nxt] = e;
            q.push(nxt);
          }
        }
      }
      if (pre[target] == INVALID) ok = false;
      else {
        DiNode x = target;
        while (x != source) {
          ecolor[pre[x]] = 3+i%6; // use colors 3(RED) to 8(CYAN), see myutils.h
          x = g.oppositeNode(x, pre[x]);
        }
      }
    }
    if (!ok) {cout << "Nao eh possivel encontrar os " << k << " caminhos!" << endl;}
    for(Digraph::ArcIt e(g); e!=INVALID; ++e) {
      if (lpvar[e] > 1.0 - EPS && ecolor[e] == NOCOLOR ) {
        cout << "Alguma(s) aresta(s) nao pertencem a qualquer caminho!" << endl;
        break;
      }
    }
    for(Digraph::ArcIt e(g); e!=INVALID; ++e) {
      if (lpvar[e] < 1.0-EPS && lpvar[e] > EPS) {
        ecolor[e] = GRAY;
      }
    }
    for(Digraph::ArcIt e(g); e!=INVALID; ++e) {
      if (lpvar[e] < 1.0-EPS && lpvar[e] > EPS) {
        cout << "Alguma(s) aresta(s) possuem valor fracionario! (marcadas com cor cinza claro)" << endl;
        break;
      }
    }
    //-----------------------------------------------------------------


    cout << "kPaths Tree Value = " << soma << endl;
    ViewListDigraph(g,vname,px,py,vcolor,ecolor,
	"minimum kPaths cost in graph with "+IntToString(T.nnodes)+
	" nodes and "+IntToString(k)+" paths: "+DoubleToString(soma));
  } catch (...) {cout << "Error during callback..." << endl; }
  return 0;
}
开发者ID:gabrielhidasy,项目名称:mc658,代码行数:101,代码来源:ex_kpaths.cpp


注:本文中的Digraph::oppositeNode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。