Optimal Milking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 17811   Accepted: 6368
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

Source

——————————————————————————————————

题目给出n头牛和m台机器的两两距离,0表示走不通,和每台机器最多容纳的牛数量,问每头牛都去一台机器,最远的牛到机器的最小距离

思路:先floyd跑出两两之间最短距离,在二分最远距离+二分图多重匹配||最大流验证

二分图多重匹配:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
const int MAXN=1005;
int uN,vN; //u,v数目
int g[MAXN][MAXN];
int linker[MAXN][MAXN];
bool used[MAXN];
int linknum[MAXN];
int cap[MAXN];
int mp[MAXN][MAXN];
int N; bool dfs(int u,int mid)
{
int v;
for(v=1; v<=vN; v++)
if(mp[u][v]<=mid&&!used[v])
{
used[v]=true;
if(linknum[v]<cap[v])
{
linker[v][++linknum[v]]=u;
return true;
}
for(int i=1; i<=cap[v]; i++)
if(dfs(linker[v][i],mid))
{
linker[v][i]=u;
return true;
}
}
return false;
} int hungary(int mid)
{
int res=0;
int u;
memset(linknum,0,sizeof linknum);
memset(linker,-1,sizeof linker);
for(u=vN+1; u<=N; u++)
{
memset(used,0,sizeof used);
if(dfs(u,mid)) res++;
}
return res;
} void floyd(){
for(int k = 1; k <= N; ++k){
for(int i = 1; i <= N; ++i){
for(int j = 1; j <= N; ++j){
mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}
}
}
}
int main()
{
int n,m,k;
while(~scanf("%d%d%d",&vN,&uN,&k))
{
N=uN+vN;
int mx=-1;
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
{
scanf("%d",&mp[i][j]);
mx=max(mx,mp[i][j]);
if(mp[i][j]==0)
mp[i][j]=INF;
} floyd();
for(int i=1; i<=vN; i++)
cap[i]=k;
int l=0,r=INF;
int ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(hungary(mid)==uN) ans=mid,r=mid-1;
else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
} 最大流 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 500 struct node
{
int u, v, next, cap;
} edge[MAXN*MAXN];
int nt[MAXN], s[MAXN], d[MAXN], visit[MAXN];
int cnt;
int n,m,k;
int mp[MAXN][MAXN];
int N;
void init()
{
cnt = 0;
memset(s, -1, sizeof(s));
} void add(int u, int v, int c)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = s[u];
s[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].cap = 0;
edge[cnt].next = s[v];
s[v] = cnt++;
} bool BFS(int ss, int ee)
{
memset(d, 0, sizeof d);
d[ss] = 1;
queue<int>q;
q.push(ss);
while (!q.empty())
{
int pre = q.front();
q.pop();
for (int i = s[pre]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap > 0 && !d[v])
{
d[v] = d[pre] + 1;
q.push(v);
}
}
}
return d[ee];
} int DFS(int x, int exp, int ee)
{
if (x == ee||!exp) return exp;
int temp,flow=0;
for (int i = nt[x]; ~i ; i = edge[i].next, nt[x] = i)
{
int v = edge[i].v;
if (d[v] == d[x] + 1&&(temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
{
edge[i].cap -= temp;
edge[i ^ 1].cap += temp;
flow += temp;
exp -= temp;
if (!exp) break;
}
}
if (!flow) d[x] = 0;
return flow;
} int Dinic_flow(int mid)
{
init();
for(int i=1; i<=n; i++)
add(0,i,k);
for(int i=n+1; i<=N; i++)
add(i,n+1+m,1);
for(int i=1; i<=n; i++)
for(int j=1+n; j<=N; j++)
if(mp[i][j]<=mid)
add(i,j,1);
int ss=0,ee=m+n+1;
int ans = 0;
while (BFS(ss, ee))
{
for (int i = 0; i <=ee; i++) nt[i] = s[i];
ans+= DFS(ss, INF, ee);
}
return ans;
} void floyd()
{
for(int k = 1; k <= N; ++k)
{
for(int i = 1; i <= N; ++i)
{
for(int j = 1; j <= N; ++j)
{
mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}
}
}
} int main()
{
while(~scanf("%d%d%d",&n,&m,&k))
{
N=m+n;
int mx=-1;
for(int i=1; i<=N; i++)
for(int j=1; j<=N; j++)
{
scanf("%d",&mp[i][j]);
mx=max(mx,mp[i][j]);
if(mp[i][j]==0)
mp[i][j]=INF;
}
floyd(); int l=0,r=INF;
int ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(Dinic_flow(mid)==m) ans=mid,r=mid-1;
else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}

  

POJ2112 Optimal Milking的更多相关文章

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

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

  2. POJ2112 Optimal Milking (网络流)(Dinic)

                                             Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

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

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

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

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

  5. POJ2112 Optimal Milking(最大流)

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

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

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

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

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

  8. POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 20262   Accepted: 7230 ...

  9. Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏

    Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 13968 Accepted: 5044 Case ...

随机推荐

  1. PHP——explode的应用(获取字符串,拆为下拉列表)

    <?php //定义有默认值的函数 function Main3($f=5,$g=6) { echo $f*$g; } Main3(2,3); echo "<br />&q ...

  2. TCP Nagle算法&&延迟确认机制

    TCP Nagle算法&&延迟确认机制 收藏 秋风醉了 发表于 3年前 阅读 1367 收藏 0 点赞 0 评论 0 [腾讯云]买域名送云解析+SSL证书+建站!>>> ...

  3. Kali xrdp远程桌面

    发现论坛没有该教程,在这里分享给需要的基友.源还是要更新的,楼主在网上百度的kali源,而不是linux源,比163.搜狐的源好些.首先安装xrdp: apt-get install xrdp 复制代 ...

  4. Porsche PIWIS TESTER III

    Allscanner VXDIAG Porsche Piwis III with Lenovo T440P Laptop  Porsche Piwis tester III V37.250.020 N ...

  5. 发送邮件--MFMailComposeViewController

    只能在真机使用. 模拟器没有E-mail发送功能.无法调用 #import "EmailViewController.h" #import <UIKit/UIKit.h> ...

  6. Luogu 3620 数据备份 - Set

    Solution 很显然, 最优情况肯定是相邻两个相连 . 然后模型就跟 Luogu1484 类似了. 把两个房子 看成一个坑 (参考 Luogu1484), 选取 $k$ 个不相邻的坑, 使得权值最 ...

  7. 2016年3月31日_应化所群体Review

    Review目的: Phonegap的ble插件可以接收到设备发送的信息,但接收并在控制台显示的数据夏一鸣不知道是正确的数据,还是由于编码解码问题导致的乱码问题.此次Review要解决的问题即判断接收 ...

  8. 斐波那契数列(NOIP1997)

    题目链接:斐波那契数列 这题是数论的一个基本应用,还是很水,因为数据范围太水了,只有48,这也太小了.不过也有可能是当时的电脑速度跑得比较慢的原因.但是这个算法应该还是这个算法.主要思路就是递推求斐波 ...

  9. linux_添加一个普通用户

    useradd 用户名 passwd 密码 su 用户名 可以切换用户 exit 返回之前登录的用户 sodu用户 --> 不用告诉普通用户root的密码 可以查看所有的系统文件 包括root下 ...

  10. idea运行项目时报Error:java无效的源发行版:1.8

    如果你安装的是JDK1.7,而在file->project structure中设置的是language level是8的话,就会出现这个错误提示:无效的源发行版:8. 解决办法:将语言级别改为 ...