POJ 2112 Optimal Milking

链接: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. 

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

解题思路:

这是一道最大流的题目。基本解题思路: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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品类型管理页面,此页面提供给管理员的功能有:新增药品类型,修改药品类型,删除药品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值