zoukankan      html  css  js  c++  java
  • 费用流

    #define MAXN 2002
    #define MAXM 500001
    #define INF 2147483647
    int S,T,n,A,B;
    int en,u[MAXM],v[MAXM],first[MAXN],next[MAXM],cap[MAXM],cost[MAXM];//Next Array
    bool inq[MAXN];
    int d[MAXN]/*spfa*/,p[MAXN]/*spfa*/,a[MAXN]/*可改进量*/;
    queue<int>q;
    void Init_MCMF(){memset(first,-1,sizeof(first));en=0;S=0;T=(B<<1|1);}
    void AddEdge(const int &U,const int &V,const int &W,const int &C)
    {u[en]=U; v[en]=V; cap[en]=W; cost[en]=C; next[en]=first[U]; first[U]=en++;
    u[en]=V; v[en]=U; cost[en]=-C; next[en]=first[V]; first[V]=en++;}
    bool Spfa(int &Flow,int &Cost)
    {
        memset(d,0x7f,sizeof(d));
        memset(inq,0,sizeof(inq));
        d[S]=0; inq[S]=1; p[S]=0; a[S]=INF; q.push(S);
        while(!q.empty())
          {
            int U=q.front(); q.pop(); inq[U]=0;
            for(int i=first[U];i!=-1;i=next[i])
              if(cap[i] && d[v[i]]>d[U]+cost[i])
                {
                  d[v[i]]=d[U]+cost[i];
                  p[v[i]]=i;
                  a[v[i]]=min(a[U],cap[i]);
                  if(!inq[v[i]]) {q.push(v[i]); inq[v[i]]=1;}
                }
          }
        if(d[T]>2100000000) return 0;
        Flow+=a[T]; Cost+=d[T]*a[T]; int U=T;
        while(U!=S)
          {
            cap[p[U]]-=a[T]; cap[p[U]^1]+=a[T];
            U=u[p[U]];
          }
        return 1;
    }
    int Mincost()
    {
        int Flow=0,Cost=0;
        while(Spfa(Flow,Cost));
        printf("%d %d
    ",Flow>>1,(-Cost)>>1);
    }
    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;
        if(sqr((int)sqrt(t))!=t) return 0;
        if(gcd(b,(int)sqrt(t))==1) return 1;
        return 0;
    }
    int main()
    {
        scanf("%d%d",&A,&B); Init_MCMF();
        for(int i=A;i<=B;++i)
          for(int j=A;j<i;++j)
            if(check(i,j))
              {
                AddEdge(i,j+B,1,-i-j);
                AddEdge(j,i+B,1,-i-j);
              }
        for(int i=A;i<=B;++i)
          {
            AddEdge(S,i,1,0);
            AddEdge(i+B,T,1,0);
          }
        Mincost();
        return 0;
    }
  • 相关阅读:
    codeforces-1139 (div2)
    codeforces-1140 (div2)
    codeforces-1141 (div3)
    第四届CCCC团体程序设计天梯赛 后记
    BZOJ 3674 可持久化并查集
    你能回答这些问题吗
    [JSOI2008]最大数
    关路灯
    愤怒的小鸟
    推销员
  • 原文地址:https://www.cnblogs.com/zxhl/p/5069089.html
Copyright © 2011-2022 走看看