修车加强版。发现每个厨师拆成p个点太浪费了,毕竟总共用到的才p个点。于是从下往上一个一个加,加到满流就停。
论动态加点费用流的正确姿势......
我自己加总是出现负环...我是每次加一整层,然后跑完这一层再加下一层,这样会显而易见的出现负环......
然后我们发现如果每增广一流量就加边就不会出现这种毒瘤现象,因为每次加的一定比增广的劣......
注意一定要动态开点,不能只用一个点代表厨师。否则可能出现厨师的第一次给了多个菜的情况...
#include <bits/stdc++.h> const int N = , INF = 0x7f7f7f7f; struct Edge {
int nex, v, c, len;
Edge(int Nex = , int V = , int C = , int Len = ) {
nex = Nex;
v = V;
c = C;
len = Len;
}
}edge[]; int tp = ; int e[N], lm, now[N], pre[N], flow[N], p[N], cnt[N], d[N];
int val[][];
bool vis[N];
std::queue<int> Q; inline void add(int x, int y, int z, int w) {
edge[++tp] = Edge(e[x], y, z, w);
e[x] = tp;
edge[++tp] = Edge(e[y], x, , -w);
e[y] = tp;
return;
} inline bool SPFA(int s, int t) {
memset(d + , 0x7f, lm * sizeof(int));
vis[s] = ;
flow[s] = INF;
d[s] = ;
Q.push(s);
while(!Q.empty()) {
int x = Q.front();
Q.pop();
vis[x] = ;
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
if(d[y] > d[x] + edge[i].len && edge[i].c) {
d[y] = d[x] + edge[i].len;
//printf("%d -> %d \n", x, y);
flow[y] = std::min(flow[x], edge[i].c);
pre[y] = i;
if(!vis[y]) {
vis[y] = ;
Q.push(y);
}
}
}
}
return d[t] < INF;
} inline void update(int s, int t) {
int f = flow[t];
while(t != s) {
int i = pre[t];
edge[i].c -= f;
edge[i ^ ].c += f;
t = edge[i ^ ].v;
}
return;
} int main() { int n, m, tot = ;
scanf("%d%d", &n, &m);
lm = n;
int s = ++lm;
int t = ++lm;
for(int i = ; i <= n; i++) {
scanf("%d", &p[i]);
add(s, i, p[i], );
tot += p[i];
}
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
scanf("%d", &val[i][j]);
}
}
/// sol
for(int i = ; i <= m; i++) {
add(++lm, t, , );
cnt[i] = ;
now[i] = tp - ;
for(int j = ; j <= n; j++) {
add(j, lm, , val[j][i]);
}
} int ans = ;
while(tot) {
//printf("tot = %d ans = %d \n", tot, ans);
SPFA(s, t);
ans += d[t] * flow[t];
tot -= flow[t];
update(s, t);
for(int j = ; j <= m; j++) {
if(edge[now[j]].c) continue;
add(++lm, t, , );
now[j] = tp - ;
cnt[j]++;
for(int i = ; i <= n; i++) {
add(i, lm, , cnt[j] * val[i][j]);
}
break;
}
} printf("%d\n", ans);
return ;
}
AC代码