[SCOI2007]修车 费用流 BZOJ 1070

题目描述

同一时刻有N位车主带着他们的爱车来到了汽车维修中心。维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的。现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均等待的时间最小。

说明:顾客的等待时间是指从他把车送至维修中心到维修完毕所用的时间。

输入输出格式

输入格式:

第一行有两个数M,N,表示技术人员数与顾客数。

接下来n行,每行m个整数。第i+1行第j个数表示第j位技术人员维修第i辆车需要用的时间T。

输出格式:

最小平均等待时间,答案精确到小数点后2位。

输入输出样例

输入样例#1: 复制
2 2
3 2
1 4
输出样例#1: 复制
1.50

说明

(2<=M<=9,1<=N<=60), (1<=T<=1000)

假设对于某一个技术工人来说,维修序列为:

w1,w2,w3...,wn;

那么等待的时间:

T=n*w1+(n-1)*w2+...+wn;

可见费用(消耗的时间)与位置有关;

那么我们将技术工拆点,拆成 n 个;

然后建边的时候分别设立不同的费用,最后跑一下最小费用最大流;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 20005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

bool vis[maxn];
int n, m, s, t;
int x, y, f, z;
int dis[maxn], pre[maxn], last[maxn], flow[maxn];
int maxflow, mincost;

struct node {
	int to, nxt, flow, cost;
}edge[maxn<<2];

int head[maxn], cnt;
queue<int>q;

void addedge(int from, int to, int flow, int cost) {
	edge[++cnt].to = to; edge[cnt].flow = flow; edge[cnt].cost = cost;
	edge[cnt].nxt = head[from]; head[from] = cnt;
}

bool spfa(int s, int t) {
	memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow));
	ms(vis);
	q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1;
	while (!q.empty()) {
		int now = q.front(); q.pop(); vis[now] = 0;
		for (int i = head[now]; i != -1; i = edge[i].nxt) {
			if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].cost) {
				int v = edge[i].to;
				dis[v] = dis[now] + edge[i].cost;
				pre[v] = now; last[v] = i;
				flow[v] = min(flow[now], edge[i].flow);
				if (!vis[v]) {
					vis[v] = 1; q.push(v);
				}
			}
		}
	}
	return pre[t] != -1;
}

void mincost_maxflow() {
	while (spfa(s, t)) {
		int now = t;
		maxflow += flow[t]; mincost+=flow[t] * dis[t];
		while (now != s) {
			edge[last[now]].flow -= flow[t];
			edge[last[now] ^ 1].flow += flow[t];
			now = pre[now];
		}
	}
}


int main() {
	//ios::sync_with_stdio(0);
	memset(head, -1, sizeof(head)); cnt = 1;
	rdint(m); rdint(n);
	s = 1000; t = s + 1;
	for (int i = 1; i <= n; i++)addedge(s, i, 1, 0), addedge(i, s, 0, 0);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			int tmp; rdint(tmp);
			for (int k = 1; k <= n; k++) {
				addedge(i, j*n + k, 1, tmp*k); addedge(j*n + k, i, 0, -tmp * k);
			}
		}
	}
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n; j++)addedge(i*n + j, t, 1, 0), addedge(t, i*n + j, 0, 0);
	}
	mincost_maxflow();
	printf("%.2lf\n", 1.0*mincost / n);
	return 0;
}

 

posted @ 2019-01-02 20:04  NKDEWSM  阅读(141)  评论(0编辑  收藏  举报