1. 程式人生 > >[SCOI2007]修車 費用流

[SCOI2007]修車 費用流

題目描述

同一時刻有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;
}