Optimal Milking
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Problem 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.
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.
* 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
PKU
和上一题基本一个思路额~~
本来想着能不能直接建边不用floyd 后来觉得不行啊~~
#include <cstring>
#include <cstdio>
#include <queue>
#define MAXN 5000
#define MAXM 500000
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
int u,v,f,c;
};
node edge[MAXM*3];
int first[MAXN],next[MAXM*3];
int gap[MAXN],d[MAXN],curedge[MAXN],pre[MAXN];
int cc;
inline void add_edge(int u,int v,int f,int c)
{
edge[cc].u=u;
edge[cc].v=v;
edge[cc].f=f;
edge[cc].c=c;
next[cc]=first[u];
first[u]=cc;
cc++;
edge[cc].u=v;
edge[cc].v=u;
edge[cc].f=0;
edge[cc].c=0;
next[cc]=first[v];
first[v]=cc;
cc++;
}
int ISAP(int s,int t,int n)
{
int cur_flow,flow_ans=0,u,tmp,neck,i,v;
memset(d,0,sizeof(d));
memset(gap,0,sizeof(gap));
memset(pre,-1,sizeof(pre));
for(i=0;i<=n;i++)
curedge[i]=first[i];
gap[0]=n+1;
u=s;
while(d[s]<=n)
{
if(u==t)
{
cur_flow=inf;
for(i=s;i!=t;i=edge[curedge[i]].v)
{
if(cur_flow>edge[curedge[i]].f)
{
neck=i;
cur_flow=edge[curedge[i]].f;
}
}
for(i=s;i!=t;i=edge[curedge[i]].v)
{
tmp=curedge[i];
edge[tmp].f-=cur_flow;
edge[tmp^1].f+=cur_flow;
}
flow_ans+=cur_flow;
u=neck;
}
for(i=curedge[u];i!=-1;i=next[i])
{
v=edge[i].v;
if(edge[i].f&&d[u]==d[v]+1)
break;
}
if(i!=-1)
{
curedge[u]=i;
pre[v]=u;
u=v;
}
else
{
if(0==--gap[d[u]])
break;
curedge[u]=first[u];
for(tmp=n+5,i=first[u];i!=-1;i=next[i])
if(edge[i].f)
tmp=min(tmp,d[edge[i].v]);
d[u]=tmp+1;
++gap[d[u]];
if(u!=s)
u=pre[u];
}
}
return flow_ans;
}
void build(int limit,int cnt,int m)
{
int i;
for(i=0;i<cnt;i=i+2)
{
edge[i].f=m;
edge[i^1].f=0;
}
for(i=cnt;i<cc;i=i+2)
{
edge[i].f=(edge[i].c<=limit);
edge[i^1].f=0;
}
}
int dist[300][300];
void Floyd(int n)
{
int i,j,k;
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(dist[i][k]!=inf&&dist[k][j]!=inf&&
dist[i][j]>dist[i][k]+dist[k][j])
{
dist[i][j]=dist[i][k]+dist[k][j];
}
}
}
}
}
int main()
{
int k,c,m;
while(scanf("%d%d%d",&k,&c,&m)!=EOF)
{
memset(first,-1,sizeof(first));
memset(next,-1,sizeof(next));
cc=0;
int i,j;
int s=0,t=k+c+1;
for(i=1;i<=k;i++)
add_edge(i,t,m,0);
int cnt1=cc;
for(i=1;i<=k+c;i++)
{
for(j=1;j<=k+c;j++)
{
scanf("%d",&dist[i][j]);
if(dist[i][j]==0)
dist[i][j]=inf;
}
}
Floyd(k+c);
for(i=k+1;i<=k+c;i++)
{
for(j=1;j<=k;j++)
add_edge(i,j,inf,dist[i][j]);
}
//int cnt2=cc;
for(i=k+1;i<=k+c;i++)
add_edge(s,i,1,0);
int l=0,r=400000;
int mid,ans=inf;
while(l<=r)
{
mid=(l+r)>>1;
build(mid,cnt1,m);
int res=ISAP(s,t,t);
if(res<c)
l=mid+1;
else if(res>=c)
{
ans=min(ans,mid);
r=mid-1;
}
}
printf("%d\n",ans);
}
return 0;
}