void referredCell::locallyMapEdgeList
(
    const labelList& points,
    const edgeList& sourceCellEdges
)
{
    edges_.setSize(sourceCellEdges.size());

    forAll(sourceCellEdges, sCE)
    {
        const edge& e(sourceCellEdges[sCE]);

        edges_[sCE].start() = findIndex(points, e.start());

        edges_[sCE].end() = findIndex(points, e.end());

        if
        (
            edges_[sCE].start() == -1
         || edges_[sCE].end() == -1
        )
        {
            FatalErrorIn("Foam::referredCell::locallyMapEdgeList")
                << "edgeList and points labelList for "
                << "referred cell do not match: "
                << nl << "points: " << points
                << nl << "egdes: " << sourceCellEdges
                << abort(FatalError);
        }
    }
}
Ejemplo n.º 2
0
bool hasAug(){
	queue<int> q;
	vi vis(V+1,0);
	P.assign(V+1,NULL);
	q.push(S);
	vis[S] = 1;
	while(!q.empty()){
		int u = q.front();q.pop();

		if(u == T){
			break;
		}
		for (int i = 0; i < G[u].size(); ++i) {
			Edge * e = G[u][i];
			int v = e->other(u);
			if(!vis[v] && e->resTo(v)>0){
				q.push(v);
				vis[v] = 1;
				P[v] = e;
			}
		}
	}

	return P[T] != NULL;
}
Ejemplo n.º 3
0
bool augmentingPath(){
	queue<int> q;
	parent.assign(MAX_V, NULL);
	visited.assign(MAX_V, 0);

	q.push(SRC);
	visited[SRC] = 1;

	while(!q.empty()){
		int u = q.front();
		q.pop();
		if(u == SINK)
			break;
		for (int i = 0; i < adj[u].size(); ++i) {
			Edge* e = adj[u][i];
			int v = e->other(u);
			if(!visited[v] && e->residualTo(v) > 0){
				visited[v] = 1;
				parent[v] = e;
				q.push(v);
			}
		}
	}
	return visited[SINK];
}
Ejemplo n.º 4
0
bool hasAugmentingPath(){

	queue<int> q;
	vi vist(t+1, 0);
	vist[s] = 1;
	q.push(s);
	p.assign(t+1, NULL);
//	printf("s' = %d\n", s);
	while(!q.empty()){
		int u = q.front();
		q.pop();
//		printf("u = /%d\n", u);
		if(u == t)break;

		for (int i = 0; i < G[u].size(); ++i) {
			Edge *e = G[u][i];
			int v = e->other(u);
			if(!vist[v] && e->retTo(v) > 0){
				q.push(v);
				vist[v] = 1;
				p[v] = e;
			}
		}
	}

	return p[t] != NULL;
}
int main(){
	while (cin >> n){
		if (n == 0)
			break;
		cin >> m;
		V = 50001 + n + 2;
		s = 0; t = 50001 + n;
		mf = total = 0;
		G.assign(V,edgeList());
		for (int i = 0; i < n; i++){
			cin >> c >> a >> b;
			for (int j = a; j < b; j++){
				addEdge(j, 50001 + i, 1);
				addEdge(s, j, m);
			}
			addEdge(50001 + i, t, c);
		}

		while (1) {
			vi dist(V, INF);
			dist[s] = 0;
			queue<int> q;
			q.push(s);
			P.assign(V, NULL);
			while (!q.empty()){
				int u = q.front(); q.pop();
				cout << "u: " << u <<endl;
				if (u == t)
					break;
				for (int j = 0; j < G[u].size(); j++){
					Edge * e = G[u][j];
					int v = e->other(u);
					if (e->cap > 0 && dist[v] == INF){
						dist[v] = dist[u] + 1, q.push(v), P[v] = e;
					}
				}
			}

			int min_res = INF;
			for(int v = t; v!= s;v = P[v]->other(v)){
				min_res = min(min_res,  P[v]->cap);
			}
			cout << "f: " << min_res << endl;
//			// Add flow to the residual graph
			for(int v = t; v!= s;v = P[v]->other(v)){
				G[P[v]->other(v)][v]->cap -= min_res;
			}

			if (min_res == 0)
				break;
			mf += min_res;
		}
		cout << mf << endl;
	}

	return 0;
}
Ejemplo n.º 6
0
void network::edgesMatching (edgeList &res, networkElementType edgeType)
{
    node::edgeDescriptor ea,ee;
    nodeIterator vi;

    for (vi = theNodes.begin(); vi != theNodes.end(); vi++)
    {
        ea = 0;
        ee = node::theNodes[*vi]->degree();
        for (; ea != ee; ea++)
            if (((edgeVirtual*)node::theNodes[*vi]->getEdge(ea)   )->getEdgeInfo().theEdgeType == edgeType)
                if (isInsideNetwork((node::theNodes[*vi])->getTarget(ea)))
                    res.push_back (make_pair(*vi,ea));

    }
}
/**
 * Returns true if there is an augmenting path
 * stores augmenting path in P
 */
