示例#1
0
void dijkstra(node& p)
{
  if ( !ok(p) || stop || isvisited(p) )
    return;

  // neighbors
  node a(p.x+1, p.y); // move right
  node b(p.x, p.y+1); // move down
  node c(p.x, p.y-1); // move up
  node D(p.x-1, p.y); // move left

  // tentative distances
  uint32_t d = getdist(p);
  uint32_t da = sum(d, getcost(a));
  uint32_t db = sum(d, getcost(b));
  uint32_t dc = sum(d, getcost(c));
  uint32_t dd = sum(d, getcost(D));

  // update distances
  if ( !isvisited(a) && da < getdist(a) ) setdist(a, da);
  if ( !isvisited(b) && db < getdist(b) ) setdist(b, db);
  if ( !isvisited(c) && dc < getdist(c) ) setdist(c, dc);
  if ( !isvisited(D) && dd < getdist(D) ) setdist(D, dd);

  // mark this node
  setvisited(p);

  // has all dest nodes been visited?
  if ( allvisited() ) {
    // target node is lower right
    final = getdist(node(xmax-1, ymax-1));

    stop = true;
    return;
  }
示例#2
0
void dijkstra(node& p)
{
  if ( !ok(p) || stop || isvisited(p) )
    return;

  // neighbors
  node a(p.x+1, p.y);
  node b(p.x, p.y+1);

  // tentative distances
  uint32_t d = getdist(p);
  uint32_t da = sum(d, getcost(a));
  uint32_t db = sum(d, getcost(b));

  // update distances
  if ( !isvisited(a) && da < getdist(a) ) setdist(a, da);
  if ( !isvisited(b) && db < getdist(b) ) setdist(b, db);

  // mark this node
  setvisited(p);

  // has dest node been visited?
  if ( isvisited(node(xmax-1, ymax-1)) ) {
    // ...
    uint32_t final = getdist(node(xmax-1, ymax-1));
    printf("Finished with distance %u\n", final);
    stop = true;
    return;
  }
示例#3
0
static void update(Dict_t * Q, Agnode_t * dest, Agnode_t * src, double len)
{
    double newlen = getdist(src) + len;
    double oldlen = getdist(dest);

    if (oldlen == 0) {		/* first time to see dest */
	setdist(dest, newlen);
	dtinsert(Q, dest);
    } else if (newlen < oldlen) {
	dtdelete(Q, dest);
	setdist(dest, newlen);
	dtinsert(Q, dest);
    }
}
示例#4
0
void dijkstra(Dict_t * Q, Agraph_t * G, Agnode_t * n)
{
    Agnode_t *u;
    Agedge_t *e;

    pre(G);
    setdist(n, 1);
    dtinsert(Q, n);
    while ((u = extract_min(Q))) {
	for (e = agfstedge(u); e; e = agnxtedge(e, u)) {
	    update(Q, e->node, u, getlength(e));
	}
    }
    post(G);
}