【最小割】【Dinic】bzoj3275 Number

來源:互聯網
上載者:User

標籤:blog   io   sp   for   on   div   log   ad   ef   

每個點拆點,分別向源/匯連a[i]的邊,滿足條件的相互連INF的邊,答案為sum-maxflow*2。

因為若有幾個點不能同時被選,我們要貪心地選擇其中和盡量大的部分,這可以由最小割來保證。

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespace std;#define INF 2147483647#define MAXN 6005#define MAXM 600301int v[MAXM],cap[MAXM],en,first[MAXN],next[MAXM];int d[MAXN],cur[MAXN],A[3005],sumv;queue<int>q;int n,S,T;void Init_Dinic(){memset(first,-1,sizeof(first)); en=0; S=0; T=(n<<1|1);}void AddEdge(const int &U,const int &V,const int &W){v[en]=V; cap[en]=W; next[en]=first[U]; first[U]=en++;v[en]=U; next[en]=first[V]; first[V]=en++;}bool bfs(){    memset(d,-1,sizeof(d)); q.push(S); d[S]=0;    while(!q.empty())      {        int U=q.front(); q.pop();        for(int i=first[U];i!=-1;i=next[i])          if(d[v[i]]==-1 && cap[i])            {              d[v[i]]=d[U]+1;              q.push(v[i]);            }      }    return d[T]!=-1;}int dfs(int U,int a){    if(U==T || !a) return a;    int Flow=0,f;    for(int &i=cur[U];i!=-1;i=next[i])      if(d[U]+1==d[v[i]] && (f=dfs(v[i],min(a,cap[i]))))        {          cap[i]-=f; cap[i^1]+=f;          Flow+=f; a-=f; if(!a) break;        }    if(!Flow) d[U]=-1;    return Flow;}int max_flow(){    int Flow=0,tmp=0;    while(bfs())      {        memcpy(cur,first,((n<<1)+5)*sizeof(int));        while(tmp=dfs(S,INF)) Flow+=tmp;      }    return Flow;}int gcd(int a,int b){return b==0?a:gcd(b,a%b);}int sqr(const int &x){return x*x;}bool check(const int &a,const int &b){int t=a*a+b*b;return (sqr((int)sqrt(t))==t&&(gcd(a,b)==1));}int main(){scanf("%d",&n);for(int i=1;i<=n;++i)  {  scanf("%d",&A[i]);  sumv+=A[i];  }Init_Dinic();for(int i=1;i<=n;++i)  for(int j=1;j<i;++j)    if(check(A[i],A[j]))      AddEdge(i,j+n,INF),  AddEdge(j,i+n,INF);for(int i=1;i<=n;++i)  AddEdge(S,i,A[i]),  AddEdge(i+n,T,A[i]);printf("%d\n",sumv-(max_flow()>>1));return 0;}

  

【最小割】【Dinic】bzoj3275 Number

聯繫我們

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