zoukankan      html  css  js  c++  java
  • #9 //[SDOI2017]新生舞会

    题解:

    分数规划+费用流

    常数巨大开o2加inline加register还是不行

    我也不知道为什么

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int N=5e4;
    const int N1=2e3;
    #define INF 1e9
    #define INF2 1e4
    #define eps 1e-7
    #define rg register
    #define IL inline
    int pre[N],aa[N],s,t,n,m;
    double dis[N];
    bool vis[300];
    int l,p1[N1][N1],p2[N1][N1],head[10000];
    struct re{
        int a,b,c,flow,from;
        double cost;
    }a[N];
    IL void arr(int x,int y,int z,int flow,double cost)
    {
         a[++l].a=head[x];
         a[l].b=y;
         a[l].c=z;
         a[l].flow=flow;
         a[l].from=x;
         a[l].cost=cost;
         head[x]=l;
    }
    IL bool bellmanford(double &flow,double &cost)
    {
          queue<int>q;
        for (rg int i=1;i<=t;i++) dis[i]=INF;
        aa[s]=INF;
        memset(vis,1,sizeof(vis));
        q.push(s);
        while (!q.empty())
        {
            rg int x=q.front(); q.pop();
            rg int u=head[x];
            while (u)
            {
              rg  int v=a[u].b;
              if (dis[x]+a[u].cost<dis[v]&&a[u].c>a[u].flow)
              {
                   dis[v]=dis[x]+a[u].cost;
                   aa[v]=min(aa[x],a[u].c-a[u].flow);
                   pre[v]=u;
                   if (vis[v])
                   {
                         vis[v]=0; q.push(v);
                     }
              }
              u=a[u].a;
            }
            vis[x]=1;
        }
        if (dis[t]==INF) return(0);
        rg int x=t; flow+=aa[t]; cost+=aa[t]*dis[t];
        while (pre[x])
        {
            rg int y=pre[x];
            a[y].flow+=aa[t];
            if (y%2==1) a[y+1].flow-=aa[t];
                   else a[y-1].flow-=aa[t];
            x=a[y].from;
        }
        return 1;
    }
    double flow,cost;
    IL void mincost()
    {
        while (bellmanford(flow,cost));
    }
    #define mid (h+t)/2
    IL bool check(double x)
    {
        l=0;
        memset(head,0,sizeof(head));
        for (rg int i=1;i<=n;i++)
          for (rg int j=1;j<=n;j++)
          {
              arr(i,j+n,1,0,-(p1[i][j]-p2[i][j]*x));
              arr(j+n,i,0,0,(p1[i][j]-p2[i][j]*x));
            }
        s=0,t=2*n+1;
        for (rg int i=1;i<=n;i++)
        {
            arr(0,i,1,0,0); arr(i,0,0,0,0);
            arr(i+n,t,1,0,0); arr(t,i+n,0,0,0);
        }
        flow=0; cost=0;
        mincost();
        if (cost<=0) return(1);
        else return(0);
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
        cin>>n;
        for (rg int i=1;i<=n;i++)
          for (rg int j=1;j<=n;j++)
            cin>>p1[i][j];
        for (rg int i=1;i<=n;i++)
          for (rg int j=1;j<=n;j++)
            cin>>p2[i][j];
        double h=0,t=INF2;
        while (t-h>eps)
        {
            if (check(mid)) h=mid;
            else t=mid;
        }
        printf("%.6f",h);
        return 0;
    }
  • 相关阅读:
    Windows共享作为公司文件服务器的案例
    关于限制域用户登陆计算机的几点总结
    防止加入域主机脱离域的控制(管理员权限)
    域组策略禁止执行指定软件
    域用户执行金蝶K/3报错解决方法
    WinXP/Win7/Win8本地用户配置文件迁移至域用户
    Windows批处理:请求远程协助
    浅谈JavaScript闭包
    新蛋那几月
    JavaScript 32位整型无符号操作
  • 原文地址:https://www.cnblogs.com/yinwuxiao/p/8930024.html
Copyright © 2011-2022 走看看