[Luogu2973][USACO10HOL]赶小猪Driving Out the Piggi…

题目描述

The Cows have constructed a randomized stink bomb for the purpose of driving away the Piggies. The Piggy civilization consists of N (2 <= N <= 300) Piggy cities conveniently numbered 1..N connected by M (1 <= M <= 44,850) bidirectional roads specified by their distinct endpoints A_j and B_j (1 <= A_j <= N; 1 <= B_j <= N). Piggy city 1 is always connected to at least one other city.

The stink bomb is deployed in Piggy city 1. Each hour (including the first one), it has a P/Q (1 <= P <= 1,000,000; 1 <= Q <=

1,000,000; P <= Q) chance of polluting the city it occupies. If it does not go off, it chooses a random road out of the city and follows it until it reaches a new city. All roads out of a city are equally likely to be chosen.

Because of the random nature of the stink bomb, the Cows are wondering which cities are most likely to be polluted. Given a map of the Piggy civilization and the probability that the stink bomb detonates in a given hour, compute for each city the probability that it will be polluted.

For example, suppose that the Piggie civilization consists of two cities connected together and that the stink bomb, which starts in city 1, has a probability of 1/2 of detonating each time it enters a city:

1--2 We have the following possible paths for the stink bomb (where the last entry is the ending city):

1: 1 2: 1-2 3: 1-2-1

4: 1-2-1-2

5: 1-2-1-2-1

etc. To find the probability that the stink bomb ends at city 1, we can add up the probabilities of taking the 1st, 3rd, 5th, ... paths above (specifically, every odd-numbered path in the above list). The probability of taking path number k is exactly (1/2)^k - the bomb must not remain in its city for k - 1 turns (each time with a probability of 1 - 1/2 = 1/2) and then land in the last city

(probability 1/2).

So our probability of ending in city 1 is represented by the sum 1/2 + (1/2)^3 + (1/2)^5 + ... . When we sum these terms infinitely, we will end up with exactly 2/3 as our probability, approximately 0.666666667. This means the probability of landing in city 2 is 1/3, approximately 0.333333333.

Partial feedback will be provided for your first 50 submissions.

一个无向图,节点1有一个炸弹,在每个单位时间内,有p/q的概率在这个节点炸掉,有1-p/q的概率随机选择一条出去的路到其他的节点上。问最终炸弹在每个节点上爆炸的概率。

输入输出格式

输入格式:

* Line 1: Four space separated integers: N, M, P, and Q

* Lines 2..M+1: Line i+1 describes a road with two space separated integers: A_j and B_j

输出格式:

* Lines 1..N: On line i, print the probability that city i will be destroyed as a floating point number. An answer with an absolute error of at most 10^-6 will be accepted (note that you should output at least 6 decimal places for this to take effect).

输入输出样例

输入样例#1: 
2 1 1 2 
1 2 
输出样例#1: 
0.666666667 
0.333333333 




做完这道题,我感觉我离完全理解高斯消元又远了一步...
其实,这道题就是“游走”的简化版。
只要求出到每个点的期望次数,然后乘以p/q就是答案。
问题就是怎么样求期望次数。
设f[i]为到i点的期望次数, 于是f[i] = Σ(1/deg[v]) * f[v], v是i的所有相邻的点。
于是高斯消元解决,注意f[1]最后要加1,因为它一开始就经过。




#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
inline int read(){
    int res=0;char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch)){res=(res<<3)+(res<<1)+(ch^48);ch=getchar();}
    return res; 
}
#define eps 1e-13
int n, m, p, q;
int deg[305];
double a[305][305];
struct edge{
    int nxt, to;
}ed[44855*2];
int head[305], cnt;
inline void add(int x, int y)
{
    ed[++cnt] = (edge){head[x], y};
    head[x] = cnt;
}

inline void Gauss()
{
    for (int i = 1 ; i <= n ; i ++)
    {
        int pivot = i;
        for (int j = i + 1 ; j <= n ; j ++)
            if (fabs(a[j][i] - a[pivot][i]) <= eps) pivot = j;
        if (pivot != i)
            for (int j = 1 ; j <= n + 1 ; j ++)
                swap(a[i][j], a[pivot][j]);
        for (int j = n + 1 ; j >= i ; j --) a[i][j] /= a[i][i];
        for (int j = 1 ; j <= n ; j ++)
            if (i != j)
                for (int k = n + 1 ; k >= i ; k --)
                    a[j][k] -= a[j][i] * a[i][k];
    }
}

int main()
{
    n = read(), m = read(), p = read(), q = read();
    double k = (double) p / (double) q;
    for (int i = 1 ; i <= m ; i ++)
    {
        int x = read(), y = read();
        deg[x]++, deg[y]++;
        add(x, y), add(y, x);
    }
    for (int x = 1 ; x <= n ; x ++)
    {
        a[x][x] = 1;
        for (int i = head[x] ; i ; i = ed[i].nxt)
        {
            int to = ed[i].to;
            a[x][to] = (- 1.0 / deg[to]) * (1.0 - k);
        }
    }
    a[1][n+1] = 1;
    Gauss();
    for (int i = 1 ; i <= n ; i ++)
        printf("%.9lf\n", k * a[i][n+1]);
    return 0;
}

 


posted @ 2018-07-26 15:40  zZhBr  阅读(294)  评论(0编辑  收藏  举报