Poj21__optimal milking (maximum network flow dinic + minimum circuit flody + two points)

Source: Internet
Author: User

Solution report

There are K milking machines and C-headed cows on the farm. The distance between each cow and each milking machine is different. Each milking machine can squeeze a maximum of M milk per day.

Find a solution to arrange for each cow to milk a certain milking machine, so that the maximum and minimum values of all the routes that C cows need to take.

To make every cow milk, it is necessary to infer whether the model is full.

Because it is a multi-source multi-point network, if the source point is 0, the sink point N + 1 (n = K + C)

The capacity from the source point to each cow is 1, and the capacity from each machine to the sink point is M. Use flody to find the shortest path from a cow to a random machine;

The minimum value of the maximum distance can be obtained in binary notation.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define inf 99999999#define K 33#define C 210using namespace std;int k,c,m,mmap[K+C][K+C],l[K+C],edge[K+C][K+C],n;void flody(){    for(int l=1; l<=n; l++)    {        for(int i=1; i<=n; i++)        {            for(int j=1; j<=n; j++)            {                if(mmap[i][j]>mmap[i][l]+mmap[l][j])                    mmap[i][j]=mmap[i][l]+mmap[l][j];            }        }    }}int bfs(){    memset(l,-1,sizeof(l));    queue<int>Q;    Q.push(0);    l[0]=0;    while(!Q.empty())    {        int u=Q.front();        Q.pop();        for(int i=0; i<=n+1; i++)        {            if(edge[u][i]&&l[i]==-1)            {                l[i]=l[u]+1;                Q.push(i);            }        }    }    if(l[n+1]>0)return 1;    else return 0;}void G(int mid){    int i,j;    memset(edge,0,sizeof(edge));    for(i=1; i<=k; i++)        edge[i][n+1]=m;    for(i=k+1; i<=n; i++)        edge[0][i]=1;    for(i=k+1; i<=n; i++)    {        for(j=1; j<=k; j++)        {            if(mmap[i][j]<=mid)                edge[i][j]=1;            else edge[i][j]=0;        }    }}int dfs(int x,int f){    int a;    if(x==n+1)return f;    for(int i=0; i<=n+1; i++)    {        if(edge[x][i]&&l[i]==l[x]+1&&(a=dfs(i,min(f,edge[x][i]))))        {            edge[x][i]-=a;            edge[i][x]+=a;            return a;        }    }    return 0;}int dinic(int mid){    int ans=0,a;    G(mid);    while(bfs())        while(a=dfs(0,inf))            ans+=a;    return ans;}int main(){    int i,j;    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",&mmap[i][j]);                if(!mmap[i][j])                    mmap[i][j]=inf;            }        }        flody();        int L=0,R=20000;        while(L<R)        {            int mid=(L+R)/2;            int ans=dinic(mid);            if(ans>=c)R=mid;            else L=mid+1;        }        printf("%d\n",R);    }    return 0;}




Optimal milking
Time limit:2000 ms   Memory limit:30000 K
Total submissions:11664   Accepted:4238
Case time limit:1000 ms

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 mathematical 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 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0

Sample output

2


Poj21__optimal milking (maximum network flow dinic + minimum circuit flody + two points)

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.