poj 2112 Optimal Milking(floyd+二分+最大流)
2016-06-03 15:07
393 查看
Optimal Milking
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
Sample Output
题意:f台取奶机,c只奶牛,每台取奶机可以同时给m只奶牛取奶
取奶机与奶牛之间两两有一个距离,走过去需要时间,问你让所有奶牛取到奶,最少要多久?
思路:用floyd跑一遍求出两点间的最短距离,然后上界是两点间最大距离+1(最好初始权值也要比一遍),然后二分,每次把小于mid的边加进来,缩小范围,最后的mid就是最短距离。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 550
#define INF 1<<28
struct Edge
{
int to,next,cap;
} edge[N*N];
int cnt,head
,d
;
int now
,can
;
int maxn,dp
;
void addedge(int from,int to,int cap)
{
edge[cnt].to=to;
edge[cnt].cap=cap;
edge[cnt].next=head[from];
head[from]=cnt++;
edge[cnt].to=from;
edge[cnt].cap=0;
edge[cnt].next=head[to];
head[to]=cnt++;
}
int bfs(int s,int t)
{
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==-1&&edge[i].cap>0)
{
d[v]=d[u]+1;
q.push(v);
}
}
}
return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
if(s==t||f==0) return f;
int flow=0;
for(int i=head[s]; i!=-1&&flow<f; i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==d[s]+1&&edge[i].cap>0)
{
int x=min(f-flow,edge[i].cap);
x=dfs(v,t,x);
flow+=x;
edge[i].cap-=x;
edge[i^1].cap+=x;
if(f==0) break;
}
}
if(!flow) d[s]=-2;
return flow;
}
int Dinic(int s,int t)///起点s终点t
{
int flow=0,f;
while(bfs(s,t))
{
while(f=dfs(s,t,INF))
flow+=f;
}
return flow;
}
void floyd(int n)
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(dp[i][k]==0||dp[k][j]==0) continue;
if(dp[i][j]==0||dp[i][j]>dp[i][k]+dp[k][j])
{
dp[i][j]=dp[i][k]+dp[k][j];
maxn=max(maxn,dp[i][j]);
}
}
}
int main()
{
int k,c,m,t;
while(~scanf("%d %d %d",&k,&c,&m))
{
int S=0,T=k+c+2;
int sum=c;
maxn=-INF;
for(int i=1; i<=k+c; i++)
for(int j=1; j<=k+c; j++)
{
scanf("%d",&dp[i][j]);
maxn=max(maxn,dp[i][j]);
}
floyd(k+c);
int low=0,high=maxn+1,mid,ans=-1;
while(low<high)
{
mid=(low+high)/2;
cnt=0;
memset(head,-1,sizeof(head));
for(int i=1; i<=k; i++)
addedge(i,T,m);
for(int i=k+1; i<=k+c; i++)
addedge(S,i,1);
for(int i=k+1; i<=k+c; i++)
for(int j=1; j<=k; j++)
if(dp[i][j]!=0&&dp[i][j]<=mid)
addedge(i,j,INF);
if(Dinic(S,T)==sum) ans=high=mid;
else low=mid+1;
}
printf("%d\n",ans);
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 15753 | Accepted: 5617 | |
Case Time Limit: 1000MS |
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
题意:f台取奶机,c只奶牛,每台取奶机可以同时给m只奶牛取奶
取奶机与奶牛之间两两有一个距离,走过去需要时间,问你让所有奶牛取到奶,最少要多久?
思路:用floyd跑一遍求出两点间的最短距离,然后上界是两点间最大距离+1(最好初始权值也要比一遍),然后二分,每次把小于mid的边加进来,缩小范围,最后的mid就是最短距离。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define N 550
#define INF 1<<28
struct Edge
{
int to,next,cap;
} edge[N*N];
int cnt,head
,d
;
int now
,can
;
int maxn,dp
;
void addedge(int from,int to,int cap)
{
edge[cnt].to=to;
edge[cnt].cap=cap;
edge[cnt].next=head[from];
head[from]=cnt++;
edge[cnt].to=from;
edge[cnt].cap=0;
edge[cnt].next=head[to];
head[to]=cnt++;
}
int bfs(int s,int t)
{
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==-1&&edge[i].cap>0)
{
d[v]=d[u]+1;
q.push(v);
}
}
}
return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
if(s==t||f==0) return f;
int flow=0;
for(int i=head[s]; i!=-1&&flow<f; i=edge[i].next)
{
int v=edge[i].to;
if(d[v]==d[s]+1&&edge[i].cap>0)
{
int x=min(f-flow,edge[i].cap);
x=dfs(v,t,x);
flow+=x;
edge[i].cap-=x;
edge[i^1].cap+=x;
if(f==0) break;
}
}
if(!flow) d[s]=-2;
return flow;
}
int Dinic(int s,int t)///起点s终点t
{
int flow=0,f;
while(bfs(s,t))
{
while(f=dfs(s,t,INF))
flow+=f;
}
return flow;
}
void floyd(int n)
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
{
if(dp[i][k]==0||dp[k][j]==0) continue;
if(dp[i][j]==0||dp[i][j]>dp[i][k]+dp[k][j])
{
dp[i][j]=dp[i][k]+dp[k][j];
maxn=max(maxn,dp[i][j]);
}
}
}
int main()
{
int k,c,m,t;
while(~scanf("%d %d %d",&k,&c,&m))
{
int S=0,T=k+c+2;
int sum=c;
maxn=-INF;
for(int i=1; i<=k+c; i++)
for(int j=1; j<=k+c; j++)
{
scanf("%d",&dp[i][j]);
maxn=max(maxn,dp[i][j]);
}
floyd(k+c);
int low=0,high=maxn+1,mid,ans=-1;
while(low<high)
{
mid=(low+high)/2;
cnt=0;
memset(head,-1,sizeof(head));
for(int i=1; i<=k; i++)
addedge(i,T,m);
for(int i=k+1; i<=k+c; i++)
addedge(S,i,1);
for(int i=k+1; i<=k+c; i++)
for(int j=1; j<=k; j++)
if(dp[i][j]!=0&&dp[i][j]<=mid)
addedge(i,j,INF);
if(Dinic(S,T)==sum) ans=high=mid;
else low=mid+1;
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- 常用Docker命令
- Hadoop2.6.0版本MapReudce示例之WordCount(二)
- Hadoop2.6.0版本MapReudce示例之WordCount(一)
- linux 查看系统版本信息
- 本地测试web项目部署到服务器空间出错
- centos7配置VSFTPD
- 内存溢出之Tomcat内存配置
- 系统监控命令
- 服务器安装linux后,网卡位置和系统内的编号乱了怎么解决?
- 安装openresty的脚本
- centos7 关闭firewall安装iptables并配置
- shell脚本语言学习笔记-3
- 菜鸟学linux之四Linux文件与目录管理第一节
- Linux服务器搭建之<PHP>(三)
- HDFS源码分析数据块复制监控线程ReplicationMonitor(二)
- VIM 配置ctags/cscope 函数跳转
- 搭建Kafka运行环境
- HDFS源码分析数据块复制监控线程ReplicationMonitor(一)
- centos服务器异常断电mysql启动异常启动Mysql时报错:mysqld_safe mysqld from pid file /usr/local/mysql/Linux.pid end
- Squid安装和反向代理配置简明指南