|
|
вернуться в форумI got stuck in test 4, any idea? #include <iostream> #include <vector> #include <cstring> #include <queue> using namespace std; int N, M, S, T, A, B, P; int total = 0; struct Edge { int to, next; double prob; } edges[100010]; int head[100010]; int parent[100010], dist[100010]; double prob[100010]; void addEdge(int start, int end, int p) { edges[total].to = end; edges[total].next = head[start]; edges[total].prob = 1.0 - (double)p / 100.0; head[start] = total++; } // The most tricky part is the probability, we are looking for the probability // of meeting Kraken in any route so it should be P(A|B), which equals to // P(A) + P(B) - P(AB) int main() { memset(head, -1, sizeof head); memset(parent, -1, sizeof parent); memset(dist, -1, sizeof dist); cin >> N >> M >> S >> T; for (int i = 0; i <= N; i ++) prob[i] = 0.0; for (int i = 1; i <= M; i ++) { cin >> A >> B >> P; addEdge(A, B, P); addEdge(B, A, P); } queue<int> q; q.push(S); dist[S] = 0 ; prob[S] = 1.0; while (!q.empty()) { int u = q.front(); q.pop(); for (int idx = head[u]; idx != -1; idx = edges[idx].next) { int v = edges[idx].to; double p = edges[idx].prob; if (dist[v] == -1 || (dist[v] == dist[u] + 1 && prob[v] < prob[u] * p)) { if (dist[v] == -1) { q.push(v); } dist[v] = dist[u] + 1; prob[v] = prob[u] * p; parent[v] = u; } } } vector<int> path; for (int i = T; i != S; i = parent[i]) { path.push_back(i); } path.push_back(S); cout << path.size() << " "; printf("%.7f\n", 1.0 - prob[T]); for (int i = path.size() - 1; i >= 1; i --) cout << path[i] << " "; cout << path[0] << endl; return 0; } |
|
|