Poj 2112 Optimal Milking【Floyd+二分+最大流Dinic】
2016-07-31 16:59
661 查看
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
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
题目大意:有k个机器,有c头牛,每台机器最多给m个牛挤奶,每头牛都要选择一个机器去挤奶,问所有牛都有选择的情况下,最小的最远距离为多少。
输入 k,c,m
然后接下来k+c*k+c的一个邻接矩阵。
思路:
1、首先Floyd求一下两点间最短路。
2、然后对于这个最远距离我们二分求出。
3、然后针对这个二分当前值mid进行最大流判定:
①首先将k个机器连入汇点T,其权值为m,表示每台机器可以给m头牛挤奶。
②源点S,连接各个牛,其权值为1,表示这是一头牛。
③如果牛到机器的距离小于等于mid,那么建立一条从牛到机器的边,权值为1,表示这头牛可以到这个机器来挤奶。
4、然后建立好图之后,跑一遍Dinic,如果最大流==k,那么就说明当前情况是一个可行解,记录ans。二分结束之后,输出这个ans
Ac代码:
K,C输入反了,大家看的时候注意一下。
#iclude<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
int head[15500];
int div[15500];
int cur[15500];
struct node
{
int from;
int to;
int w;
int next;
}e[1500000];
int map[500][500];
int c,k,m,n,ss,tt,cont;
void add(int from,int to,int w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void getmap(int mid)
{
ss=n+1;
tt=ss+1;
cont=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=c;i++)
{
add(i,tt,m);
add(tt,i,0);
}
for(int i=c+1;i<=n;i++)
{
add(ss,i,1);
add(i,ss,0);
}
for(int i=c+1;i<=n;i++)
{
for(int j=1;j<=c;j++)
{
if(map[i][j]<=mid)
{
add(i,j,1);
add(j,i,0);
}
}
}
}
int makediv()
{
memset(div,0,sizeof(div));
queue<int >s;
s.push(ss);
div[ss]=1;
while(!s.empty())
{
int u=s.front();
if(u==tt)return 1;
s.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(w&&div[v]==0)
{
div[v]=div[u]+1;
s.push(v);
}
}
}
return 0;
}
int Dfs(int u,int maxflow,int tt)
{
if(u==tt)return maxflow;
int ret=0;
for(int &i=cur[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(w&&div[v]==div[u]+1)
{
int f=Dfs(v,min(maxflow-ret,w),tt);
e[i].w-=f;
e[i^1].w+=f;
ret+=f;
if(ret==maxflow)return ret;
}
}
return ret;
}
int Dinic(int mid)
{
getmap(mid);
int ans=0;
while(makediv()==1)
{
memcpy(cur,head,sizeof(head));
ans+=Dfs(ss,INF,tt);
}
if(ans==k)return 1;
else return 0;
}
int main()
{
while(~scanf("%d%d%d",&c,&k,&m))
{
for(int i=1;i<=c+k;i++)
{
for(int j=1;j<=c+k;j++)
{
scanf("%d",&map[i][j]);
if(i!=j&&map[i][j]==0)
map[i][j]=0x3f3f3f3f;
}
}
n=c+k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
map[j][k]=min(map[j][i]+map[i][k],map[j][k]);
}
}
}
int ans,mid;
int l=0;
int r=10000;
while(r-l>=0)
{
mid=(l+r)/2;
if(Dinic(mid)==1)
{
r=mid-1;
ans=mid;
}
else l=mid+1;
}
printf("%d\n",ans);
}
}
Time Limit: 2000MS | | Memory Limit: 30000K |
Total Submissions: 16151 | | Accepted: 5788 |
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
Source
USACO 2003 U S Open
题目大意:有k个机器,有c头牛,每台机器最多给m个牛挤奶,每头牛都要选择一个机器去挤奶,问所有牛都有选择的情况下,最小的最远距离为多少。
输入 k,c,m
然后接下来k+c*k+c的一个邻接矩阵。
思路:
1、首先Floyd求一下两点间最短路。
2、然后对于这个最远距离我们二分求出。
3、然后针对这个二分当前值mid进行最大流判定:
①首先将k个机器连入汇点T,其权值为m,表示每台机器可以给m头牛挤奶。
②源点S,连接各个牛,其权值为1,表示这是一头牛。
③如果牛到机器的距离小于等于mid,那么建立一条从牛到机器的边,权值为1,表示这头牛可以到这个机器来挤奶。
4、然后建立好图之后,跑一遍Dinic,如果最大流==k,那么就说明当前情况是一个可行解,记录ans。二分结束之后,输出这个ans
Ac代码:
K,C输入反了,大家看的时候注意一下。
#iclude<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
int head[15500];
int div[15500];
int cur[15500];
struct node
{
int from;
int to;
int w;
int next;
}e[1500000];
int map[500][500];
int c,k,m,n,ss,tt,cont;
void add(int from,int to,int w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void getmap(int mid)
{
ss=n+1;
tt=ss+1;
cont=0;
memset(head,-1,sizeof(head));
for(int i=1;i<=c;i++)
{
add(i,tt,m);
add(tt,i,0);
}
for(int i=c+1;i<=n;i++)
{
add(ss,i,1);
add(i,ss,0);
}
for(int i=c+1;i<=n;i++)
{
for(int j=1;j<=c;j++)
{
if(map[i][j]<=mid)
{
add(i,j,1);
add(j,i,0);
}
}
}
}
int makediv()
{
memset(div,0,sizeof(div));
queue<int >s;
s.push(ss);
div[ss]=1;
while(!s.empty())
{
int u=s.front();
if(u==tt)return 1;
s.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(w&&div[v]==0)
{
div[v]=div[u]+1;
s.push(v);
}
}
}
return 0;
}
int Dfs(int u,int maxflow,int tt)
{
if(u==tt)return maxflow;
int ret=0;
for(int &i=cur[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
int w=e[i].w;
if(w&&div[v]==div[u]+1)
{
int f=Dfs(v,min(maxflow-ret,w),tt);
e[i].w-=f;
e[i^1].w+=f;
ret+=f;
if(ret==maxflow)return ret;
}
}
return ret;
}
int Dinic(int mid)
{
getmap(mid);
int ans=0;
while(makediv()==1)
{
memcpy(cur,head,sizeof(head));
ans+=Dfs(ss,INF,tt);
}
if(ans==k)return 1;
else return 0;
}
int main()
{
while(~scanf("%d%d%d",&c,&k,&m))
{
for(int i=1;i<=c+k;i++)
{
for(int j=1;j<=c+k;j++)
{
scanf("%d",&map[i][j]);
if(i!=j&&map[i][j]==0)
map[i][j]=0x3f3f3f3f;
}
}
n=c+k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
map[j][k]=min(map[j][i]+map[i][k],map[j][k]);
}
}
}
int ans,mid;
int l=0;
int r=10000;
while(r-l>=0)
{
mid=(l+r)/2;
if(Dinic(mid)==1)
{
r=mid-1;
ans=mid;
}
else l=mid+1;
}
printf("%d\n",ans);
}
}
相关文章推荐
- poj 2112 Optimal Milking(floyd+二分+最大流)
- 【POJ- 2112】Optimal Milking 【floyd+二分+最大流】
- POJ 2112--Optimal Milking【二分找最大距离的最小值 && 最大流dinic】
- POJ 2112 —— Optimal Milking 二分+Floyd+最大流
- POJ 2112 Optimal Milking (Floyd+二分+最大流)
- POJ 2112 Optimal Milking (Floyd+二分+最大流)
- poj 2112 Optimal Milking(floyd+二分+最大流)
- HLG 1392 (POJ 2112 Optimal Milking)【二分+二分图多重匹配 + 最大流】
- POJ2112_Optimal Milking(网洛流最大流Dinic+最短路Flody+二分)
- poj 2112 Optimal Milking, 二分,最短路,最大流
- POJ 2112 Optimal Milking 最大流 二分答案
- POJ 2112 Optimal Milking (二分+最大流/多重匹配) && POJ 2391 Ombrophobic Bovines( 二分+拆点+最大流)
- POJ 2112 Optimal Milking (二分 + 最大流)
- POJ 2112 Optimal Milking 二分最大流
- 【最大流+最短路+二分】POJ-2112 Optimal Milking
- Optimal Milking POJ - 2112 floyd+二分+最大流
- POJ 2112 Optimal Milking 二分图多重匹配 网络流+Floyd+二分
- POJ 2112 Optimal Milking (二分+最短路+最大流or二分图多重匹配)
- POJ 2112 floyd+二分最大流
- poj 2112 Optimal Milking(最大费用流+dinic算法+二分搜索)