BZOJ1070:[SCOI2007]修车——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1070
https://www.luogu.org/problemnew/show/P2053#sub
同一时刻有N位车主带着他们的爱车来到了汽车维修中心。维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的。现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均等待的时间最小。
说明:顾客的等待时间是指从他把车送至维修中心到维修完毕所用的时间。
请转至:http://www.cnblogs.com/luyouqi233/p/7953480.html (POJ3686)
所以省选考了道原题emmmm……
#include<cstdio> #include<iostream> #include<queue> #include<cstring> #include<algorithm> #include<cctype> using namespace std; typedef long long ll; const int INF=1e9; const int N=70; const int M=10; const int P=N+M*N+2; inline int read(){ int X=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=getchar();} while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } struct node{ int nxt,to,w,b; }edge[(N+N*N*M+N*M)*2]; int head[P],cnt=-1; void add(int u,int v,int w,int b){ cnt++; edge[cnt].to=v; edge[cnt].w=w; edge[cnt].b=b; edge[cnt].nxt=head[u]; head[u]=cnt; return; } int dis[P]; bool vis[P]; inline bool spfa(int s,int t,int n){ deque<int>q; memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++)dis[i]=INF; dis[t]=0;q.push_back(t);vis[t]=1; while(!q.empty()){ int u=q.front(); q.pop_front();vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].nxt){ int v=edge[i].to; int b=edge[i].b; if(edge[i^1].w&&dis[v]>dis[u]-b){ dis[v]=dis[u]-b; if(!vis[v]){ vis[v]=1; if(!q.empty()&&dis[v]<dis[q.front()]){ q.push_front(v); }else{ q.push_back(v); } } } } } return dis[s]<INF; } int ans=0; int dfs(int u,int flow,int m){ if(u==m){ vis[m]=1; return flow; } int res=0,delta; vis[u]=1; for(int e=head[u];e!=-1;e=edge[e].nxt){ int v=edge[e].to; int b=edge[e].b; if(!vis[v]&&edge[e].w&&dis[u]-b==dis[v]){ delta=dfs(v,min(edge[e].w,flow-res),m); if(delta){ edge[e].w-=delta; edge[e^1].w+=delta; res+=delta; ans+=delta*b; if(res==flow)break; } } } return res; } inline int costflow(int S,int T,int n){ while(spfa(S,T,n)){ do{ memset(vis,0,sizeof(vis)); dfs(S,INF,T); }while(vis[T]); } return ans; } int main(){ memset(head,-1,sizeof(head)); cnt=-1; int m=read(),n=read(); int S=n+m*n+1,T=n+m*n+2; for(int i=1;i<=n;i++){ add(S,i,1,0); add(i,S,0,0); for(int j=1;j<=m;j++){ int z=read(); for(int k=1;k<=n;k++){ int p=j*n+k; add(i,p,1,z*k); add(p,i,0,-z*k); } } } for(int i=n+1;i<=n+n*m;i++){ add(i,T,1,0); add(T,i,0,0); } printf("%.2f\n",costflow(S,T,n+m*n+2)*1.0/n); return 0; }
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++