UVA 690 PipelineScheduling 位元運算+dfs+剪枝

來源:互聯網
上載者:User

標籤:

一開始最容易想到間隔最多為n,但是結點還是太多了,需要最佳化。

剪枝1:預判一下並儲存下一個可以放的位置距離之前的距離。這樣可以減少很多判斷。

剪枝2:如果當前長度+剩下沒放的程式*最短間隔如果大於等於ans,那麼對答案沒有貢獻,可以剪去。

最佳化:佔用和不佔用兩種狀態,如果橫向來看可以壓縮為int,判斷時用上為運算。

此題掛在長度的枚舉上,我把長度為n給忽略了,實際上可以只有一部分長度為n。

#include<bits/stdc++.h>//using namespace std;//#define localconst int maxn = 20;const int N = 5;int tab[N];int ans;int ivs[maxn],SZ;void dfs(int d,int* pre,int len){    if(len + (10 - d)*ivs[0] >= ans ) return;     for(int i = 0; i < SZ; i++){        int iv  = ivs[i];        bool ok = true;        for(int j = 0; j < N; j++) {            if((pre[j]>>iv)&tab[j]) { ok = false; break;}        }        if(ok) {            if(d == 9) {                ans = std::min(ans,len+iv);                return ;            }            int now[N];            for(int j = 0; j < N; j++) {                now[j] = (pre[j] >> iv) | tab[j];            }            dfs(d+1,now,len+iv);        }    }}void preJudge(int n){    SZ = 0;    for(int iv = 1; iv <= n; iv++){        bool ok = true;        for(int i = 0; i < N; i++) {            if((tab[i]>>iv)&tab[i]) { ok = false; break;}        }        if(ok) ivs[SZ++] = iv;    }}int main(){#ifdef local    freopen("in.txt","r",stdin);   // freopen("out.txt","w",stdout);#endif // local    int n;    char G[maxn+1];    while(~scanf("%d",&n)&&n) {        memset(tab,0,sizeof(tab));        for(int i = 0; i < N; i++){            scanf("%s",G);            for(int j = 0; j < n; j++) if(G[j] == ‘X‘){                tab[i] |= 1<<j;            }        }        ans = n*10;        preJudge(n);        dfs(1,tab,n);        printf("%d\n",ans);    }    return 0;}

 

UVA 690 PipelineScheduling 位元運算+dfs+剪枝

聯繫我們

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