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

Optimal Milking

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

Submit Status Practice POJ 2112

Description

FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C.

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

* Line 1: A single line with three space-separated integers: K, C, and M.

* 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

A single line with a single integer that is the minimum possible total distance for the furthest walking cow.

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

  

				
时间: 2024-12-26 06:03:21

POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】的相关文章

POJ 2112 Optimal Milking 最优挤奶方案 Floyd算法+二分查找+最大流

题目链接:POJ 2112 Optimal Milking Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12446   Accepted: 4494 Case Time Limit: 1000MS Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among

POJ 2112 Optimal Milking 二分答案+最大流

首先二分最长的边,然后删去所有比当前枚举的值长的边,算最大流,看是否能满足所有的牛都能找到挤奶的地方 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

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

POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C≤200)头奶牛,在奶牛和挤奶器之间有一组不同长度的路.K个挤奶器的位置用1-K的编号标明,奶牛的位置用K+1-K+C 的编号标明.每台挤奶器每天最多能为M(1≤M≤15)头奶牛挤奶.寻找一个方案,安排每头奶牛到某个挤奶器挤奶,并使得C 头奶牛需要走的所有路程中的最大路程最小.每个测试数据中至少有一

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

POJ 2112 Optimal Milking 题目链接 题意:给定一些机器和奶牛,在给定距离矩阵,(不在对角线上为0的值代表不可达),每个机器能容纳m个奶牛,问所有奶牛都能挤上奶,那么走的距离最大的奶牛的最小值是多少 思路:明显的二分+最大流,注意floyd求出的距离矩阵最大值可能不止200,所以二分的上限要注意 代码: #include <cstdio> #include <cstring> #include <queue> #include <algori

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

Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named

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

题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 奶的情况下,走的最远的那头奶牛走的距离最小是多少. 数据保证有解. 算法讨论: 首先可以想到是二分,然后在选择流网络的时候,一开始选择的最小费用最大流,让二分的边权充当最小费用,但是这样跑发现每次二分的是我们要跑的答案,不可行.所以就改用最大流. 最大流肯定是在二分的情况下判定最大流是否等于c,即

POJ 2112: Optimal Milking【二分,网络流】

题目大意:K台挤奶机,C个奶牛,每台挤奶器可以供M头牛使用,给出奶牛和和机器间的距离矩阵,求所有奶牛走最大距离的最小值 思路:最大距离的最小值,明显提示二分,将最小距离二分之后问题转化成为:K台挤奶机,C个奶牛,每台挤奶器可以供M头牛使用,已知每头牛可以到的挤奶机是哪些,问能否让所有奶牛挤上奶. 这个问题就是典型的二分图多重匹配问题,跑个网络流看是否满流即可,最后才发现给出的矩阵不一定是最短路径TUT 所以要跑一遍floyd #include<iostream> #include<cst

POJ 2112 Optimal Milking(最大流)

题目链接:http://poj.org/problem?id=2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The

POJ 2112 Optimal Milking

Optimal Milking Time Limit: 2000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 211264-bit integer IO format: %lld      Java class name: Main FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures amon