Example #1
0
void AVL::breadFirstPrint(){
  TQueue<Node*> q;
  if(root_){
    q.enqueue(root_);
    while(!q.isEmpty()){
      Node* curr=q.front();
      q.dequeue();
      if(curr->left_)
        q.enqueue(curr->left_);
      if(curr->right_)
        q.enqueue(curr->right_);
      cout << curr->data_ << endl;
      
    }
  }
}
Example #2
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);
			}
		}
	}
}
Example #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;
}