POJ_3686_The Windy's(最小费用流 / KM)

来源:互联网 发布:php高级工程师简历 编辑:程序博客网 时间:2024/06/14 10:58
The Windy's
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 4660 Accepted: 1962

Description

The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receivesN orders for toys. The manager knows that every order will take different amount of hours in different workshops. More precisely, thei-th order will take Zij hours if the toys are making in thej-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 andM (1 ≤N,M ≤ 50).
The next N lines each contain M integers, describing the matrixZij (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

33 4100 100 100 199 99 99 198 98 98 13 41 100 100 10099 1 99 9998 98 1 983 41 100 100 1001 99 99 9998 1 98 98

Sample Output

2.0000001.0000001.333333

题意:现在要加工M个产品,然后N个工厂,告诉产品在每个工厂加工所需的时间,一个工厂每次只能加工一个产品,现在问加工所有产品所用的时间平均数最小是多少。


分析:最小费用流 or 二分图带权匹配。

最小费用流:假设有k个产品在a工厂加工,那么总时间就是k*a1 + (k-1) * a2 + …… + ak;由此我们可以把 j 工厂拆成N个点,然后由 i 产品指向k点的时候表示该产品在 j 工厂第k个加工,容量为1,费用为 k * Z[i][j];加入超级源点s(0)指向所有的产品,容量为1,费用为0;加入超级汇点t(N+N*M+1),工厂拆成的点都指向汇点t,容量为1,费用为0;然后跑一遍最小费用流即可。

二分图带权匹配:运用KM算法,原则上差不多,只是KM是求最大权匹配,所以可以把边权值赋成相反数,然后再取反即可得。


题目链接:http://poj.org/problem?id=3686


代码清单:

最小费用流

#include<map>#include<set>#include<cmath>#include<queue>#include<stack>#include<ctime>#include<cctype>#include<string>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;#define end() return 0typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxn = 1000000 + 5;const int INF = 0x7f7f7f7f;struct Edge{    int from,to,cap,flow,cost;    Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){}};struct MCMF{    int n,m;    vector<Edge>edge; //边数的两倍    vector<int>G[maxn]; //邻接表,G[i][j]表示i的第j条边在e数组中的序号    int inq[maxn]; //是否在队列    int d[maxn]; //Bellman-Ford    int p[maxn]; //上一条弧    int a[maxn]; //可改进量    void init(int n){        this -> n = n;        for(int i=0;i<=n;i++) G[i].clear();        edge.clear();    }    void addEdge(int from,int to,int cap,int cost){        edge.push_back(Edge(from,to,cap,0,cost));        edge.push_back(Edge(to,from,0,0,-cost));        m=edge.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    bool BellmanFord(int s,int t,int f,int& flow,int& cost){        fill(d,d+t+1,INF);        memset(inq,0,sizeof(inq));        d[s]=0; inq[s]=1; p[s]=0; a[s]=INF;        queue<int>q;        q.push(s);        while(!q.empty()){            int u=q.front();q.pop();            inq[u]=0;            for(int i=0;i<G[u].size();i++){                Edge& e=edge[G[u][i]];                if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){                    d[e.to]=d[u]+e.cost;                    p[e.to]=G[u][i];                    a[e.to]=min(a[u],e.cap-e.flow);                    if(!inq[e.to]){                        q.push(e.to);                        inq[e.to]=1;                    }                }            }        }        if(d[t]==INF) return false;        flow+=a[t];        cost+=a[t]*d[t];        if(flow==f) return false;        for(int u=t;u!=s;u=edge[p[u]].from){            edge[p[u]].flow+=a[t];            edge[p[u]^1].flow-=a[t];        }        return true;    }    //需要保证初始网络中没有负权圈    int MincostMaxflow(int s,int t,int f){        int flow=0,cost=0;        while(BellmanFord(s,t,f,flow,cost));        return cost;    }};int T;int N,M;int tail;int c[105][105];MCMF mcmf;void input(){    scanf("%d%d",&N,&M);    for(int i=1;i<=N;i++){        for(int j=1;j<=M;j++){            scanf("%d",&c[i][j]);        }    }}void createGraph(){    tail=N+M*N+1;    mcmf.init(tail+1);    for(int i=1;i<=N;i++){        mcmf.addEdge(0,i,1,0);    }    for(int i=N+1;i<=tail-1;i++){        mcmf.addEdge(i,tail,1,0);    }    for(int i=1;i<=N;i++){        for(int j=1;j<=M;j++){            for(int k=1;k<=N;k++){                mcmf.addEdge(i,j*N+k,1,k*c[i][j]);            }        }    }}void solve(){    createGraph();    printf("%.6lf\n",((double)mcmf.MincostMaxflow(0,tail,N))/((double)N));}int main(){    scanf("%d",&T);    while(T--){        input();        solve();    }    end();}

KM算法

#include <map>#include <set>#include <cmath>#include <queue>#include <stack>#include <ctime>#include <vector>#include <cctype>#include <string>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define end() return 0typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxn = 3000 + 5;const int INF = 0x7f7f7f7f;int nx, ny, w[maxn][maxn];int match[maxn], lx[maxn], ly[maxn], slack[maxn];bool visx[maxn], visy[maxn];bool dfs(int x){visx[x] = true;for(int y = 1; y <= ny; y++){if(visy[y]) continue;int t = lx[x] + ly[y] - w[x][y];if(t == 0){visy[y] = true;if(match[y] == -1 || dfs(match[y])){match[y] = x;return true;}}else if(slack[y] > t){ //不在相等子图中slack取最小的slack[y] = t;}}return false;}int KM(int n, int m){nx = n;ny = n * m;memset(match, -1, sizeof(match));memset(ly, 0, sizeof(ly));for(int i = 1; i <= nx; i++){ //lx 初始化为与它关联边中最大的lx[i] = -INF;for(int j = 1; j <= ny; j++){if(w[i][j] > lx[i]) lx[i] = w[i][j];}}for(int x = 1; x <= nx; x++){for(int i = 1; i <= ny; i++){slack[i] = INF;}while(true){memset(visx, false, sizeof(visx));memset(visy, false, sizeof(visy));//若成功(找到了增广路),则该点增广完毕,下一个点进入增广if(dfs(x)) break;//若失败,则需要改变一些点的顶标,使得图中可行边的数量增加//(1)将所有在增广轨中的 X 方点的标号 全部减去一个常数 d ;//(2)将所有在增广轨中的 Y 方点的标号 全部加上一个常数 d ;int d = INF;for(int i = 1; i <= ny; i++){if(!visy[i] && d > slack[i]) d = slack[i];}for(int i = 1; i <= nx; i++){if(visx[i]) lx[i] -= d;}for(int i = 1; i <= ny; i++){if(visy[i]) ly[i] += d;else slack[i] -= d;}}}int res = 0;for(int i = 1; i <= ny; i++){if(match[i] > -1) res += w[match[i]][i];}return -res;}const int maxN = 50 + 5;int T;int N, M;int Z[maxN][maxN];void input(){scanf("%d%d", &N, &M);for(int i = 1; i <= N; i++){for(int j = 1; j <= M; j++){scanf("%d", &Z[i][j]);}}}void createGraph(){for(int i = 1; i <= N; i++){for(int j = 1; j <= M; j++){for(int k = 1; k <= N; k++){w[i][(j - 1) * N + k] = -Z[i][j] * k;}}}}void solve(){createGraph();printf("%.6lf\n", (double)KM(N, M) / (double)N);}int main(){scanf("%d", &T);while(T--){input();solve();}   end();}


0 0