POJ 2112 floyd+二分最大流

Optimal Milking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 10739   Accepted: 3899
Case Time Limit: 1000MS

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 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 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 2
0 3 2 1 1
3 0 3 2 0
2 3 0 1 0
1 2 1 0 2
1 0 0 2 0

Sample Output

2
与POJ 2391 一样,
代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014-1-25 14:49:49
File Name :3.cpp
************************************************ */

#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef int ll;
const ll inf=10000000;
const ll maxn=2000;
ll head[maxn],tol,dep[maxn];
struct node 
{
      ll from,to,next,cap;
      node(){};
      node(ll from,ll to, ll next,ll cap):from(from),to(to),next(next),cap(cap){}
}edge[1000000];
void add(ll u,ll v,ll cap)
{
      edge[tol]=node(u,v,head[u],cap);
      head[u]=tol++;
      edge[tol]=node(v,u,head[v],0);
      head[v]=tol++;
}
bool bfs(ll s,ll t)
{
       ll que[maxn],front=0,rear=0;
       memset(dep,-1,sizeof(dep));
       dep[s]=0;que[rear++]=s;
       while(front!=rear)
       {
              ll u=que[front++];front%=maxn;
              for(ll i=head[u];i!=-1;i=edge[i].next)
              {
                     ll v=edge[i].to;
                     if(edge[i].cap>0&&dep[v]==-1)
                     {
                            dep[v]=dep[u]+1;
                            que[rear++]=v;
                            rear%=maxn;
                            if(v==t)return 1;
                     }
              }
       }
       return 0;
}

ll dinic(ll s,ll t)
{
      ll res=0;
      while(bfs(s,t))
      {
	     ll Stack[maxn],top,cur[maxn];
	     memcpy(cur,head,sizeof(head));
	     top=0;
	     ll u=s;
	     while(1)
	     {
		    if(t==u)
		    {
			   ll min=inf;
			   ll loc;
			   for(ll i=0;i<top;i++)
		          if(min>edge[Stack[i]].cap)
			   {
				  min=edge[Stack[i]].cap;
				  loc=i;
			   }
			   for(ll i=0;i<top;i++)
			   {
				  edge[Stack[i]].cap-=min;
				  edge[Stack[i]^1].cap+=min;
			   }
			   res+=min;
			   top=loc;
                        u=edge[Stack[top]].from;
		    }
		    for(ll i=cur[u];i!=-1;cur[u]=i=edge[i].next)
			   if(dep[edge[i].to]==dep[u]+1&&edge[i].cap>0)break;
		    if(cur[u]!=-1)
		    {
			   Stack[top++]=cur[u];
			   u=edge[cur[u]].to;
		    }
		    else 
		    {
			   if(top==0)break;
			   dep[u]=-1;
			   u=edge[Stack[--top]].from;
		    }
	     }
      }
      return res;
}

ll dist[1000][1000];
int main()
{
      int K,C,M,i,j,k,m,n;
      while(cin>>K>>C>>M)
      {
	     ll left=0,right,mid;
           for(i=1;i<=K+C;i++)
		   for(j=1;j<=K+C;j++)
		    {
                      cin>>dist[i][j];
			 if(dist[i][j]==0)
				dist[i][j]=inf;
		    }
	   for(k=1;k<=K+C;k++)
	   {
		  for(i=1;i<=K+C;i++)
		  {
			 for(j=1;j<=K+C;j++)
			 {
				if(i==j||i==k||j==k)continue;
                             if(dist[i][j]>dist[i][k]+dist[k][j])
				  dist[i][j]=dist[i][k]+dist[k][j];
			 }
				
		  }
	   }
	    right=300000;
	    while(left<right)
	    {
		   mid=(left+right)>>1;
		   memset(head,-1,sizeof(head));tol=0;
		   for(i=1;i<=K;i++)
		   for(j=K+1;j<=K+C;j++)
			  if(dist[i][j]<=mid)
				 add(i,j,1);
		   for(i=K+1;i<=K+C;i++)
			  add(i,K+C+1,1);
		   for(i=1;i<=K;i++)add(0,i,M);
		   ll pp=dinic(0,K+C+1);
		  // cout<<"tol="<<tol<<"mid="<<mid<<" "<<"pp="<<pp<<endl;
		   if(pp==C)right=mid;
		   else left=mid+1;
	    }
	    cout<<left<<endl;
      }
      return 0;
}


 

POJ 2112 floyd+二分最大流

上一篇:UVa 11077 Find the Permutations / 置换


下一篇:当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'users' 中的标识列插入显式值