首页 技术 正文
技术 2022年11月18日
0 收藏 817 点赞 4,489 浏览 1712 个字

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;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902