"Poj2122" Optimal milking

Source: Internet
Author: User

http://poj.org/problem?id=2112 (Topic link)

Test instructions

There are K can squeeze M-head cows milking machine and C-head cows, tell some milking machine and the distance between cows, to find the optimal allocation scheme to minimize the maximum distance.

Solution

First Floyd run out of 22 points between the shortest distance, two answers, maximum flow.

Details

Note that distances not exceeding 200 are Floyd before the distance between two points is less than 200.

Code
poj2112#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring># include<cstdio> #include <cmath> #include <queue> #define LL long long#define inf 10000000#define Pi ACOs ( -1.0) #define FREE (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout); using namespace Std;const int maxn= 500010;struct Edge {int to,next,w;} E[maxn<<1];int head[maxn],d[maxn],f[300][300];int n,m,cnt=1,es,et,c,k,m,ans;void Link (int u,int v,int W) {e[++ Cnt]= (Edge) {v,head[u],w};head[u]=cnt;e[++cnt]= (edge) {u,head[v],w};head[v]=cnt;} void Floyd () {for (Int. k=1;k<=k+c;k++) for (int. i=1;i<=k+c;i++) for (int j=1;j<=k+c;j++) f[i][j]=min (f[i][j],f[i ][K]+F[K][J]);} BOOL BFs () {memset (d,-1,sizeof (d));queue<int> Q;q.push (es);d [Es]=0;while (!q.empty ()) {int X=q.front (); Q.pop () ; for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]<0) {D[e[i].to]=d[x]+1;q.push (e[i].to);}} return d[et]>0;} int dfs (int x,int f) {if (X==et | | f==0) return f;int W,used=0;for (int i=head[x];i;i=e[i].next) if (e[i].w && d[e[i].to]==d[x]+1) {W=dfs (E[i].to,min (e[i].w,f-used)) ; Used+=w;e[i].w-=w;e[i^1].w+=w;if (used==f) return used;} if (!used) D[x]=-1;return used;} BOOL Dinic (int mid) {int Ans=0;memset (head,0,sizeof (head)); cnt=1;for (int i=k+1;i<=k+c;i++) {for (int j=1;j<=k;j+ +) if (f[i][j]<=mid) link (i,j,1); link (es,i,1);} for (int i=1;i<=k;i++) Link (i,et,m), while (BFS ()) Ans+=dfs (Es,inf); return ans==c;} int main () {scanf ("%d%d%d", &k,&c,&m), es=k+c+1;et=es+1;for (int i=1;i<=k+c;i++) for (int j=1;j<=k+c; J + +) {scanf ("%d", &f[i][j]); if (f[i][j]==0) F[i][j]=inf;} Floyd (); int L=0,r=60000,res;while (l<=r) {int mid= (l+r) >>1;if (Dinic (mid)) Res=mid,r=mid-1;else l=mid+1;} printf ("%d", res); return 0;}

  

"Poj2122" 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.