Optimal MilkingTime
limit:2000MS
Memory Limit:30000KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticePOJ 2112
Description
FJ has moved he K (1 <= k <=) milking machines out into the cow pastures among the C (1 <= C <=) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations is named by ID numbers 1..K; The cow locations is named by ID numbers k+1..k+c.
Each milking point can "process" at the most M (1 <= m <=) cows each day.
Write a program-to-find an assignment-cow to some milking, so, the distance the furthest-walking cow t Ravels is minimized (and, of course, the milking machines was not overutilized). At least one legal assignment are possible for all input data sets. Cows can traverse several paths on the "to" their milking machine.
Input
* Line 1: A "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 is positive integers no larger than 200. Entities not directly connected by a path has a distance of 0. The distance from a entity to itself (i.e., all numbers in the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+c >, a row was broken into successive lines of numbers and a Potentially shorter line-to-finish up a row. Each new row begins the own line.
Output
A single line with a single integer so is the minimum possible total distance for the furthest walking cow.
Sample Input
2 3 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0
Sample Output
2
The main topic: give you k milking point, C cow, each milking point can be the most crowded K cow. Here are the matrices, rows and columns representing K-milking points, C-Bull. Matrix A (I,J) represents the distance from I to J. The distance is positive, and if 0 is present, it is not directly connected. Data is guaranteed to be solvable. Ask you how far the farthest cow will go at least if you can make the M cow milking.
Problem solving idea: two-minute enumeration distance, each time according to the enumeration distance, re-composition. The number of matches per milking point is known. But this topic has a bit of a pit, that is, when the binary enumeration, R should start from the maximum INF, because 200 is only the direct distance between two points, after Floyd, may appear more than 200 distance, should be noted.
#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)) {L INKER[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) {//will not write two points, wrong n multiple 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 —————— "Multiple matching, binary enumeration answer, Floyd preprocessing"