又是奶牛。。。

J - Optimal Milking
Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

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
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

今天看大神的比赛,看着看着。。。自己好水!先说说这个题?

其实这个题的思路很简单,就是找出所有奶牛的匹配中存在的奶牛路径最长的匹配的最小值,既然要找最小的,那就需要知道这个值的性质,这个题很简单,所以这个值只有一个性质,那就是满足所有牛都有匹配,这样,只要在枚举的过程中准确的判断是否全部匹配即可,这里需要注意一个细节,就是我们需要求出任意两点的距离最小值,这样只要任意两点的距离为最小值的话,我们就可以将这个图视为完美的图,不需要顾忌这个图在给出数据上的讨论。我觉得这个题不需要贴代码,白话一下。

1,floyed算法,建图:

2,最远距离:这头奶牛很不幸辗转了所有点且每个点都是最大值200(这里假设这些点都相连) = (n-1)*200;最小距离大于0 = 1;

3,二分查找从1 ~ (n-1)*200的这些数中最小的数 m,这个m是所有匹配中匹配权最大的数;

4,匈牙利算法,判断是否在m时所有奶牛都有匹配。

还是上代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 99999999
using namespace std;
int map[300][300],match[300][20],n,k,c,m,vis[300];
void floyed(){
	for(int k=1;k<=n;k++)
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	map[i][j] = min(map[i][j],map[i][k]+map[k][j]);
}
int find1(int u,int bin){
	for(int i=1;i<=k;i++)
	{
		if(map[u][i]<=bin&&!vis[i])
		{
			vis[i] = 1;
			if(match[i][0]<m){
				match[i][++match[i][0]] = u;
			    return 1;
			}
			for(int j = 1;j<=m;j++)
			if(find1(match[i][j],bin)){match[i][j] = u;return 1;}
		}
	}
	return 0;
}
int hungary(int bin){
	memset(match,0,sizeof(match));
	for(int i = k+1;i<=k+c;i++){
		memset(vis,0,sizeof(vis));
			if(!find1(i,bin))return 0;
	}

	return 1;
}
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(!map[i][j])map[i][j] = INF;
		}
		floyed();
		int l = 1;
		int r = (n-1) *200;
		while(l  < r){
			int mid = (l+r)/2;
			if(hungary(mid))r = mid;
			else l = mid +1;
		}
		printf("%d\n",r);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值