Exemplo n.º 1
0
void DFS(const TGraph& g, int v, TLine* result) {
	result->resize(g.size());
	fill(result->begin(), result->end(), -1);
	TQueue q;
	(*result)[v] = 0;
	q.push(v);
	while (!q.empty()) {
		int now = q.front();
		q.pop();
		for (int i = 0; i < g[now].size(); ++i) {
			int next = g[now][i];
			if (-1 == (*result)[next]) {
				(*result)[next] = (*result)[now] + 1;
				q.push(next);
			}
		}
	}
}
Exemplo n.º 2
0
void TwoThreeTree::levelOrder() {
    if (root == NULL) return;

    // Use a tree to do levelOrder Traversal
    TQueue queue;
    queue.push(root);
    root->level = 0;

    cout << "Level Order Traversal:\n";

    int currentLevel = 0;
    while (!queue.isEmpty()) {
        const TNode *t = queue.top();
        
        if (t->level != currentLevel) {
            cout << endl;
            currentLevel = t->level;
        }
        
        // push t's children on queue, set their level nubmer
        t->print();
        if (!t->isLeaf) {
            int nextLevel = t->level + 1;
            t->left->level = nextLevel;
            queue.push(t->left);

            t->middle->level = nextLevel;
            queue.push(t->middle);

            if (t->rlow != -1) {
                t->right->level = nextLevel;
                queue.push(t->right);
            }
        }

        queue.pop();
    }
    cout << endl << endl;
}
Exemplo n.º 3
0
int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif

	int n = ReadInt();
	int m = ReadInt();

	int s = ReadInt() - 1;
	int f = ReadInt() - 1;

	TGraph g(n);
	for (int i = 0; i < m; ++i) {
		int begin = ReadInt() - 1;
		int end = ReadInt() - 1;
		int cost = ReadInt();

        double p = 1.0 - static_cast<double>(cost) / 100.0;
		g[begin].push_back( TNode(end, p) );
		g[end].push_back( TNode(begin, p) );
	}

    /*
    for (int i = 0; i < n; ++i) {
        shuffle(g[i].begin(), g[i].end(), default_random_engine());
    }
    */

    const TCost INF(n + 1000, -10.0);
    TCosts costs(n, INF);
    TIntegers parent(n, -1);
    parent[s] = -2;

    TQueue q;
    {
        const TCost cost(0, 1.0);
        q.push(s);
        costs[s] = cost;
    }

    while (!q.empty()) {
        const int now = q.front();
        q.pop();
        if (now == f) {
            break;
        }
        const TCost& nowCost = costs[now];
        for (TNodes::const_iterator toNode = g[now].begin(); toNode != g[now].end(); ++toNode) {
            const int next = toNode->_to;
            TCost nextCost(nowCost._iDist + 1, nowCost._fine*toNode->_p);
            if (-1 == parent[next] || ((nowCost._iDist + 1 == costs[next]._iDist) && (nowCost._fine*toNode->_p > costs[next]._fine))) {
                if (-1 == parent[next]) {
                    q.push(next);
                }
                costs[next] = nextCost;
                parent[next] = now;
            }
        }
    }

    TIntegers path;
    int now = f;
    while (-2 != now) {
        path.push_back(now);
        now = parent[now];
    }

    printf("%d %.12lf\n", path.size(), 1.0 - costs[f]._fine);
    for (TIntegers::const_reverse_iterator toPath = path.rbegin(); toPath != path.rend(); ++toPath) {
        printf("%d ", *toPath + 1);
    }
    printf("\n");

	return 0;
}