Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 19347 | Accepted: 6907 | |
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 2 Sample Output 2 Source |
求行走距离的最远的奶牛的至少要走多远。
注意要先用Floyd求每两点之间的最短路。。。。。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = , INF = 0x3f3f3f3f;
typedef long long LL; int head[maxn], d[maxn], vis[maxn], p[maxn], f[maxn], way[][];
int n, m, s, t, neng;
int cnt, flow, value; struct node{
int u, v, c, w, next;
}Node[]; void add_(int u, int v, int c, int w)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].w = w;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int c, int w)
{
add_(u, v, c, w);
add_(v, u, , -w);
} int spfa()
{
queue<int> Q;
for(int i=; i<maxn; i++) d[i] = INF;
d[s] = ;
mem(vis, );
mem(p, -);
Q.push(s);
vis[s] = ;
p[s] = ; f[s] = INF;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
vis[u] = ;
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] > max(d[e.u], e.w) && e.c > )
{
d[e.v] = max(d[e.u], e.w);
p[e.v] = i;
f[e.v] = min(f[u], e.c);
if(!vis[e.v])
{
Q.push(e.v);
vis[e.v] = ;
}
}
}
}
if(p[t] == -) return ;
// cout<< value <<endl;
flow += f[t], value = d[t];
for(int i=t; i!=s; i=Node[p[i]].u)
{
Node[p[i]].c -= f[t];
Node[p[i]^].c += f[t];
}
return ;
} void max_flow()
{
while(spfa());
printf("%d\n",value);
} int main()
{
mem(head, -);
mem(way, INF);
cnt = ;
scanf("%d%d%d", &n, &m, &neng);
for(int i=; i<=n+m; i++)
way[i][i] = ; for(int i=; i<=n+m; i++)
{
for(int j=; j<=n+m; j++)
{
int w;
scanf("%d",&w);
if(w) way[i][j] = w; }
}
for(int k=;k<=n+m;k++)
for(int i=;i<=n+m;i++)
for(int j=;j<=n+m;j++)
way[i][j]=min(way[i][j],way[i][k]+way[k][j]);
for(int i=; i<=m; i++)
for(int j=; j<=n; j++)
if(way[n+i][j] < INF)
add(n+i, j, , way[n+i][j]); s = ; t = n + m + ;
for(int i=; i<=m; i++)
add(s, n+i, , );
for(int j=; j<=n; j++)
add(j, t, neng, );
max_flow(); return ;
}