zoukankan      html  css  js  c++  java
  • POJ 3686.The Windy's 最小费用最大流

    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 5477   Accepted: 2285

    Description

    The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys. The manager knows that every order will take different amount of hours in different workshops. More precisely, the i-th order will take Zij hours if the toys are making in the j-th workshop. Moreover, each order's work must be wholly completed in the same workshop. And a workshop can not switch to another order until it has finished the previous one. The switch does not cost any time.

    The manager wants to minimize the average of the finishing time of the N orders. Can you help him?

    Input

    The first line of input is the number of test case. The first line of each test case contains two integers, N and M (1 ≤ N,M ≤ 50).
    The next N lines each contain M integers, describing the matrix Zij (1 ≤ Zij ≤ 100,000) There is a blank line before each test case.

    Output

    For each test case output the answer on a single line. The result should be rounded to six decimal places.

    Sample Input

    3
    
    3 4
    100 100 100 1
    99 99 99 1
    98 98 98 1
    
    3 4
    1 100 100 100
    99 1 99 99
    98 98 1 98
    
    3 4
    1 100 100 100
    1 99 99 99
    98 1 98 98
    

    Sample Output

    2.000000
    1.000000
    1.333333
    
    

    Source

     
    题意:有n个玩具m个工厂,一个工厂同时只能生产一个玩具。问平均每个玩具的生产时间。
    思路:现在有n个玩具,m个工厂,但是一个工厂可以生产多个玩具。当一个工厂按顺序生产a1,a2,a3...,an玩具,每个玩具的生产时间为Za1,Za1+Za2,Za1+Za2+Za3,...,Za1+Za2+Za3+...+Zan。T=n*Za1+(n-1)*Za2+(n-2)*Za3+...+Zan。这个式子也可以理解为,多个只能生产1个玩具的工厂,但是时间为1~n倍。
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> P;
    #define PI acos(-1.0)
    const int maxn=3e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
    const ll INF=1e18+7;
    struct edge
    {
        int from,to;
        int cap,cost;
        int rev;
    };
    int NN;
    vector<edge>G[maxn];
    int h[maxn];
    ///顶点的势,取h(u)=(s到u的最短距离),边e=(u,v)的长度变成d`(e)=d(e)+h(u)-h(v)>=0
    int dist[maxn];
    int prevv[maxn],preve[maxn];///前驱结点和对应的边
    void addedge(int u,int v,int cap,int cost)
    {
        edge e;
        e.from=u,e.to=v,e.cap=cap,e.cost=cost,e.rev=G[v].size();
        G[u].push_back(e);
        e.from=v,e.to=u,e.cap=0,e.cost=-cost,e.rev=G[u].size()-1;
        G[v].push_back(e);
    }
    int min_cost_flow(int s,int t,int f)
    {
        int res=0;
        fill(h,h+NN,0);
        while(f>0)
        {
            priority_queue<P,vector<P>,greater<P> >q;
            fill(dist+1,dist+NN,inf);
            dist[s]=0;
            q.push(P(dist[s],s));
            while(!q.empty())
            {
                P p=q.top();
                q.pop();
                int u=p.second;
                if(dist[u]<p.first) continue;
                for(int i=0; i<G[u].size(); i++)
                {
                    edge e=G[u][i];
                    if(e.cap>0&&dist[e.to]>dist[u]+e.cost+h[u]-h[e.to])
                    {
                        dist[e.to]=dist[u]+e.cost+h[u]-h[e.to];
                        prevv[e.to]=u;
                        preve[e.to]=i;
                        q.push(P(dist[e.to],e.to));
                    }
                }
            }
            if(dist[t]==inf) return res;
            for(int i=0; i<NN; i++) h[i]+=dist[i];
            int d=f;
            for(int i=t; i!=s; i=prevv[i])
                d=min(d,G[prevv[i]][preve[i]].cap);
            f-=d;
            res+=d*h[t];
            for(int i=t; i!=s; i=prevv[i])
            {
                //cout<<i<<" ";
                edge &e=G[prevv[i]][preve[i]];
                e.cap-=d;
                G[i][e.rev].cap+=d;
            }
            //cout<<s<<endl;
        }
        return res;
    }
    int z[100][100];
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            int s=0,t=n+n*m+1;
            NN=t+1;
            for(int i=1; i<=n; i++)
            {
                addedge(s,i,1,0);
                for(int j=1; j<=m; j++)
                {
                    scanf("%d",&z[i][j]);
                    for(int k=1; k<=n; k++)
                        addedge(i,n+(j-1)*n+k,1,k*z[i][j]);
                }
            }
            for(int j=1; j<=m; j++)
            {
                for(int k=1; k<=n; k++)
                    addedge(n+(j-1)*n+k,t,1,0);
            }
            printf("%.6f
    ",min_cost_flow(s,t,inf)*1.0/n);
            for(int i=0; i<NN; i++) G[i].clear();
        }
        return 0;
    }
    最小费用最大流
  • 相关阅读:
    webpack压缩图片之项目资源优化
    vue v-cloak 指令 处理页面显示源码
    js 获取url 参数
    element-ui Drawer抽屉组件封装
    js中的this指向
    对js闭包的理解
    vue作用域插槽
    flex布局实战
    vue 组件之间传值
    js 面试题一
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7257734.html
Copyright © 2011-2022 走看看