POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】
Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Description
Each milking point can "process" at most M (1 <= M <= 15) cows each day.
Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.
Input
* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.
Output
Sample Input
2 3 2
0 3 2 1 1
3 0 3 2 0
2 3 0 1 0
1 2 1 0 2
1 0 0 2 0
Sample Output
2 题目大意:给你K个挤奶点,C头牛,每个挤奶点能最多挤K头牛。下面是矩阵,行和列都表示K个挤奶点,C头牛。矩阵A(i,j)表示i到j的距离。距离都为正值,如果出现0,则表示不直接连通。数据保证有解。问你让这m头牛都能挤奶的条件下,最远的牛最少要走多远。 解题思路:二分枚举距离,每次根据枚举的距离,重新构图。每个挤奶点的匹配次数已知。但是这个题目有一点比较坑,就是二分枚举的时候,r应该从最大值INF开始,因为200只是两点之间的直接距离,floyd之后,可能会出现大于200的距离,应该注意。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
const int INF = 9999999;
const int maxn = 1010;
int Map[maxn][maxn];
int linker[maxn][maxn], used[maxn];
int M;
bool dfs(int u,int rn){
for(int v = 1; v <= rn; v++){
if(used[v] || !Map[u][v]){
continue;
}
used[v] = 1;
if(linker[v][0] < M){
linker[v][++linker[v][0]] = u;
return true;
}else{
for(int j = 1; j <= linker[v][0]; j++){
if(dfs(linker[v][j],rn)){
linker[v][j] = u;
return true;
}
}
}
}
return false;
}
bool Hungary(int ln,int rn){
int ret = 0;
for(int i = 0; i <= rn; i++){
linker[i][0] = 0;
}
for(int i = 1; i <= ln; i++){
memset(used,0,sizeof(used));
if(dfs(i,rn)){
ret++;
}
}
if(ln == ret){
return true;
}
return false;
}
int main(){
int K, C;
int matrix[500][500];
while(scanf("%d%d%d",&K,&C,&M)!=EOF){
int nn = K + C;
for(int i = 1; i <= nn; i++){
for(int j = 1; j <= nn; j++){
scanf("%d",&matrix[i][j]);
if(matrix[i][j] == 0){
matrix[i][j] = INF;
}
}
}
for(int k = 1; k <= nn; k++){
for(int i = 1; i <= nn; i++){
for(int j = 1; j <= nn; j++){
if(matrix[i][j] > matrix[i][k] + matrix[k][j]){
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
}
int l = 1, r = INF, ans;
while(l <= r){ //不会写二分,错了n多次 ORZ
int mid = (l+r)/2;
memset(Map,0,sizeof(Map));
for(int i = K + 1; i <= nn; i++){
for(int j = 1; j <= K; j++){
if(matrix[i][j] <= mid){
Map[i-K][j] = 1;
}
}
}
if(Hungary(C,K)){
r = mid - 1;
ans = mid;
}else{
l = mid + 1;
}
}
printf("%d\n",l);
}
return 0;
}
POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】的更多相关文章
- Poj 2112 Optimal Milking (多重匹配+传递闭包+二分)
题目链接: Poj 2112 Optimal Milking 题目描述: 有k个挤奶机,c头牛,每台挤奶机每天最多可以给m头奶牛挤奶.挤奶机编号从1到k,奶牛编号从k+1到k+c,给出(k+c)*(k ...
- POJ 2112 Optimal Milking 最短路 二分构图 网络流
题意:有C头奶牛,K个挤奶站,每个挤奶器最多服务M头奶牛,奶牛和奶牛.奶牛和挤奶站.挤奶站和挤奶站之间都存在一定的距离.现在问满足所有的奶牛都能够被挤奶器服务到的情况下,行走距离的最远的奶牛的至少要走 ...
- POJ 2112 Optimal Milking【网络流+二分+最短路】
求使所有牛都可以被挤牛奶的条件下牛走的最长距离. Floyd求出两两节点之间的最短路,然后二分距离. 构图: 将每一个milking machine与源点连接,边权为最大值m,每个cow与汇点连接,边 ...
- POJ 2112 Optimal Milking(最大流+二分)
题目链接 测试dinic模版,不知道这个模版到底对不对,那个题用这份dinic就是过不了.加上优化就WA,不加优化TLE. #include <cstdio> #include <s ...
- 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+多重匹配+二分枚举)
题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 输入数据: 第一行三个数 K, C, M 接下来是 ...
- poj 2112 Optimal Milking (二分图匹配的多重匹配)
Description FJ has moved his K ( <= K <= ) milking machines <= C <= ) cows. A ..K; the c ...
- POJ 2112 Optimal Milking(二分+最大流)
http://poj.org/problem?id=2112 题意: 现在有K台挤奶器和C头奶牛,奶牛和挤奶器之间有距离,每台挤奶器每天最多为M头奶挤奶,现在要安排路程,使得C头奶牛所走的路程中的最大 ...
随机推荐
- AttributeError: ‘module’ object has no attribute ‘ximgproc’(OpenCV)
问题描述: 使用opecv实现选择性搜索(selective search)的时候,执行如下代码时报了上述标题的错误. “ss = cv2.ximgproc.segmentation.createSe ...
- Install Open vSwitch on CentOS
转载:http://cloud-mate.org/2015/06/installing-open-vswitch-centos-cloudstack/ June 5, 2015 Stuart Ne ...
- ubuntu17.04安装flash
因为用不了软件商店(别问我为什么) 所以手动安装 1 下载文件 在firefox下下载 *****.tar.gz 压缩包 ,并解压(一般目录在 /home 当前用户下的 下载目录下) adobe官网 ...
- 静态-static
静态 1. 静态初始化块 (1)可初始化类的静态字段 (2)静态初始化块只调用一次 (3)创建子类时会调用父类的静态初始化块 源代码: class Root { static{ System.out. ...
- iOS中Info.plist文件的常见配置
. 在创建一个新的Xcode工程后,会 在Supporting Files文件夹下自动生成一个工程名-Info.plist的文件,这个是对工程做一些运行期配置的文件(很重要,必须有该文件). 如果使用 ...
- jQuery的ajax实现文件上传大小限制
用jquery的ajax实现简单的文件上传功能,并且限制文件大小,先上代码. <!DOCTYPE html> <html> <head> <meta char ...
- P3380 【模板】二逼平衡树(树套树) 线段树套平衡树
\(\color{#0066ff}{ 题目描述 }\) 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上 ...
- Qt 学习之路 2(19):事件的接受与忽略
Home / Qt 学习之路 2 / Qt 学习之路 2(19):事件的接受与忽略 Qt 学习之路 2(19):事件的接受与忽略 豆子 2012年9月29日 Qt 学习之路 2 140条评论 ...
- react 部分语法补充
jsx语法 todolist.js import React, { Component,Fragment } from 'react'; import './style.css' class Todo ...
- asp:FileUpload 控件上传多文件
<asp:FileUpload runat="server" ID="imgUpload" AllowMultiple="true" ...