BZOJ 1875 SDOI 2009 HH去散步 矩陣乘法最佳化DP

來源:互聯網
上載者:User

標籤:bzoj   矩陣乘法   dp   圖論   sdoi 2009   

題目大意:給出一張無向圖,求從A到B走k步(不能走回頭路)的方案數。(k <= 2^30)


思路:看到k的範圍就知道是矩陣乘法了。關鍵是不能走回頭路怎麼構造。正常的方法構造點的轉移不能避免這個問題,就用邊來構造。只要保證不經過自己^1的邊就可以保證不走回頭路了。


CODE:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 210#define MO 45989using namespace std; int total = 1; struct Matrix{    int num[MAX][MAX];         Matrix() {        memset(num,0,sizeof(num));    }    Matrix operator *(const Matrix &a)const {        Matrix re;        for(int i = 0; i <= total; ++i)            for(int j = 0; j <= total; ++j)                for(int k = 0; k <= total; ++k) {                    re.num[i][j] += num[i][k] * a.num[k][j];                    re.num[i][j] %= MO;                }        return re;    }}src,ans,temp; int points,edges,t;int S,T;int head[MAX];int next[MAX],aim[MAX]; inline void Add(int x,int y){    next[++total] = head[x];    aim[total] = y;    head[x] = total;} int stack[MAX],top; int main(){    cin >> points >> edges >> t >> S >> T;    ++S,++T;    for(int x,y,i = 1; i <= edges; ++i) {        scanf("%d%d",&x,&y);        x++,y++;        Add(x,y),Add(y,x);    }    for(int i = head[S]; i; i = next[i])        ans.num[0][i] = 1;    for(int i = 2; i <= total; ++i) {        int x = aim[i];        if(x == T)  stack[++top] = i;        for(int j = head[x]; j; j = next[j]) {            if(j == (i^1))  continue;            src.num[i][j] = 1;        }    }    for(int i = 0; i <= total; ++i)  temp.num[i][i] = 1;    --t;    while(t) {        if(t&1) temp = temp * src;        src = src * src;        t >>= 1;    }    ans = ans * temp;    int p = 0;    for(int i = 1; i <= top; ++i)        p = (p + ans.num[0][stack[i]]) % MO;    cout << p << endl;    return 0;}


BZOJ 1875 SDOI 2009 HH去散步 矩陣乘法最佳化DP

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.