Exemple #1
0
	void MinCost<FlowType, CostType>::Dijkstra(Node* start)
{
	assert(start->excess > 0);

	Node* i;
	Node* j;
	Arc* a;
	CostType d;
	Node* permanentNodes;

	int FLAG0 = ++ counter; // permanently labeled nodes
	int FLAG1 = ++ counter; // temporarily labeled nodes

	start->parent = NULL;
	start->flag = FLAG1;
	queue.Reset();
	queue.Add(start, 0);

	permanentNodes = NULL;

	while ( (i=queue.RemoveMin(d)) )
	{
		if (i->excess < 0)
		{
			FlowType delta = Augment(start, i);
			cost += delta*(d - i->pi + start->pi);
			for (i=permanentNodes; i; i=i->next_permanent) i->pi += d;
			break;
		}

		i->pi -= d;
		i->flag = FLAG0;
		i->next_permanent = permanentNodes;
		permanentNodes = i;

		for (a=i->firstNonsaturated; a; a=a->next)
		{
			j = a->head;
			if (j->flag == FLAG0) continue;
			d = a->GetRCost();
			if (j->flag == FLAG1)
			{
				if (d >= queue.GetKey(j)) continue;
				queue.DecreaseKey(j, d);
			}
			else
			{
				queue.Add(j, d);
				j->flag = FLAG1;
			}
			j->parent = a;
		}

	}
}
Exemple #2
0
void MinCost<FlowType, CostType>::TestOptimality() {
    Node* i;
    Arc* a;
    
    for (i = nodes; i < nodes + nodeNum; i++) {
        if (i->excess != 0) {
            assert(0);
        }
        for (a = i->firstSaturated; a; a = a->next) {
            if (a->r_cap != 0) {
                assert(0);
            }
        }
        for (a = i->firstNonsaturated; a; a = a->next) {
            CostType c = a->GetRCost();
            if (a->r_cap <= 0 || a->GetRCost() < -1e-5) {
                assert(0);
            }
        }
    }
}
Exemple #3
0
void MinCost<FlowType, CostType>::Init() {
    Node* i;
    Arc* a;
    
    for (a = arcs; a < arcs + 2 * edgeNum; a++) {
        if (a->r_cap > 0 && a->GetRCost() < 0)
            PushFlow(a, a->r_cap);
    }
    
    Node** lastActivePtr = &firstActive;
    for (i = nodes; i < nodes + nodeNum; i++) {
        if (i->excess > 0) {
            *lastActivePtr = i;
            lastActivePtr = &i->next;
        } else
            i->next = NULL;
    }
    *lastActivePtr = &nodes[nodeNum];
}