POJ 2122 Optimal Milking

Source: Internet
Author: User

Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 14040 Accepted: 5065
Case Time Limit: 1000MS

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

Test instructions: There are K milking machine C-head cows, K milking machines location with the number 1 to K, the cow position is indicated by the k+1 to the k+c number. The input is in the form of a matrix, where the diagonal line represents a distance of 0 from its own position, and the other 0 positions indicate that no edges are connected to each other. Ask how to arrange each cow to milking a milking device, so that the C-head cows need to walk the maximum distance of the smallest.

Analysis: The Floyd algorithm is used to find the shortest path between any two points, then the maximum flow is obtained by using dinic, and the dichotomy is used to determine the minimum value at the maximum distance of the search.

#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include < algorithm> #include <cmath> #define INF 9999999#define N 333using namespace Std;int k,m,c;int n;int dis[n][n];// Record the shortest path between two points int mp[n][n];//capacity network int qu[n*100];int vis[n];//tag array int sign[n][n];//hierarchy network void build_map (int x) {memset (mp,0,    sizeof MP);    for (int i=k+1;i<=n;i++) mp[0][i]=1;    for (int i=1;i<=k;i++) mp[i][n+1]=m;        for (int i=k+1;i<=n;i++) for (int j=1;j<=k;j++) {if (dis[i][j]<=x) mp[i][j]=1;    }}int BFS ()//build Hierarchy Network {memset (vis,0,sizeof vis);    memset (sign,0,sizeof sign);    int qs=0,qe=1;    Vis[0]=1;    qu[0]=0;        while (QS&LT;QE) {int v=qu[qs++];                for (int i=0;i<=n+1;i++) {if (vis[i]==0 && mp[v][i]) {vis[i]=1;                Qu[qe++]=i;            Sign[v][i]=1;    }}} if (Vis[n+1]) return 1; else return 0;} int DFS (int V,int sum)//dfs look for the augmented path {if (v==n+1) return sum;    int s=sum;    int t;            for (int i=0;i<=n+1;i++) {if (Sign[v][i]) {T=dfs (I,min (Sum,mp[v][i]));            mp[v][i]-=t;            mp[i][v]+=t;        sum-=t; }} return s-sum;}        int main () {while (~scanf ("%d%d%d", &k,&c,&m)) {n=k+c;                for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {scanf ("%d", &dis[i][j]);            if (dis[i][j]==0) Dis[i][j]=inf; } for (int t=1;t<=n;t++)//floyd solves the shortest path for (int i=1;i<=n;i++) {if (dis[i][t]            !=inf) for (int j=1;j<=n;j++) dis[i][j]=min (Dis[i][j],dis[i][t]+dis[t][j]);        } int le=0,ri=10009;        int ans;            while (Le<ri)//dichotomy find the smallest maximum value {int mid= (LE+RI)/2;            ans=0; Build_map (mid);//dinic algorithm to find the maximum flow
            while (BFS ())) Ans+=dfs (0,inf);            if (ans>=c) Ri=mid;            else le=mid+1;        }        printf ("%d\n", RI);//The smallest mid-satisfying condition is used to update RI    }    return 0;}








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2122 Optimal Milking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.