【题目链接】 http://poj.org/problem?id=2112

【题目大意】

  给出一些挤奶器,每台只能供给M头牛用,牛和挤奶器之间有一定的距离
  现在要让每头牛都挤奶,同时最小化牛到挤奶器的距离,求最小距离

【题解】

  首先用floyd计算出牛和挤奶器之间的距离,
  我们二分最小答案,然后利用二分图检验是否可行,对于M匹配这个条件可以将挤奶器拆点。

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int MAX_V=1000;
const int INF=0x3f3f3f3f;
int V,match[MAX_V];
vector<int> G[MAX_V];
bool used[MAX_V];
void add_edge(int u,int v){
G[u].push_back(v);
G[v].push_back(u);
}
bool dfs(int v){
used[v]=1;
for(int i=0;i<G[v].size();i++){
int u=G[v][i],w=match[u];
if(w<0||!used[w]&&dfs(w)){
match[v]=u;
match[u]=v;
return 1;
}
}return 0;
}
int bipartite_matching(){
int res=0;
memset(match,-1,sizeof(match));
for(int v=0;v<V;v++){
if(match[v]<0){
memset(used,0,sizeof(used));
if(dfs(v))res++;
}
}return res;
}
const int MAX_N=240;
int K,C,M;
int mp[MAX_N][MAX_N];
void construct_graph(int lim){
V=C+K*M;
for(int i=0;i<=V;i++)G[i].clear();
for(int i=1;i<=C;i++){
for(int j=1;j<=K;j++){
if(mp[K+i][j]<=lim){
for(int t=1;t<=M;t++){
add_edge(i,C+(j-1)*M+t);
}
}
}
}
}
int init(){
for(int i=1;i<=K+C;i++)
for(int j=1;j<=K+C;j++){
scanf("%d",&mp[i][j]);
if(mp[i][j]==0)mp[i][j]=INF;
}
for(int k=1;k<=K+C;k++)
for(int i=1;i<=K+C;i++)
for(int j=1;j<=K+C;j++)
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
void solve(){
int ans=1,l=1,r=200*(K+C);
while(l<=r){
int mid=(l+r)>>1;
construct_graph(mid);
if(bipartite_matching()==C)ans=mid,r=mid-1;
else l=mid+1;
}printf("%d\n",ans);
}
int main(){
while(~scanf("%d%d%d",&K,&C,&M)){
init();
solve();
}return 0;
}

POJ 2112 Optimal Milking(二分图匹配)的更多相关文章

  1. Poj 2112 Optimal Milking (多重匹配+传递闭包+二分)

    题目链接: Poj 2112 Optimal Milking 题目描述: 有k个挤奶机,c头牛,每台挤奶机每天最多可以给m头奶牛挤奶.挤奶机编号从1到k,奶牛编号从k+1到k+c,给出(k+c)*(k ...

  2. POJ 2112 Optimal Milking (二分+最短路径+网络流)

    POJ  2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K To ...

  3. POJ 2112 Optimal Milking (二分 + floyd + 网络流)

    POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...

  4. POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】

    Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Sub ...

  5. POJ 2112 Optimal Milking (二分 + 最大流)

    题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...

  6. POJ 2112 Optimal Milking (Dinic + Floyd + 二分)

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 19456   Accepted: 6947 ...

  7. poj 2112 Optimal Milking (二分图匹配的多重匹配)

    Description FJ has moved his K ( <= K <= ) milking machines <= C <= ) cows. A ..K; the c ...

  8. POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)

    题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远   输入数据: 第一行三个数 K, C, M  接下来是   ...

  9. POJ 2112 Optimal Milking(最大流)

    题目链接:http://poj.org/problem?id=2112 Description FJ has moved his K (1 <= K <= 30) milking mach ...

随机推荐

  1. [codeforces/gym/100431/E]KMP关于border的理解

    题目链接:http://codeforces.com/gym/100431/ 考虑到对于一个串β,能cover它的最短的α必然是它的border的某个前缀,或者是这个β本身. 所谓border,就是n ...

  2. Educational Codeforces Round 58 (Rated for Div. 2) 题解

    Educational Codeforces Round 58 (Rated for Div. 2)  题目总链接:https://codeforces.com/contest/1101 A. Min ...

  3. libusb 示例

    #include <usb.h> #include <stdio.h> #define VERSION "0.1.0" #define VENDOR_ID ...

  4. mycat 管理MySQL5.7主从搭建

    1.首先安装MySQL ab: 192.168.6.163 master 192.168.6.167 slave master: vi /etc/opt/rh/rh-mysql57/my.cnf.d/ ...

  5. ntpq –p命令

    ntpq用来监视ntpd操作,ntpq -p查询网络中的NTP服务器,同时显示客户端和每个服务器的关系 [root@ ~]# ntpq -p remote refid st t when poll r ...

  6. bzoj3223 文艺平衡树 codevs3303 翻转区间

    splay模版题吧 只有区间翻转 至于为什么要把须翻转区间旋到根 因为查找一个区间可以先找出他左端点左边第一个点和右端点x右边第一个点y 然后将x旋到根节点 y旋到x的右儿子 这样x的右边的点就是所有 ...

  7. webstorm vue代码修改后不更新问题

    把 safe write 的勾去掉就行了.

  8. BZOJ1037 DP

    2013-11-15 21:51 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1037 拿到这道题想到了DP,后来发现三维无法确定的表示状 ...

  9. 使用腾讯云 GPU 学习深度学习系列之二:Tensorflow 简明原理【转】

    转自:https://www.qcloud.com/community/article/598765?fromSource=gwzcw.117333.117333.117333 这是<使用腾讯云 ...

  10. Mac-WIFI总是断网

    $ cd /Library/Preferences/SystemConfiguration/ 删除如下文件 com.apple.airport.preferences.plist com.apple. ...