POJ 2112 Optimal Milking(二分图匹配)
【题目链接】 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(二分图匹配)的更多相关文章
- Poj 2112 Optimal Milking (多重匹配+传递闭包+二分)
题目链接: Poj 2112 Optimal Milking 题目描述: 有k个挤奶机,c头牛,每台挤奶机每天最多可以给m头奶牛挤奶.挤奶机编号从1到k,奶牛编号从k+1到k+c,给出(k+c)*(k ...
- POJ 2112 Optimal Milking (二分+最短路径+网络流)
POJ 2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS Memory Limit: 30000K To ...
- POJ 2112 Optimal Milking (二分 + floyd + 网络流)
POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...
- POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】
Optimal Milking Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 2112 Optimal Milking (二分 + 最大流)
题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...
- POJ 2112 Optimal Milking (Dinic + Floyd + 二分)
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 19456 Accepted: 6947 ...
- poj 2112 Optimal Milking (二分图匹配的多重匹配)
Description FJ has moved his K ( <= K <= ) milking machines <= C <= ) cows. A ..K; the c ...
- POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)
题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 输入数据: 第一行三个数 K, C, M 接下来是 ...
- POJ 2112 Optimal Milking(最大流)
题目链接:http://poj.org/problem?id=2112 Description FJ has moved his K (1 <= K <= 30) milking mach ...
随机推荐
- hdu Shell Necklace 5730 分治FFT
Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell neckl ...
- C#三层中的分页
最近写了一个winform的管理系统,里面的分页同学推荐了几种,感觉都不好用,比较麻烦,自己就找了一个比较简单的分页,利用数据存储过程来分页. reate proc usp_User@pageInde ...
- HDU2157 How many ways??---(邻接矩阵,图论,矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=2157 How many ways?? Time Limit: 2000/1000 MS (Java/Others ...
- [object-c 2.0 程序设计]object-c file handle (二)
// // main.m // cmdTry // // Created by Calos Chen on 2017/8/21. // Copyright © 2017年 Calos Chen. Al ...
- 类似web风格的 Winform 分页控件
背景 最近做一个Winform的小程序,需要用到分页,由于之前一直在用 TonyPagerForWinForm.dll ,但该库没有源代码,网上找的也不全面,索性就准备自己改造一个.在园子里翻了一下, ...
- 关于jQuery.extend
这次来了解下jQuery的extend吧,作为菜鸟的我学艺不精,看插件时经常看到extend函数的使用,从网上看到一篇不错的介绍,特地转载过来留给自己收藏学习: ------------------- ...
- 包与time,datetime,random,sys,shutil 模块
一.包 包是什么? 包是一种通过使用‘.模块名’来组织python模块名称空间的方式. 注意: 1. 在python3中,即使包下没有__init__.py文件,import 包仍然不会报错,而在py ...
- Codeforces 362E Petya and Pipes 费用流建图
题意: 给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大. 费用流:在某条边增加单位流量的费用. 那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0 同时另一条边 ...
- (12)Linux shell之read 用法
Linux shell之read 用法 #!/bin/bash#read 用来读取屏幕输入或是读取文件内容.read -p "please input you name: " ...
- 两个kernel.org国内镜像
两个kernel.org国内镜像 https://mirror.tuna.tsinghua.edu.cn/kernel/v4.x/testing/ http://mirror.bjtu.edu.cn/ ...