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

The Windy's
Time Limit: 5000MS Memory Limit: 65536K
Total 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

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

题意:现在要加工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 0

typedef 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 0

typedef 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值