bool bellmanFord(){

	vector<double> dist(N,INF);
	P.assign(N, NULL);
	dist[S] = 0;

	for (int i = 0; i < N-1; ++i) {
		for (int u = 0; u < N; ++u) {
			for (int k = 0; k < G[u].size(); ++k) {
				Edge * e = G[u][k];
				int v = e->other(u);
				// Relax edge
				if(e->resTo(v) > 0 && dist[v] > dist[u] + e->getCost(v)){
					dist[v] = dist[u] + e->getCost(v);
					// Parent of v is in edge 'e'
					P[v] = e;
				}
			}
		}
	}

	return P[T] != NULL;
}
Ejemplo n.º 8
0
bool Foam::polyMeshZipUpCells(polyMesh& mesh)
{
    if (polyMesh::debug)
    {
        Info<< "bool polyMeshZipUpCells(polyMesh& mesh) const: "
            << "zipping up topologically open cells" << endl;
    }

    // Algorithm:
    // Take the original mesh and visit all cells.  For every cell
    // calculate the edges of all faces on the cells.  A cell is
    // correctly topologically closed when all the edges are referenced
    // by exactly two faces.  If the edges are referenced only by a
    // single face, additional vertices need to be inserted into some
    // of the faces (topological closedness).  If an edge is
    // referenced by more that two faces, there is an error in
    // topological closedness.
    // Point insertion into the faces is done by attempting to create
    // closed loops and inserting the intermediate points into the
    // defining edge
    // Note:
    // The algorithm is recursive and changes the mesh faces in each
    // pass.  It is therefore essential to discard the addressing
    // after every pass.  The algorithm is completed when the mesh
    // stops changing.

    label nChangedFacesInMesh = 0;
    label nCycles = 0;

    labelHashSet problemCells;

    do
    {
        nChangedFacesInMesh = 0;

        const cellList& Cells = mesh.cells();
        const pointField& Points = mesh.points();

        faceList newFaces = mesh.faces();

        const faceList& oldFaces = mesh.faces();
        const labelListList& pFaces = mesh.pointFaces();

        forAll(Cells, cellI)
        {
            const labelList& curFaces = Cells[cellI];
            const edgeList cellEdges = Cells[cellI].edges(oldFaces);
            const labelList cellPoints = Cells[cellI].labels(oldFaces);

            // Find the edges used only once in the cell

            labelList edgeUsage(cellEdges.size(), 0);

            forAll(curFaces, faceI)
            {
                edgeList curFaceEdges = oldFaces[curFaces[faceI]].edges();

                forAll(curFaceEdges, faceEdgeI)
                {
                    const edge& curEdge = curFaceEdges[faceEdgeI];

                    forAll(cellEdges, cellEdgeI)
                    {
                        if (cellEdges[cellEdgeI] == curEdge)
                        {
                            edgeUsage[cellEdgeI]++;
                            break;
                        }
                    }
                }
            }

            edgeList singleEdges(cellEdges.size());
            label nSingleEdges = 0;

            forAll(edgeUsage, edgeI)
            {
                if (edgeUsage[edgeI] == 1)
                {
                    singleEdges[nSingleEdges] = cellEdges[edgeI];
                    nSingleEdges++;
                }
                else if (edgeUsage[edgeI] != 2)
                {
                    WarningIn("void polyMeshZipUpCells(polyMesh& mesh)")
                        << "edge " << cellEdges[edgeI] << " in cell " << cellI
                        << " used " << edgeUsage[edgeI] << " times. " << nl
                        << "Should be 1 or 2 - serious error "
                        << "in mesh structure. " << endl;

#                   ifdef DEBUG_ZIPUP
                    forAll(curFaces, faceI)
                    {
                        Info<< "face: " << oldFaces[curFaces[faceI]]
                            << endl;
                    }

                    Info<< "Cell edges: " << cellEdges << nl
                        << "Edge usage: " << edgeUsage << nl
                        << "Cell points: " << cellPoints << endl;

                    forAll(cellPoints, cpI)
                    {
                        Info<< "vertex create \"" << cellPoints[cpI]
                            << "\" coordinates "
                            << Points[cellPoints[cpI]] << endl;
                    }
#                   endif

                    // Gather the problem cell
                    problemCells.insert(cellI);
                }
            }

            // Check if the cell is already zipped up
            if (nSingleEdges == 0) continue;

            singleEdges.setSize(nSingleEdges);

#           ifdef DEBUG_ZIPUP
            Info<< "Cell " << cellI << endl;

            forAll(curFaces, faceI)
            {
                Info<< "face: " << oldFaces[curFaces[faceI]] << endl;
            }

            Info<< "Cell edges: " << cellEdges << nl
                << "Edge usage: " << edgeUsage << nl
                << "Single edges: " << singleEdges << nl
                << "Cell points: " << cellPoints << endl;

            forAll(cellPoints, cpI)
            {
                Info<< "vertex create \"" << cellPoints[cpI]
                    << "\" coordinates "
                    << points()[cellPoints[cpI]] << endl;
            }
#           endif

            // Loop through all single edges and mark the points they use
            // points marked twice are internal to edge; those marked more than
            // twice are corners

            labelList pointUsage(cellPoints.size(), 0);

            forAll(singleEdges, edgeI)
            {
                const edge& curEdge = singleEdges[edgeI];

                forAll(cellPoints, pointI)
                {
                    if
                    (
                        cellPoints[pointI] == curEdge.start()
                     || cellPoints[pointI] == curEdge.end()
                    )
                    {
                        pointUsage[pointI]++;
                    }
                }
            }

            boolList singleEdgeUsage(singleEdges.size(), false);

            // loop through all edges and eliminate the ones that are
            // blocked out
            forAll(singleEdges, edgeI)
            {
                bool blockedHead = false;
                bool blockedTail = false;

                label newEdgeStart = singleEdges[edgeI].start();
                label newEdgeEnd = singleEdges[edgeI].end();

                // check that the edge has not got all ends blocked
                forAll(cellPoints, pointI)
                {
                    if (cellPoints[pointI] == newEdgeStart)
                    {
                        if (pointUsage[pointI] > 2)
                        {
                            blockedHead = true;
                        }
                    }
                    else if (cellPoints[pointI] == newEdgeEnd)
                    {
                        if (pointUsage[pointI] > 2)
                        {
                            blockedTail = true;
                        }
                    }
                }

                if (blockedHead && blockedTail)
                {
                    // Eliminating edge singleEdges[edgeI] as blocked
                    singleEdgeUsage[edgeI] = true;
                }
            }