Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏
Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 13968 Accepted: 5044
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
USACO 2003 U S Open
题意:有c头奶牛和k个挤奶器,奶牛之间,挤奶器之间,奶牛与挤奶器之间都有一定的距离,问使c头奶牛都能挤奶的最大距离的最小值;
做法:距离问题,怎样知道任意点之间的距离,而且是最短距离,最短路算法floyd.
怎样求最大距离的最小值:由于数据量比较大,不能枚举,采用二分;
要确定二分的值是不是正确,就要用Dinic算法求出最大流看看是否>=c
#include <map>
#include <list>
#include <climits>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout)
const int Max = 300;
int Map[Max][Max];
int Dis[Max][Max];
bool vis[Max];
bool sign[Max][Max];
int K,c,m;
int n;
void BuildGraph(int MaxNum)//建立残留网络
{
memset(Map,0,sizeof(Map));
for(int i=K+1;i<=n;i++)
{
Map[0][i]=1;//将每一头奶牛与源点建立弧,对于每一头奶牛只去一个挤奶器
}
for(int i=1;i<=K;i++)
{
Map[i][n+1]=m;//将每个挤奶器与汇点建立弧,对于每个挤奶器最多接受m头奶牛
}
for(int i=K+1;i<=n;i++)
{
for(int j=1;j<=K;j++)
{
if(Dis[i][j]<=MaxNum)//限制条件
{
Map[i][j]=1;
}
}
}
}
bool BFS()//Dinic算法,建立层次网络;
{
memset(vis,false,sizeof(vis));
memset(sign,false,sizeof(sign));
int a;
queue<int>Q;
vis[0]=true;
Q.push(0);
while(!Q.empty())
{
a=Q.front();
Q.pop();
for(int i=0;i<=n+1;i++)
{
if(!vis[i]&&Map[a][i])
{
vis[i]=true;
sign[a][i]=true;
Q.push(i);
}
}
}
if(vis[n+1])//当标记不到汇点的时候说明残留网络中没有增广路,已经求得最大流,算法结束
{
return true;
}
return false;
}
int DFS(int s,int num)//
{
if(s==n+1)
{
return num;
}
int sum = num;
for(int i=0;i<=n+1;i++)
{
if(sign[s][i])
{
int ans=DFS(i,min(Map[s][i],num));//进行多次的增广,
Map[s][i]-=ans;
Map[i][s]+=ans;
num-=ans;
if(!num)//如果残余容量为零,则不必再增广
{
break;
}
}
}
return sum-num;
}
int main()
{
while(~scanf("%d %d %d",&K,&c,&m))
{
n=K+c;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&Dis[i][j]);
if(!Dis[i][j])
{
Dis[i][j]=INF;
}
}
}
for(int k=1;k<=n;k++)//通过floyd算法计算任意点之间的距离
{
for(int i=1;i<=n;i++)
{
if(Dis[i][k]!=INF)
{
for(int j=1;j<=n;j++)
{
Dis[i][j]=min(Dis[i][j],Dis[i][k]+Dis[k][j]);
}
}
}
}
int L=0,R=10000;
while(L<R)//通过不断的二分找到满足条件的最小值
{
int mid = (L+R)/2;
int sum = 0;
BuildGraph(mid);
while(BFS())//Dinic算法,直到不能增广时结束
{
sum+=DFS(0,INF);//计算有多少的奶牛挤奶
}
if(sum>=c)
{
R=mid;
}
else
{
L=mid+1;
}
}
printf("%d\n",R);//R就是所要求的最大距离的最小值
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Optimal Milking 分类: 图论 POJ 最短路 查找 2015-08-10 10:38 3人阅读 评论(0) 收藏的更多相关文章
- Pots 分类: 搜索 POJ 2015-08-09 18:38 3人阅读 评论(0) 收藏
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11885 Accepted: 5025 Special Judge D ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1272 小希的迷宫(并查集) 分类: 并查集 2015-07-07 23:38 2人阅读 评论(0) 收藏
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
- Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...
- HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏
Sum Problem Time Limit: 1000/500 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- Python调用C可执行程序(subprocess) 分类: python 服务器搭建 C/C++ shell 2015-04-13 21:03 87人阅读 评论(0) 收藏
从Python 2.4开始,Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system.os.spawn.os.popen.popen2.commands. ...
- /var/log目录下的20个Linux日志文件功能详解 分类: 服务器搭建 linux内核 Raspberry Pi 2015-03-27 19:15 80人阅读 评论(0) 收藏
如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时从容找出问题并加以解决. 以下介绍的 ...
- shell入门之流程控制语句 分类: 学习笔记 linux ubuntu 2015-07-10 16:38 89人阅读 评论(0) 收藏
1.case 脚本: #!/bin/bash #a test about case case $1 in "lenve") echo "input lenve" ...
- IOS第三方数据库--FMDB 分类: ios技术 2015-03-01 09:38 57人阅读 评论(0) 收藏
iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepers ...
随机推荐
- C++之路进阶——bzoj2199(奶牛议会)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser gryz2016 Logout 捐赠本站 Notice:由于本OJ ...
- 关于vptr指针初始化的分步
vptr:一个具有虚函数类的对象所具有的隐藏的成员,指向该类的虚函数表. 父类对象的vptr指向是一直指向父类的.但子类的vptr指针最终是指向子类的, 当子类创建的时候,先调用父类构造函数,这个时候 ...
- mysql之消息队列
消息队列:在消息的传输过程中保存消息的容器. 消息队列管理器在将消息从它的源中继到它的目标时充当中间人.队列的主要目的是提供路由并保证消息的传递:如果发送消息时接收者不可用,消息队列会保留消息,直到可 ...
- HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...
- Session的工作机制详解和安全性问题(PHP实例讲解)
我们先简单的了解一些http的知识,从而理解该协议的无状态特性.然后,学习一些关于cookie的基本操作.最后,我会一步步阐述如何使用一些简单,高效的方法来提高你的php应用程序的安全性以及稳定行. ...
- paper 7:支持向量机系列四:Outliers —— 介绍支持向量机使用松弛变量处理 outliers 方法。
在最开始讨论支持向量机的时候,我们就假定,数据是线性可分的,亦即我们可以找到一个可行的超平面将数据完全分开.后来为了处理非线性数据,使用 Kernel 方法对原来的线性 SVM 进行了推广,使得非线性 ...
- C#.Net GC(garbage Collector) 垃圾回收器
以前一直以为gc的原理很简单,也就是分代处理堆数据,直到我的膝盖中了一箭(好吧 直到有天汪涛和我说他面试携程的面试题 关于服务器和 工作站gc 的区别)其实我当时尚不知道 工作站和服务器有什么区别更不 ...
- 13.熟悉JDK的配置,环境变量
已经做烂的东西,公司的新人环境配置手册文档Java方面的就是我写的,有意的留邮箱,很详细
- ThinkPHP讲解(一)框架基础
ThinkPHP框架知识点过于杂乱,接下来将以问题的形势讲解tp(ThinkPHP的简写) 1.tp框架是什么,为什么使用是它? 一堆代码的集合,里边有变量.函数.类.常量,里边也有许多设计模式MVC ...
- HTML输入框点击内容消失
在input标签中这样写 type='text' onfocus='if(this.value=='请输入内容以搜索') this.value=''' onblur='if(this.value==' ...