https://www.luogu.org/problemnew/show/P2053
题意
同一时刻有N位车主带着他们的爱车来到了汽车维修中心。维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的。现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均等待的时间最小。
说明:顾客的等待时间是指从他把车送至维修中心到维修完毕所用的时间。
思路
左边放n个点,表示n辆车。右边放m * n个点,表示m个工人,拆除n个点表示不同阶段。即如果第i辆车连上了第m个工人的第k个阶段,表示第m个工人第k阶段才修理第i辆车。
假设第i辆车的等待时间是wi
那么总等待时间是n*w1 + (n-1)*w2 + …… + 1*wn;
所以越早修理,需要的时间是多倍的。
addedge(i, n + (j-1)*n + k, 1, mp[i][j] * (n - k + 1));
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \(‘?‘) > ⌒ヽ / へ\ / / \\ ? ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / ‘ノ ) L? */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ‘\n‘ #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<‘0‘||ch>‘9‘) f|=(ch==‘-‘),ch=getchar(); while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 609; struct E{ int v,val; int cost; int nxt; }edge[80009]; int head[maxn],gtot = 0; void addedge(int u,int v,int val,int cost){ edge[gtot].v = v; edge[gtot].val = val; edge[gtot].cost = cost; edge[gtot].nxt = head[u]; head[u] = gtot++; edge[gtot].v = u; edge[gtot].val = 0; edge[gtot].cost = -cost; edge[gtot].nxt = head[v]; head[v] = gtot++; } int mp[maxn][maxn]; int dis[maxn],path[maxn],vis[maxn],pre[maxn]; bool spfa(int s,int t){ memset(pre, -1 ,sizeof(pre)); memset(dis, inf,sizeof(dis)); memset(vis, 0, sizeof(vis)); queue<int>que; que.push(s); vis[s] = 1; dis[s] = 0; while(!que.empty()){ int u = que.front(); que.pop(); vis[u] = 0; for(int i=head[u]; ~i; i = edge[i].nxt){ int v = edge[i].v, val = edge[i].val, cost = edge[i].cost; if(val > 0 && dis[v] > dis[u] + cost){ dis[v] = dis[u] + cost; pre[v] = u; path[v] = i; if(vis[v] == 0){ vis[v] = 1; que.push(v); } } } } return pre[t] != -1; } int mcmf(int s,int t){ int flow = 0,cost = 0; while(spfa(s, t)){ int f = inf; for(int i=t; i!=s; i = pre[i]){ f = min(f, edge[path[i]].val); } flow += f; cost += f * dis[t]; for(int i=t; i!=s; i = pre[i]){ edge[path[i]].val -= f; edge[path[i]^1].val += f; } } return cost; } int main(){ memset(head, -1, sizeof(head)); int n,m; scanf("%d%d", &m, &n); int s = 0, t = n+m*n+1; rep(i, 1, n) { addedge(s, i, 1, 0); rep(j, 1, m){ scanf("%d", &mp[i][j]); } } for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ for(int t = 1; t<=n; t++){ addedge(i, n + (j-1)*n + t, 1, mp[i][j] * (n - t + 1)); } } } for(int i=n+1; i<t; i++) addedge(i, t, 1, 0); printf("%.2f\n", 1.0*mcmf(s,t)/n); return 0; }
原文地址:https://www.cnblogs.com/ckxkexing/p/10393519.html
时间: 2024-08-12 14:45:27