链接:http://poj.org/problem?id=2112
题目:
Optimal Milking
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 11103 | Accepted: 4028 | |
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.
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
解题思路:
这是一道最大流的题目。基本解题思路:floyd() +二分 + 最大流。先用floyd()求出所有点之间的最短距离,这里关键是建图,我们可以把0这个点作为源点,源点与挤奶机点的容量为m,记n = k + c;n + 1这个点为汇点,奶牛点与汇点相连,容量为1,二分走得最远的奶牛走的长度mid,如果挤奶机与奶牛之间的距离d<=mid;容量为1,这样的话,在利用最大流来判断是否符合,最大流等于奶牛的数目的话,说明现在的mid是可行的,所求的答案缩小到[l,mid]范围。ps:这道题我用G++交wa了无数次,用c++交就过了,无语。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <limits.h>
#include <queue>
using namespace std;
#define min(x,y) x < y ? x : y
const int MAXN = 235;
int k, c, m, n, p[MAXN], flow[MAXN][MAXN], map[MAXN][MAXN];
void floyd()
{
for(int k = 1; k <= n; k++)
{
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(map[i][k] < INT_MAX && map[k][j] < INT_MAX)
map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
}
}
}
}
int bfs(int s, int t)
{
queue<int> q; q.push(s);
memset(p, -1, sizeof(p));
while(!q.empty())
{
int u = q.front(); q.pop();
for(int v = 0; v <= t; v++)
{
if(-1 == p[v] && flow[u][v] > 0)
{
p[v] = u; q.push(v);
if(v == t) return 1;
}
}
}
return 0;
}
int ek(int s, int t)
{
int f = 0;
while(bfs(s,t))
{
int inc = INT_MAX;
for(int u = t; u != s; u = p[u])
{
inc = min(inc, flow[p[u]][u]);
}
if(0 == inc) break;
for(int u = t; u != s; u = p[u])
{
flow[p[u]][u] -= inc;
flow[u][p[u]] += inc;
}
f += inc;
}
return f;
}
int main()
{
while(~scanf("%d%d%d", &k, &c, &m))
{
memset(map, 0, sizeof(map));
n = k + c;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%d", &map[i][j]);
if(0 == map[i][j] && i != j) map[i][j] = INT_MAX;
}
}
floyd();
int l = 0, r = 10000, mid;
while(l < r)
{
memset(flow, 0, sizeof(flow));
mid = (l + r) >> 1;
for(int i = 1; i <= k; i++)
{
for(int j = k + 1; j <= n; j++)
{
if(map[i][j] <= mid)
flow[i][j] = 1;
}
}
for(int i = 1; i <= k; i++)
{
flow[0][i] = m;
}
for(int i = k + 1; i <= n; i++)
{
flow[i][n+1] = 1;
}
if(c == ek(0, n+1))
{
r = mid;
}
else
{
l = mid + 1;
}
}
printf("%d\n", l);
}
return 0;
}