BZOJ 4078: [Wf2014]Metal Processing Plant

來源:互聯網
上載者:User

標籤:ted   app   str   iostream   ref   http   複雜   .com   sort   

4078: [Wf2014]Metal Processing PlantTime Limit: 100 Sec  Memory Limit: 128 MB
Submit: 86  Solved: 20
[Submit][Status][Discuss]Description

定義集合S的價值D(S)為:

 現在給你n個元素,並給出其中任意兩個元素之間的d(i,j)值,要你將這些元素劃分成兩個集合A、B。求min{D(A)+D(B)}。註:d(i,j)=d(j,i)。Input

輸入資料的第一行是一個整數n,代表元素個數。

之後n-1行描述的是d(i,j),這部分裡,第i行包含n-i個整數,第i行第j列的整數代表的是d(i,i+j)。Output

 輸出只有一行,一個整數,代表min{D(A)+D(B)}。

Sample Input5
4 5 0 2
1 3 7
2 0
4
Sample Output4
HINTSource

鳴謝qpswwww提供譯文

分析:

貌似TLE了兩個下午QAQ...

考慮最暴力的方法,枚舉$s1$和$s2$的最大值,然後判斷是否合法,判斷的時候就是一個$2-SAT$問題,然後發現貌似$s1$確定的時候$s2$具有單調性,可以二分,然而複雜度還是很大...

所以考慮剪枝(貌似也可以用什麼壓位演算法...然而不想學...),我們從大到小枚舉$s1$,然後把不合法的邊都連起來,發現如果不是一個二分圖了,那麼就可以停止枚舉了...

貌似玄學複雜度...感覺這個剪枝很機智...

代碼:
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<ctime>//by NeighThorn#define inf 0x3f3f3f3fusing namespace std; const int maxn=400+5,maxm=200000+5; int id;int lala,fa[maxn],co[maxn];int C,tim,top,mp[maxn],dfn[maxn],low[maxn],stk[maxm],instk[maxn];int n,s1,s2,ans,cnt,len,w[maxn][maxn],hd[maxn],to[maxm],nxt[maxm]; struct M{         int x,y,v;         inline M(){};         M(int a,int b,int c){        x=a,y=b,v=c;    }         friend bool operator < (M a,M b){        if(a.v!=b.v)return a.v>b.v;if(a.x!=b.x)return a.x>b.x;return a.y>b.y;    }     }e[maxm]; inline int read(void){    char ch=getchar();int x=0;    while(!(ch>=‘0‘&&ch<=‘9‘)) ch=getchar();    while(ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();    return x;   } inline void add(int x,int y){    to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++;} inline void tarjan(int x){    low[x]=dfn[x]=++tim;stk[++top]=x;instk[x]=1;    for(int i=hd[x];i!=-1;i=nxt[i]){        if(!dfn[to[i]])            tarjan(to[i]),low[x]=min(low[x],low[to[i]]);        else if(instk[to[i]])            low[x]=min(low[x],dfn[to[i]]);    }    if(dfn[x]==low[x]){        C++;int tmp;        do{            tmp=stk[top--],instk[tmp]=0;mp[tmp]=C;        }while(tmp!=x);    }} inline bool check(void){    cnt=C=tim=top=0;    memset(mp,0,sizeof(mp));    memset(hd,-1,sizeof(hd));    memset(dfn,0,sizeof(dfn));    memset(low,0,sizeof(low));    memset(instk,0,sizeof(instk));    for(int i=1;i<n;i++)        for(int j=i+1;j<=n;j++){            if(w[i][j]>s1)                add(i<<1,j<<1|1),add(j<<1,i<<1|1);            if(w[i][j]>s2)                add(i<<1|1,j<<1),add(j<<1|1,i<<1);        }    for(int i=2;i<=(n<<1|1);i++)        if(!dfn[i]) tarjan(i);    for(int i=1;i<=n;i++)        if(mp[i<<1]==mp[i<<1|1])            return false;    return true;} inline int find(int x){    if(fa[x]==x)        return x;    int fx=find(fa[x]);    co[x]^=co[fa[x]];    return fa[x]=fx;}inline int calc(int x){int l=0,r=x,res=-1;while(l<=r){        int mid=(l+r)>>1;s2=mid;        if(check())            r=mid-1,res=mid;        else            l=mid+1;    }    return res;} signed main(void){    n=read();ans=inf;    if(n<=2) return puts("0"),0;    for(int i=1;i<n;i++)        for(int j=i+1;j<=n;j++)            w[i][j]=w[j][i]=read(),e[++lala]=M(i,j,w[i][j]);    sort(e+1,e+lala+1);for(int i=1;i<=n;i++) fa[i]=i;    for(int i=1,x,y,fx,fy,res;i<=lala;i++){        s1=e[i].v;x=e[i].x,y=e[i].y,fx=find(x),fy=find(y);        if(fx!=fy){        res=calc(s1);        if(res!=-1) ans=min(ans,s1+res);        co[fx]=co[x]^co[y]^1;fa[fx]=fy;        }        else if(co[x]==co[y]){        res=calc(s1);        if(res!=-1) ans=min(ans,res+s1);break;        }    }    printf("%d\n",ans);    return 0;}

 

By NeighThorn

 

BZOJ 4078: [Wf2014]Metal Processing Plant

聯繫我們

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