bzoj1070:
把每个工人拆成汽车那么多个点,假如说 工人(i, j) 和 汽车k 连边,那就代表第i个工人倒数第j个修汽车k,那么这条边对以后的贡献就是k*time[i修k]。
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define drep(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define mp make_pair
#define pb push_back
#define clr(x) memset(x, 0, sizeof(x))
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int, int> pii;
const int inf = ~0U >> ;
const i64 INF = ~0ULL >> ;
//********************************* const int maxn = , maxm = ; struct Ed {
int u, v, nx, c, w; Ed() {}
Ed(int _u, int _v, int _nx, int _c, int _w) :
u(_u), v(_v), nx(_nx), c(_c), w(_w) {}
} E[maxm << ];
int G[maxn], edtot;
void addedge(int u, int v, int c, int w) {
E[edtot] = (Ed){u, v, G[u], c, w};
G[u] = edtot++;
E[edtot] = (Ed){v, u, G[v], , -w};
G[v] = edtot++;
} bool vis[maxn]; int dis[maxn], s, t;
bool spfa() {
static int que[maxm]; int qh(), qt();
rep(i, s, t) vis[i] = , dis[i] = 0x3f3f3f3f;
dis[que[++qt] = s] = ; vis[s] = ;
while (qh != qt) {
int x = que[++qh]; vis[x] = ;
for (int i = G[x]; i != -; i = E[i].nx) {
if (E[i].c && dis[E[i].v] > dis[x] + E[i].w) {
dis[E[i].v] = dis[x] + E[i].w;
if (!vis[E[i].v]) vis[que[++qt] = E[i].v] = ;
}
}
}
return dis[t] != 0x3f3f3f3f;
}
int ans, cur[maxn];
int dfs(int u, int rm) {
vis[u] = ;
if (u == t) return rm;
int rm1 = rm;
for (int &i = cur[u]; i != -; i = E[i].nx) {
if (E[i].c && !vis[E[i].v] && dis[E[i].v] == dis[u] + E[i].w) {
int flow = dfs(E[i].v, min(rm, E[i].c));
E[i].c -= flow, E[i ^ ].c += flow;
ans += flow * E[i].w;
if ((rm -= flow) == ) break;
}
}
if (rm1 == rm) dis[u] = ;
return rm1 - rm;
} int a[][];
int main() {
int m, n; scanf("%d%d", &m, &n);
rep(i, , n) rep(j, , m) scanf("%d", &a[i][j]);
s = , t = n + n * m + ;
memset(G, -, sizeof(G));
rep(i, , n) addedge(s, i, , );
rep(i, n + , n + n * m) addedge(i, t, , );
rep(i, , n) {
rep(j, , m) {
rep(k, , n) {
addedge(i, n + (j - ) * n + k, , k * a[i][j]);
}
}
}
while (spfa()) memcpy(cur, G, sizeof(G)),dfs(s, 0x3f3f3f3f);
printf("%.2lf\n", 1.0 * ans / n);
return ;
}
bzoj2879:
首先把每个食物和厨师连边,一开始只用和每个厨师的倒数第一这个时间段连边,如果用了的话再用这个厨师的倒数第二去连边。
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define drep(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define mp make_pair
#define clr(x) memset(x, 0, sizeof(x))
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int, int> pii;
const int inf = ~0U >> ;
const i64 INF = ~0ULL >> ;
//********************************** const int maxn = , maxm = ; struct Ed {
int u, v, nx, c, w; Ed() {}
Ed(int _u, int _v, int _nx, int _c, int _w) :
u(_u), v(_v), nx(_nx), c(_c), w(_w) {}
} E[maxm];
int G[maxn], edtot = ;
void addedge(int u, int v, int c, int w) {
E[++edtot] = Ed(u, v, G[u], c, w);
G[u] = edtot;
E[++edtot] = Ed(v, u, G[v], , -w);
G[v] = edtot;
} int tot, n, m; bool vis[maxn]; int dis[maxn], s, t, pre[maxn];
bool spfa() {
static int que[maxn]; int qh(), qt();
rep(i, s, t) vis[i] = , dis[i] = inf;
vis[que[++qt] = s] = , dis[s] = ;
while (qh != qt) {
int x = que[++qh]; if (qh == t) qh = ;
for (int i = G[x]; i; i = E[i].nx) {
if (E[i].c && dis[E[i].v] > dis[x] + E[i].w) {
dis[E[i].v] = dis[x] + E[i].w;
pre[E[i].v] = i;
if (!vis[E[i].v]) {
vis[que[++qt] = E[i].v] = ;
if (qt == t) qt = ;
}
}
}
vis[x] = ;
}
return dis[t] != inf;
}
int a[][];
int ans;
void mcf() {
int flow = inf, x, y;
for (int i = pre[t]; i; i = pre[E[i].u]) {
flow = min(flow, E[i].c);
if (E[i].v == t) {
x = (E[i].u - ) / tot + ; y = E[i].u % tot + ;
}
}
for (int i = pre[t]; i; i = pre[E[i].u]) {
E[i].c -= flow, E[i ^ ].c += flow, ans += flow * E[i].w;
}
addedge((x - ) * tot + y, t, , );
for (int i = ; i <= n; i++)
addedge(m * tot + i, (x - ) * tot + y, , y * a[i][x]);
} int main() {
scanf("%d%d", &n, &m);
static int c[];
rep(i, , n) scanf("%d", c + i), tot += c[i];
rep(i, , n) rep(j, , m) scanf("%d", &a[i][j]);
s = , t = m * tot + n + ;
rep(i, , n) addedge(s, m * tot + i, c[i], );
rep(i, , m) addedge((i - ) * tot + , t, , );
rep(i, , m) rep(j, , n) addedge(m * tot + j, (i - ) * tot + , , a[j][i]);
while (spfa()) mcf();
printf("%d\n", ans);
return ;
}
注意,把从食物向厨师连边比较快。