http://poj.org/problem?id=2112 (题目链接)
题意
有K个能挤M头奶牛的挤奶机和C头奶牛,告诉一些挤奶机和奶牛间距离,求最优分配方案使最大距离最小。
Solution
先Floyd跑出两两点之间的最短距离,二分答案,最大流。
细节
注意距离不超过200是Floyd之前两点之间的距离不超过200。
代码
// poj2112 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<cmath> #include<queue> #define LL long long #define inf 10000000 #define Pi acos(-1.0) #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); using namespace std; const int maxn=500010; struct edge {int to,next,w;}e[maxn<<1]; int head[maxn],d[maxn],f[300][300]; int n,m,cnt=1,es,et,C,K,M,ans; void link(int u,int v,int w) { e[++cnt]=(edge){v,head[u],w};head[u]=cnt; e[++cnt]=(edge){u,head[v],w};head[v]=cnt; } void Floyd() { for (int k=1;k<=K+C;k++) for (int i=1;i<=K+C;i++) for (int j=1;j<=K+C;j++) f[i][j]=min(f[i][j],f[i][k]+f[k][j]); } bool bfs() { memset(d,-1,sizeof(d)); queue<int> q;q.push(es);d[es]=0; while (!q.empty()) { int x=q.front();q.pop(); for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) { d[e[i].to]=d[x]+1; q.push(e[i].to); } } return d[et]>0; } int dfs(int x,int f) { if (x==et || f==0) return f; int w,used=0; for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) { w=dfs(e[i].to,min(e[i].w,f-used)); used+=w; e[i].w-=w;e[i^1].w+=w; if (used==f) return used; } if (!used) d[x]=-1; return used; } bool Dinic(int mid) { int ans=0;memset(head,0,sizeof(head)); cnt=1; for (int i=K+1;i<=K+C;i++) { for (int j=1;j<=K;j++) if (f[i][j]<=mid) link(i,j,1); link(es,i,1); } for (int i=1;i<=K;i++) link(i,et,M); while (bfs()) ans+=dfs(es,inf); return ans==C; } int main() { scanf("%d%d%d",&K,&C,&M); es=K+C+1;et=es+1; for (int i=1;i<=K+C;i++) for (int j=1;j<=K+C;j++) { scanf("%d",&f[i][j]); if (f[i][j]==0) f[i][j]=inf; } Floyd(); int L=0,R=60000,res; while (L<=R) { int mid=(L+R)>>1; if (Dinic(mid)) res=mid,R=mid-1; else L=mid+1; } printf("%d",res); return 0; }
时间: 2024-10-09 23:10:17