POJ 2112-optimal Milking (Network flow _ maximum Flow +floyd+ Two-point search)

Source: Internet
Author: User

Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 12810 Accepted: 4632
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 machines, C cows, each milking every day can squeeze m head cows. Next K+c line, each line has k+c number (for example, the label 1-2 is the milking device, the label 3-5 for the Cow), so the first line represents 1th distance 1th milking device distance is 0, the distance from the second milking device is 3, the distance from the 3rd cow is 2, the distance from the 4th cow is 1, The distance from cow number 5th is 1. (PS: English slag looked for a long time to understand test instructions). The minimum distance required for all cows to reach the milking device.

Idea: To say that the network flow is the most important to build a diagram, first to the input diagram with Floyd to find the minimum path, and then found the maximum edge between the cow and Milking Maxx, from the "0,maxx" to find the largest edge, according to the largest side of the re-map, and then use the maximum flow to determine whether the edge is not exceeding the

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include < algorithm> #include <map> #include <set> #include <queue>using namespace Std;const int inf= 0x3f3f3f3f;int head[310],num[310],d[310],pre[310],q[310],cur[310];int dp[310][310];int n,m,cnt,s,t,nv,c;int maxint        =inf;struct node {int u, V, cap; int next;}        edge[1000000];void Add (int u, int v, int cap) {edge[cnt].v=v;        Edge[cnt].cap=cap;        Edge[cnt].next=head[u];        head[u]=cnt++;        Edge[cnt].v=u;        Edge[cnt].cap=0;        EDGE[CNT].NEXT=HEAD[V]; head[v]=cnt++;}        void Floyd () {int I, j, K;                                For (k=1, k<=n; k++) for (I=1, i<=n; i++) for (j=1; j<=n; j + +) Dp[i][j]=min (Dp[i][j],dp[i][k]+dp[k][j]);}        void BFs () {memset (num,0,sizeof (num));        Memset (d,-1,sizeof (d));        int f1=0, f2=0, I;        d[t]=0;    Num[0]=1;    q[f1++]=t;                while (F1&GT;=F2) {int u=q[f2++];                        for (i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;                                if (d[v]==-1) {d[v]=d[u]+1;                                num[d[v]]++;                        Q[f1++]=v;        }}}}int Isap () {memcpy (cur,head,sizeof (cur));        BFS ();        int flow=0, u=pre[s]=s, I;                        while (D[S]&LT;NV) {if (u==t) {int f=maxint, POS;                                        for (i=s; i!=t; i=edge[cur[i]].v) {if (F>edge[cur[i]].cap) {                                        F=edge[cur[i]].cap;                                Pos=i; }} for (i=s; i!=t; i=edge[cur[i]].v) {Edge[c                                Ur[i]].cap-=f; EdGe[cur[i]^1].cap+=f;                        } flow+=f;                U=pos; } for (i=cur[u]; i!=-1; i=edge[i].next) {if (d[edge[i].v]+1==d[u]&&edge[i].ca                        p) {break;                        }} if (I!=-1) {cur[u]=i;                        Pre[edge[i].v]=u;                U=EDGE[I].V;                        } else {if (--num[d[u]]==0) break;                        int MIND=NV;                                        for (i=head[u]; i!=-1; i=edge[i].next) {if (Mind>d[edge[i].v]&&edge[i].cap) {                                        MIND=D[EDGE[I].V];                                Cur[u]=i;                        }} d[u]=mind+1;                        num[d[u]]++;             U=pre[u];   }} return flow;}        int main () {int k, m, I, J, X, ans;                while (~SCANF ("%d%d%d", &k,&c,&m)) {n=k+c; for (I=1; i<=n; i++) {for (j=1; j<=n; J + +) {scanf ("%d", &AMP;DP I                                [j]);                        if (dp[i][j]==0) Dp[i][j]=maxint;                }} Floyd ();                int low=1,high=40000, Mid;                        while (Low<=high) {mid= (High+low)/2;                        s=0;                        t=n+1;                        nv=t+1;                        memset (head,-1,sizeof (head));                        cnt=0;                        for (I=1; i<=k; i++) Add (s,i,m);                        for (I=1; i<=c; i++) Add (k+i,t,1);             for (I=1; i<=k; i++)                   for (j=1; j<=c; J + +) {if (Dp[i][k+j]<=mid)                                Add (i,k+j,1);                        } X=ISAP ();                                if (x>=c) {ans=mid;                        High=mid-1;                        } else if (x<c) {low=mid+1;        }} printf ("%d\n", ans); } return 0;}


POJ 2112-optimal Milking (Network flow _ maximum Flow +floyd+ Two-point search)

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.