【题目链接】 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. chrome 不支持12px以下字体为题的解决

    现英文9px 设置 在chrome 下无效,可以通过 -webkit-transform: scale(0.75); 12*0.75 =9  得到小字体(在chrome浏览器下 大小缩放到0.75倍) ...

  2. linux bash善用判断式

    1.利用 test 指令的测试功能 $ test -e hello.sh && echo "ok" || echo "no" ok 2.首先,判 ...

  3. 关于 WizTools.org RESTClient的使用

    今天分享一个很好用的测试service的工具,很好用 提供两种方法使用这个东东. 第一种方法 通过cmd命令窗口. (1)cd C:\Users\li_weifeng\Desktop\c4d2(文件存 ...

  4. D. Relatively Prime Graph

    Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E( ...

  5. Tile Cut~网络流入门题

    Description When Frodo, Sam, Merry, and Pippin are at the Green Dragon Inn drinking ale, they like t ...

  6. 用@Component注解代替@Configuration注解,定义bean

    package com.timo.entity; import org.springframework.beans.factory.annotation.Value; import org.sprin ...

  7. 创建 React 项目

    依次输入命令: npm install -g create-react-app create-react-app react16 cd my-app npm start 在浏览器中输入 local:3 ...

  8. nodejs与sqlite

    //打开数据库var db = new sqlite3.Database('xx.db'); // 关闭数据库db.close(); db.run('xx');  // 数据库对象的run函数可以执行 ...

  9. Spring - IoC(6): 作用域

    Spring 支持五种作用域,分别是 singleton.prototype.request.session 和 global session. 作用域 说明  singleton (默认作用域)单例 ...

  10. IntelliJ 创建main函数快捷

    今天偶然发现了IntelliJ中 创建main函数的快捷键,依次还有for循环,System.out.println(); 在编写代码的时候直接输入psv就会看到一个psvm的提示,此时点击tab键一 ...