Optimal Milking (poj 2112 网络流+二分+floyd)

Language:

Optimal Milking

Time Limit:2000MSMemory Limit:30000K

Total Submissions:12951Accepted:4688

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 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0

Sample Output

2

Source

题意:农场有K个挤奶器和C头奶牛,每个挤奶器和奶牛都在不同的位置,挤奶器编号1~K,奶牛编号K+1~K+C,邻接矩阵给出它们之间的距离,每台挤奶器每天最多能为M头牛挤奶。寻找一个方案,安排每头奶牛到某个挤奶器挤奶,,并使得C头奶牛需要走的路程中的最大路程最小。

思路:先用floyd算法求出能到达的任意两点之间的最短距离,然后dinic算法求最大流,搜索最大距离的最小值采用二分法。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 1005#define MAXN 1010#define MAXM 20010#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FRL(i,a,b) for(i = a; i < b; i++)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n)scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pfprintf#define DBGpf("Hi\n")typedef long long ll;using namespace std;struct Edge{int to,next,cap,flow;}edge[MAXM];int tol,k,n,m,c;int head[MAXN];int mp[MAXN][MAXN];void init(){tol=2;memset(head,-1,sizeof(head));}void addedge(int u,int v,int w,int rw=0){edge[tol].to=v;edge[tol].cap=w;edge[tol].flow=0;edge[tol].next=head[u];head[u]=tol++;edge[tol].to=u;edge[tol].cap=rw;//双向边时改成wedge[tol].flow=0;edge[tol].next=head[v];head[v]=tol++;}int Q[MAXN];int dep[MAXN],cur[MAXN],sta[MAXN];bool bfs(int s,int t,int n){int front=0,tail=0;memset(dep,-1,sizeof(dep[0])*(n+1));dep[s]=0;Q[tail++]=s;while (front<tail){int u=Q[front++];for (int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if (edge[i].cap>edge[i].flow && dep[v]==-1){dep[v]=dep[u]+1;if (v==t) return true;Q[tail++]=v;}}}return false;}int dinic(int s,int t,int n){int maxflow=0;while (bfs(s,t,n)){for (int i=0;i<n;i++) cur[i]=head[i];int u=s,tail=0;while (cur[s]!=-1){if (u==t){int tp=INF;for (int i=tail-1;i>=0;i–)tp=min(tp,edge[sta[i]].cap-edge[sta[i]].flow);maxflow+=tp;for (int i=tail-1;i>=0;i–){edge[sta[i]].flow+=tp;edge[sta[i]^1].flow-=tp;if (edge[sta[i]].cap-edge[sta[i]].flow==0)tail=i;}u=edge[sta[tail]^1].to;}else if (cur[u]!=-1 && edge[cur[u]].cap > edge[cur[u]].flow &&dep[u]+1==dep[edge[cur[u]].to]){sta[tail++]=cur[u];u=edge[cur[u]].to;}else{while (u!=s && cur[u]==-1)u=edge[sta[–tail]^1].to;cur[u]=edge[cur[u]].next;}}}return maxflow;}void floyd() //floyd求最短路{int i,j,k;FRE(k,1,n){FRE(i,1,n){if (mp[i][k]!=INF){FRE(j,1,n)mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);}}}}void Build_Graph(int mid) //建图(邻接表){int i,j;init();FRE(i,k+1,n)addedge(0,i,1);FRE(i,k+1,n)FRE(j,1,k)if (mp[i][j]<=mid)addedge(i,j,1);FRE(i,1,k)addedge(i,n+1,m);}bool ok(int mid) //二分判断{Build_Graph(mid);int ans=dinic(0,n+1,n+2);if (ans>=c) return true;return false;}void solve() //二分{int i,j;int l=0,r=10000;while (l<r){int mid=(l+r)>>1;if (ok(mid)) r=mid;else l=mid+1;}printf("%d\n",r);}int main(){int i,j;while (~sfff(k,c,m)){n=k+c;FRE(i,1,n)FRE(j,1,n){sf(mp[i][j]);if (mp[i][j]==0) mp[i][j]=INF;}floyd();solve();}return 0;}/*2 3 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0*/

你曾经说,等我们老的时候,

Optimal Milking (poj 2112 网络流+二分+floyd)

相关文章:

你感兴趣的文章:

标签云: