BZOJ 1070 [SCOI2007]修车
题解:
最小费用最大流
为每个师傅建一排点,表示这个师傅修的第几量车,然后他会对后面的人产生影响
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std; const int maxn=10009; const int oo=1000000000; int n,m; int p[200][200]; struct Edge{ int from,to,cap,flow,cost; }; vector<int>G[maxn]; vector<Edge>edges; void Addedge(int x,int y,int z,int w){ Edge e; e.from=x;e.to=y;e.cap=z;e.flow=0;e.cost=w; edges.push_back(e); e.from=y;e.to=x;e.cap=0;e.flow=0;e.cost=-w; edges.push_back(e); int c=edges.size(); G[x].push_back(c-2); G[y].push_back(c-1); } int s,t,totn; int pre[maxn]; int d[maxn]; int inq[maxn]; queue<int>q; int Spfa(int &nowflow,int &nowcost){ for(int i=1;i<=totn;++i){ inq[i]=0;d[i]=oo; } inq[s]=1;d[s]=0;q.push(s); while(!q.empty()){ int x=q.front();q.pop();inq[x]=0; for(int i=0;i<G[x].size();++i){ Edge e=edges[G[x][i]]; if((e.cap>e.flow)&&(d[x]+e.cost<d[e.to])){ d[e.to]=d[x]+e.cost; pre[e.to]=G[x][i]; if(!inq[e.to]){ q.push(e.to); inq[e.to]=1; } } } } if(d[t]==oo)return 0; int x=t,f=oo; while(x!=s){ Edge e=edges[pre[x]]; f=min(f,e.cap-e.flow); x=e.from; } nowflow+=f;nowcost+=d[t]*f; x=t; while(x!=s){ edges[pre[x]].flow+=f; edges[pre[x]^1].flow-=f; x=edges[pre[x]].from; } return 1; } int Mincost(){ int flow=0,cost=0; while(Spfa(flow,cost)){ } return cost; } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ p[i][j]=++totn; } } s=++totn;t=++totn; for(int j=1;j<=m;++j){ for(int i=1;i<=n;++i){ int x;scanf("%d",&x); for(int k=1;k<=m;++k){ Addedge(p[i][k],totn+j,1,k*x); } } } for(int i=1;i<=n*m;++i)Addedge(s,i,1,0); for(int i=totn+1;i<=totn+m;++i)Addedge(i,t,1,0); totn+=m; printf("%.2f\n",Mincost()*1.0/m); return 0; }
致歉:笔者已经意识到这是一篇几乎没有价值的文章,给您的阅读带来不好的体验,并且干扰了您的搜索环境,非常抱歉!