codeforces 16 E 簡單機率DP

來源:互聯網
上載者:User

http://codeforces.com/problemset/problem/16/E

題意:有n條魚,每天恰好會有一對魚相遇,並且其中的一條會吃掉另一條,給你一個n*n的矩陣,a[i][j]表示i吃掉j的機率,題目保證a[j][i]=1-a[i][j],最後讓你求每條魚存活的機率

做法:

n比較小,所以可以考慮狀態壓縮,dp[state]表示當前還剩state狀態的魚的機率

顯然,每條魚都在時的機率為1,然後可以枚舉兩條還活著的魚進行轉移即可,即枚舉誰吃了誰。

水水更健康~~

#include<stdio.h>#include<string.h>#include<algorithm>#include<string>#include<cmath>using namespace std;double dp[1<<18];double a[18][18];int main(){int n;scanf("%d",&n);for(int i=0;i<n;i++){for(int j=0;j<n;j++){scanf("%lf",&a[i][j]);}}dp[(1<<n)-1]=1.0;for(int s=(1<<n)-1;s>=0;s--){double cnt=0;for(int i=0;i<n;i++)if(s&(1<<i)) cnt++;cnt=2.0/cnt/(cnt-1);//printf("cnt=%.2lf\n",cnt);for(int i=0;i<n;i++){if(s&(1<<i)){for(int j=i+1;j<n;j++){if(s&(1<<j)){//printf("s=%d %d %d\n",s,i,j);//printf("%lf %lf %lf\n",dp[s],cnt,a[i][j]);dp[s^(1<<j)]+=dp[s]*cnt*a[i][j];//printf("%.2lf\n",dp[s^(1<<j)]);dp[s^(1<<i)]+=dp[s]*cnt*a[j][i];}}}}}for(int i=0;i<n;i++)      printf("%lf ",dp[1<<i]);}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.