void OutputQueue(TQueue queue) { typename TQueue::key_map propertyMap = queue.keys(); // Read out the queue, saving the objects in the order they were in the queue while(!queue.empty()) { typename TQueue::value_type queuedObject = queue.top(); std::cout << "queuedObject: " << queuedObject << " "; typename boost::property_traits<typename TQueue::key_map>::value_type value = get(propertyMap, queuedObject); std::cout << " value: " << value << std::endl; queue.pop(); } }
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); } } } }
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; }