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
题意:有C头奶牛和K台挤奶机,已知每台挤奶机只能给M头牛挤奶。奶牛编号从K+1 到 K+C,挤奶机编号从1 到 K。我们把奶牛和挤奶机看作K+C个点,现在给你一个 (K + C) * (K + C)的矩阵,矩阵第 i 行 第 j 列 的元素代表第i个点到第j个点的距离(当值为0的时候表示没有路)。 如果要给一头奶牛挤奶,则需要这头奶牛走到挤奶机(任意一个都行)的位置。现在让你设计一种方案:安排给每头奶牛挤奶的,使得C头奶牛需要行走的路程中的最大路程最小。
和这道题思路一模一样
代码
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
const int MAXN = 500;
const int MAXM = 100000+10;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
struct Edge {
int form,to,cap,flow,nexts;
}edge[MAXM];
int head[MAXN],top;
void init(){
memset(head,-1,sizeof(head));
top=0;
}
void addedge(int a,int b,int c){
Edge e={a,b,c,0,head[a]};
edge[top]=e;head[a]=top++;
Edge ee={b,a,0,0,head[b]};
edge[top]=ee;head[b]=top++;
}
int K,C,M;
int S,T;
int mp[MAXN][MAXN];
void floyd(){ // 求最短路
for(int k=1;k<=K+C;k++){
for(int i=1;i<=K+C;i++){
if(mp[i][k]==inf) continue;
for(int j=1;j<=K+C; j++)
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
}
}
void Read(){
int a,b;
for(int i=1;i<=K+C;i++){
for(int j=1;j<=K+C;j++){
scanf("%d",&a);
mp[i][j]=a;
if(a==0) mp[i][j]=inf;
}
}
floyd();
}
void getmap(int mid){
S=0,T=K+C+1;
for(int i=1;i<=K+C;i++){
if(i<=K)
addedge(i,T,M);
else
addedge(S,i,1);
}
for(int i=K+1;i<=K+C;i++){
for(int j=1;j<=K;j++){
if(mp[i][j]<=mid)
addedge(i,j,1);
}
}
}
int vis[MAXN],dis[MAXN];
int cur[MAXN];
bool bfs(int st,int ed){
queue<int>Q;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
Q.push(st);vis[st]=1;dis[st]=1;
while(!Q.empty()){
int now=Q.front();Q.pop();
for(int i=head[now];i!=-1;i=edge[i].nexts){
Edge e=edge[i];
if(!vis[e.to]&&e.cap-e.flow>0){
vis[e.to]=1;
dis[e.to]=dis[now]+1;
if(e.to==ed) return 1;
Q.push(e.to);
}
}
}
return 0;
}
int dfs(int now,int a,int ed){
if(a==0||now==ed) return a;
int flow=0,f;
for(int &i=cur[now];i!=-1;i=edge[i].nexts){
Edge &e=edge[i];
if(dis[e.to]==dis[now]+1&&(f=dfs(e.to,min(e.cap-e.flow,a),ed))>0){
e.flow+=f;
flow+=f;
edge[i^1].flow-=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int max_flow(int st ,int ed){
int flow=0;
while(bfs(st,ed)){
memcpy(cur,head,sizeof(head));
flow+=dfs(st,inf,ed);
}
return flow;
}
void solve(){
int l=0,r=inf;
int ans=inf;
while(l<=r){
int mid=(l+r)>>1;
init();
getmap(mid);
if(max_flow(S,T)==C){
r=mid-1; ans=mid;
}else l=mid+1;
}
if(ans == inf) puts("-1");
else printf("%d\n",ans);
}
int main(){
// fread();
// fwrite();
while(scanf("%d%d%d",&K,&C,&M)!=EOF){
Read();
solve();
}
return 0;
}