Link: bzoj-1070
Question Analysis
First, we thought of splitting points, splitting each technician into N points, splitting the I point from a technician, and connecting to a car, it indicates that this is the last car repaired by the technician. So the contribution of this car repair to the entire answer is I * time [J] [K]. J indicates the vehicle ID and K indicates the technician ID. Because the waiting time for this car and the car to be repaired subsequently increases by time [J] [K], so the waiting time for a total of I cars, including this car, and the time for repairing the car. In this way, the cost of the edge can easily represent the time for car repair to increase everyone. All vertices and edges split from S to all technicians, capacity 1, cost 0; from each car to t edge, capacity 1, cost 0.
The minimum cost for the last running side is the sum of the waiting time of all vehicles.
Code
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <queue>using namespace std;const int MaxN = 1000 + 5, MaxM = 100000 + 5, INF = 999999999;int n, m, S, T, Ans;int Time[60 + 5][9 + 5], d[MaxN];bool Used[MaxN];queue<int> Q;inline int gmin(int a, int b) {return a < b ? a : b;}inline int gmax(int a, int b) {return a > b ? a : b;} struct Edge {int u, v, w, Cost;Edge *Next, *Other;} E[MaxM], *P = E, *Point[MaxN], *Pre[MaxN]; inline void AddEdge(int x, int y, int z, int Ct) {Edge *Q = ++P; ++P;P -> u = x; P -> v = y; P -> w = z; P -> Cost = Ct; P -> Next = Point[x]; Point[x] = P; P -> Other = Q;Q -> u = y; Q -> v = x; Q -> w = 0; Q -> Cost = -Ct;Q -> Next = Point[y]; Point[y] = Q; Q -> Other = P; }bool Found() {memset(d, 0x7f, sizeof(d));memset(Used, 0, sizeof(Used));d[S] = 0; Used[S] = true; Q.push(S);while (!Q.empty()) {int x = Q.front();Used[x] = false;Q.pop();for (Edge *j = Point[x]; j; j = j -> Next) {if (j -> w && d[j -> v] > d[x] + j -> Cost) {d[j -> v] = d[x] + j -> Cost;Pre[j -> v] = j;if (!Used[j -> v]) {Used[j -> v] = true;Q.push(j -> v);}}}}if (d[T] < INF) return true;return false;}void Augment() {int Flow;Flow = INF;for (Edge *j = Pre[T]; j; j = Pre[j -> u]) Flow = gmin(Flow, j -> w);for (Edge *j = Pre[T]; j; j = Pre[j -> u]) {j -> w -= Flow;j -> Other -> w += Flow;}Ans += Flow * d[T];}int main() {scanf("%d%d", &m, &n);for (int i = 1; i <= n; ++i) {for (int j = 1; j <= m; ++j) {scanf("%d", &Time[i][j]);}}S = n * m + n + 1; T = S + 1;for (int i = 1; i <= n * m; ++i) AddEdge(S, i, 1, 0);for (int i = n * m + 1; i <= n * m + n; ++i) AddEdge(i, T, 1, 0);for (int i = 1; i <= m; ++i) {for (int j = 1; j <= n; ++j) {for (int k = 1; k <= n; ++k) {AddEdge((i - 1) * n + j, n * m + k, 1, j * Time[k][i]);}}}Ans = 0;while (Found()) Augment();printf("%.2lf\n", (double)Ans / (double)n);return 0; }
[Bzoj 1070] [scoi2007] Car Repair [billing flow]