Optimal Milking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 16461   Accepted: 5911
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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int power(int a,int b,int c){int ans=;while(b){if(b%==){ans=(ans*a)%c;b--;}b/=;a=a*a%c;}return ans;}
int dis[N][N];
int w[N][N];
bool sign[N][N];
bool used[N];
int k,c,n,m;
void Build_Graph(int min_max)
{
memset(w,,sizeof(w));
for(int i=;i<=k;i++)w[][i]=m;
for(int i=k+;i<=n;i++)w[i][n+]=;
for(int i=;i<=k;i++){
for(int j=k+;j<=n;j++){
if(dis[i][j]<=min_max) w[i][j]=;
}
}
}
bool BFS()
{
memset(used,false,sizeof(used));memset(sign,,sizeof(sign));
queue<int>q;
q.push();used[]=true;
while(!q.empty()){
int t=q.front();q.pop();
for(int i=;i<=n+;i++){
if(!used[i]&&w[t][i]){
q.push(i);
used[i]=true;
sign[t][i]=;
}
}
}
if(used[n+])return true;
return false;
}
int DFS(int v,int sum)
{
if(v==n+)return sum;
int s=sum,t;
for(int i=;i<=n+;i++){
if(sign[v][i]){
t=DFS(i,min(w[v][i],sum));
w[v][i]-=t;
w[i][v]+=t;
sum-=t;
}
}
return s-sum;
}
int main()
{
scanf("%d%d%d",&k,&c,&m);
n=k+c;
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&dis[i][j]);
if(!dis[i][j])dis[i][j]=inf;
}
}
for(int k=;k<=n;k++){
for(int i=;i<=n;i++){
if(dis[i][k]!=inf){
for(int j=;j<=n;j++){
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
}
int l=,r=;
while(l<r){
int mid=(l+r)/;
int ans=;
Build_Graph(mid);
while( BFS() )ans+=DFS(,inf);//Dinic求最大流
if(ans>=c) r=mid;
else l=mid+;
}
printf("%d\n",r);
return ;
}

POJ2112 Optimal Milking (网络流)(Dinic)的更多相关文章

  1. POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  2. POJ 2112 Optimal Milking (Dinic + Floyd + 二分)

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 19456   Accepted: 6947 ...

  3. POJ2112 Optimal Milking 【最大流+二分】

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12482   Accepted: 4508 ...

  4. POJ2112 Optimal Milking

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 17811   Accepted: 6368 ...

  5. [Poj2112][USACO2003 US OPEN] Optimal Milking [网络流,最大流][Dinic+当前弧优化]

    题意:有K个挤奶机编号1~K,有C只奶牛编号(K+1)~(C+K),每个挤奶机之多能挤M头牛,现在让奶牛走到挤奶机处,求奶牛所走的最长的一条边至少是多少. 题解:从起点向挤奶机连边,容量为M,从挤奶机 ...

  6. poj2112 Optimal Milking --- 最大流量,二分法

    nx一个挤奶器,ny奶牛,每个挤奶罐为最m奶牛使用. 现在给nx+ny在矩阵之间的距离.要求使所有奶牛挤奶到挤奶正在旅程,最小的个体奶牛步行距离的最大值. 始感觉这个类似二分图匹配,不同之处在于挤奶器 ...

  7. POJ-2112 Optimal Milking(floyd+最大流+二分)

    题目大意: 有k个挤奶器,在牧场里有c头奶牛,每个挤奶器可以满足m个奶牛,奶牛和挤奶器都可以看成是实体,现在给出两个实体之间的距离,如果没有路径相连,则为0,现在问你在所有方案里面,这c头奶牛需要走的 ...

  8. [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)

    http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...

  9. POJ2112 Optimal Milking(最大流)

    先Floyd求牛到机器最短距离,然后二分枚举最长的边. #include<cstdio> #include<cstring> #include<queue> #in ...

随机推荐

  1. OpenCV之响应鼠标(二):函数cvSetMouseCallback()和其副程式onMouse()的使用(OpenCV2.4.5)

    每當滑鼠在視訊視窗介面點擊一下的時候,都會有固定三個動作 1.點擊(Click) 2.放開(Down)3.滑動(move) 因此,程式執行滑鼠在點擊的時候onMouse()都會連續跑三次,代表滑鼠在點 ...

  2. Android中 服务里的方法抽取成接口

    1 写个类继承Service 重写 onBind方法 返回一个IBinder 对象(传递到连接成功时用) 2 服务中 写一个内部类 继承IBinder 并且实现一个接口(用于抽取方法)继承IBinde ...

  3. LINQ基础(一)

    一.学习LINQ需要先了解以下知识点: 1.1 委托 1.2 匿名方法 1.3 Lambda表达式 1.4 扩展方法 二.LINQ原理:      from s in names where s.le ...

  4. poj1276 多重背包

    //Accepted 1100 KB 47 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...

  5. iOS 开发之重力动画效果

    步骤:1.使用single view application创建新的项目 2.在viewcontroller.h文件中创建一个图片实例并与相关图片相连,然后创建一个UIDynamicAnimator ...

  6. PHP、C++的重载

    首先明确一点:PHP重载是用在面向对象的类当中,而不支持函数重载. 这点与C++不一样,在C++当中,重载可以用于面向过程和面向对象,而且方法也不一样. 在C++中,重载适用于当函数名相同时,函数所需 ...

  7. Ubuntu 重新设置网络

    sudo service network-manager stop sudo rm /var/lib/NetworkManager/NetworkManager.state sudo service ...

  8. POJ 3278 经典BFS

    进一步了解了bfs; 题意:给你n,然后用+,-,*三种运算使n变成k; 漏洞:在算出新的数字之后,一定要判边界,否则RE,而且在每一步后面都得加判断是否等于K,如果是即刻退出,否则WA,判这个的时候 ...

  9. 百度VS高德:LBS开发平台ios SDK对比评测

    随着iPhone6手机的热销,目前的iOS应用开发市场也迎来了全盛时期.据了解,目前市面上已有的iOS应用基本覆盖了购物.上门服务.用车服务.娱乐等行业.而在这些iOS应用中,内置LBS服务的应用占大 ...

  10. 批处理中for循环多个%

    实例:@echo offset NUM=10000for /f %%i in (字符.txt) do (set JSZF=%%ifor /L %%. in (0,1,%NUM%) do ( Call ...