修车
题目链接
https://www.luogu.org/problemnew/show/P2053
题解
每个人每次只能修一辆车,且这个人修的最后一辆车所花时间为111倍的修这辆车的时间,修倒数第iii辆车所花的时间是iii倍修这辆车所花的时间.
000号点代表源点,编号为1−M1-M1−M的点代表维修工人,0→[1,M]0 \rightarrow [1,M]0→[1,M]各有一条容量为INFINFINF,费用为000的点.代表每个维修工人可以维修多辆车.
(每个)第personpersonperson个维修工人应该向外连出NNN个点,容量为111,费用为000.这NNN个点中的第iii个点的含义代表该工人接下来要修的车是逆序第iii个,因此第iii个点还应该向NNN个车(carcarcar)连边,每条边容量为111,费用为i×c[person][car]i \times c[person][car]i×c[person][car].
每辆车应该向汇点TTT连接一条容量为111,费用为000的边,表示这辆车只需要维修一次.
最后跑MCMFMCMFMCMF即可.
代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#define pr(x) std::cout << #x << ':' << x << std::endl
#define rep(i,a,b) for(int i = a;i <= b;++i)const int inf = 0x3f3f3f3f;
const int mm = 111111;
const int maxn = 999;
int node,src,dest,edge;
int ver[mm],flow[mm],cst[mm],nxt[mm];
int head[maxn],work[maxn],dis[maxn],q[maxn];
int tot_cost;
void prepare(int _node,int _src,int _dest)
{node=_node,src=_src,dest=_dest;for(int i=0; i<node; ++i)head[i]=-1;edge=0;tot_cost = 0;
}
void add_edge(int u,int v,int c,int cost)
{ver[edge]=v,flow[edge]=c,nxt[edge]=head[u],cst[edge]=cost,head[u]=edge++;ver[edge]=u,flow[edge]=0,nxt[edge]=head[v],cst[edge]=-cost,head[v]=edge++;
}
int ins[maxn];
int pre[maxn];
bool Dinic_spfa()
{memset(ins,0,sizeof(ins));memset(dis,inf,sizeof(dis));memset(pre,-1,sizeof(pre));std::queue<int> Q;Q.push(src);dis[src] = 0,ins[src] = 1;pre[src] = -1;while(!Q.empty()){int u = Q.front();Q.pop();ins[u] = 0;for(int e = head[u];e != -1;e = nxt[e]){int v = ver[e];if(!flow[e]) continue;if(dis[v] > dis[u] + cst[e]){dis[v] = dis[u] + cst[e];pre[v] = e;if(!ins[v]) ins[v] = 1,Q.push(v);}}}return dis[dest] < inf;
}
int Dinic_flow()
{int i,ret=0,delta=inf;while(Dinic_spfa()){for(int i=pre[dest];i != -1;i = pre[ver[i^1]])delta = std::min(delta,flow[i]);for(int i=pre[dest];i != -1;i = pre[ver[i^1]])flow[i] -= delta,flow[i^1] += delta;ret+=delta;tot_cost += dis[dest]*delta;}return ret;
}
int c[10][70];
int n,m;
int main() {//std::ios::sync_with_stdio(false);while(std::cin >> m >> n) {prepare(n+m+2+m*n,0,n+m+1);for(int i = 1;i <= n;++i) {for(int j = 1;j <= m;++j) {std::cin >> c[j][i];}}for(int i = 1;i <= m;++i) {add_edge(0,i,inf,0);}for(int i = 1;i <= n;++i) {add_edge(m+i,m+n+1,1,0);}int tot = m+n+1;for(int i = 1;i <= m;++i) {for(int j = 1;j <= n;++j) {++tot;add_edge(i,tot,1,0);for(int k = 1;k <= n;++k) {add_edge(tot,k+m,1,j*c[i][k]);}}}int myflow = Dinic_flow();printf("%.2f\n",1.0*tot_cost/n);}return 0;
}