コード例 #1
0
void test() {
  /* Field testing: SPOJ LCA, SPOJ QTREE, TOJ 2241, Live Archive 2045, Live Archive 6140, UVa 10938, IPSC 2009 L */

  // srand(100);

  int ts = 100,
    maxn = 1000,
    maxq = 1000,
    minc = -1000,
    maxc = 1000;

  // ts = 100000;
  // maxn = 4;
  // minc = 1;
  // maxc = 4;
  // maxq = 5;

  for (int t = 0; t < ts; t++)
  {
    // printf("%d\n", t);

    int n = randint(1, maxn),
      qs = maxq;

    HLD hld1(n);
    HLD_naive hld2(n);

    union_find uf(n);
    vii edges;
    vvi adj(n);
    for (int i = 0; i < n - 1; i++)
    {
      while (true)
      {
        int a = randint(0, n - 1),
          b = randint(0, n - 1);

        if (uf.find(a) == uf.find(b))
          continue;

        uf.unite(a, b);
        // hld1.add_edge(a, b);
        // hld2.add_edge(a, b);
        edges.push_back(ii(a, b));
        edges.push_back(ii(b, a));
        adj[a].push_back(b);
        adj[b].push_back(a);
        hld1.add_edge(a,b);
        break;
      }
    }

    vector<int> parent(n, -1);

    stack<int> S;
    vector<bool> visited(n, false);
    int root = randint(0, n - 1);
    visited[root] = true;
    S.push(root);

    while (!S.empty())
    {
      int cur = S.top(); S.pop();
      for (int i = 0; i < size(adj[cur]); i++)
      {
        int nxt = adj[cur][i];
        if (!visited[nxt])
        {
          visited[nxt] = true;
          S.push(nxt);
          // hld1.add_edge(cur, nxt);
          hld2.add_edge(cur, nxt);
          parent[nxt] = cur;
        }
      }
    }

    hld1.build(root);

    // printf("\n\n");
    // for (int i = 0; i < n; i++)
    // {
    //     if (i != 0) printf(" ");
    //     printf("%d", parent[i]);
    // }

    // printf("\n");

    for (int q = 0; q < qs; q++)
    {
      // printf("\t%d\n", q);

      int type = randint(1, 3);
      // printf("\t\t%d\n", type);

      if (type == 1) // LCA
      {
        int u = randint(0, n - 1),
          v = randint(0, n - 1);

        int l1 = hld1.lca(u, v);
        int l2 = hld2.lca(u, v);

        // printf("lca %d %d = %d %d\n", u, v, l1, l2);

        assert_equal(l1, l2, true);
      }
      else if (type == 2) // QUERY
      {
        int u = randint(0, n - 1),
          v = randint(0, n - 1);

        int l1 = hld1.query(u, v);
        // printf("\t\t%d\n", l1);
        int l2 = hld2.query(u, v);
        // printf("\t\t%d\n", l2);

        // printf("query %d %d = %d %d\n", u, v, l1, l2);

        assert_equal(l1, l2, true);
      }
      else if (type == 3) // UPDATE
      {
        if (n > 1)
        {
          int i = randint(0, (int)size(edges) - 1);
          int u = edges[i].first,
            v = edges[i].second;

          int c = randint(minc, maxc);

          // printf("update %d %d %d\n", u, v, c);

          hld1.update_cost(u, v, c);
          hld2.update_cost(u, v, c);
        }
      }
    }
  }
}
コード例 #2
0
double	taxCache::adj() const
{
  return adj(0) + adj(1) + adj(2);
}
コード例 #3
0
double	taxCache::total(unsigned p) const
{
  return line(p) + freight(p) + adj(p);
}
コード例 #4
0
double	taxCache::total() const
{
  return line() + freight() + adj();
}
コード例 #5
0
ファイル: metrics.hpp プロジェクト: RedSunCMX/izenelib
 inline void set(double v) {
   adj(v);
   value = v;
   cumvalue += v;
 }
コード例 #6
0
ファイル: graphlab_naive.hpp プロジェクト: zmahdavi/grappa
  static void run_sync(GlobalAddress<Graph<V,E>> _g) {
    
    call_on_all_cores([=]{ g = _g; });
    
    ct = 0;
    // initialize GraphlabVertexProgram
    forall(g, [=](Vertex& v){
      v->prog = new VertexProg(v);
      if (prog(v).gather_edges(v)) ct++;
    });
    
    if (ct > 0) {
      forall(g, [=](Vertex& v){
        forall<async>(adj(g,v), [=,&v](Edge& e){
          // gather
          auto delta = prog(v).gather(v, e);

          call<async>(e.ga, [=](Vertex& ve){
            prog(ve).post_delta(delta);
          });
        });
      });
    }
    int iteration = 0;
    size_t active = V::total_active;
    while ( active > 0 && iteration < FLAGS_max_iterations )
        GRAPPA_TIME_REGION(iteration_time) {
      VLOG(1) << "iteration " << std::setw(3) << iteration;
      VLOG(1) << "  active: " << active;

      double t = walltime();
      
      forall(g, [=](Vertex& v){
        if (!v->active) return;
        v->deactivate();

        auto& p = prog(v);

        // apply
        p.apply(v, p.cache);

        v->active_minor_step = p.scatter_edges(v);
      });

      forall(g, [=](Vertex& v){
        if (v->active_minor_step) {
          v->active_minor_step = false;
          auto prog_copy = prog(v);
          // scatter
          forall<async>(adj(g,v), [=](Edge& e){
            _do_scatter(prog_copy, e, &VertexProg::scatter);
          });
        }
      });
  
  {
    symmetric_static std::ofstream myFile;
    //std::ofstream myFile;
    int pid = getpid();
    LOG(INFO) << "start writing file";
    std::string path = NaiveGraphlabEngine<G,VertexProg>::OutputPath;
      //on_all_cores( [pid, iteration, path] {
          std::ostringstream oss;
          oss << OutputPath << "-" << pid << "-" << mycore() << "-" << iteration;
          new (&myFile) std::ofstream(oss.str());
          if (!myFile.is_open()) exit(1);
        //});
      forall(g, [](VertexID i, Vertex& v){ 
          // LOG(INFO) << "id: " << i << " label: " << v->label;
          myFile << i << " ";
          for (int j = 0; j < NaiveGraphlabEngine<G,VertexProg>::Number_of_groups; j++) {
            myFile << prog(v).cache.label_count[j] << " ";
          }
          myFile << v->label << "\n";
        });
      //on_all_cores( [] {
          myFile.close();
        //});
    LOG(INFO) << "end writig file";
    }
      iteration++; 
      VLOG(1) << "  time:   " << walltime()-t;
      active = V::total_active;
    
    }
  
  

    forall(g, [](Vertex& v){ delete static_cast<VertexProg*>(v->prog); });
  }