Trash (KM算法)

You were just hired as CEO of the local junkyard.One of your jobs is dealing with the incoming trash and sorting it for recycling.The trash comes every day in N containers and each of these containers contains certain amount of each of the N types of trash. Given the amount of trash in the containers find the optimal way to sort the trash. Sorting the trash means putting every type of trash in separate container. Each of the given containers has infinite capacity. The effort for moving one unit of trash from container i to j is 1 if i ≠  j otherwise it is 0.You are to minimize the total effort.
Input
The first line contains the number N (1 ≤  N ≤ 150), the rest of the input contains the descriptions of the containers.The (1 +  i)-th line contains the description of the i-th container the j-th amount (0 ≤ amount ≤ 100) on this line denotes the amount of the j-th type of trash in the i-th container.
Output
You should write the minimal effort that is required for sorting the trash.
Example
input
4
62 41 86 94
73 58 11 12
69 93 89 88
81 40 69 13
output  
650
题意: 有n个垃圾桶,每个垃圾桶内有n种垃圾,现在是要你把垃圾分类,到每一个垃圾桶最后都只能装一种垃圾,从一个垃圾桶里把垃圾移到另一个垃圾桶会有所消耗,且都是单位消耗。。

     那么,要总消耗最少,就要原来每个垃圾桶各自保留的垃圾总和要最多,这么看来,问题就可以转化为带权二分图的最佳匹配问题。

      求出这个最佳匹配  k   最后 ans = s(所有垃圾总和)-k

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define Inf 0x3f3f3f3f
using namespace std;
const int N = 200;
int dx[N],dy[N];
int vx[N],vy[N];
int match[N],slack[N];
int e[N][N];
int n;
int Find(int u){
	vx[u]=1;
	for(int i=1;i<=n;i++){
		if(vy[i]) continue;
	    int gap=dx[u]+dy[i]-e[u][i];
	    if(gap==0){
	    	vy[i]=1;
	    	if(match[i]==-1||Find(match[i])){
	    		match[i]=u;
	    		return 1;
			}
		}
		else slack[i]=min(gap,slack[i]);
	}
	return 0;
}
int KM(){
	memset(dx,0,sizeof(dx));
	memset(dy,0,sizeof(dy));
	memset(match,-1,sizeof(match));
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	dx[i]=max(dx[i],e[i][j]);
	
	for(int i=1;i<=n;i++){
		memset(slack,Inf,sizeof(slack));
		while(1){
			memset(vx,0,sizeof(vx));
			memset(vy,0,sizeof(vy));
			if(Find(i)) break;
			int dis=Inf;
			for(int j=1;j<=n;j++)
			   if(!vy[j]) dis=min(dis,slack[j]);
			for(int j=1;j<=n;j++){
				if(vx[j]) dx[j]-=dis;
				if(vy[j]) dy[j]+=dis;
				else slack[j]-=dis;
			}
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		if(match[i]!=-1) ans+=e[match[i]][i];
	}
	return ans;
}
int main(){
	scanf("%d",&n);
	int sum=0;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++){
		scanf("%d",&e[i][j]);
		sum+=e[i][j];
	}
	printf("%d\n",sum-KM());
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
trash_icra19指的是2021年国际机器人大会和自动化国际会议(ICRA)的垃圾处理主题。ICRA是世界上最重要的机器人和自动化领域的学术会议之一,每年都吸引着来自全球各地的专家学者和工业界人士。 垃圾处理是一个全球性的问题,尤其是在人口增长和城市化的背景下。利用智能机器人技术来改善垃圾处理过程,可以提高效率、降低成本,并减少对人类的影响。 trash_icra19可能涵盖了与垃圾处理有关的各种领域和技术。首先,机器人可以被用于自动化的垃圾收集和分类。例如,智能垃圾箱可以通过传感器识别垃圾的类型和重量,自动将其分类并在合适的时间通知收运车辆。同时,机器人也可以在垃圾堆中进行自主导航和识别,以帮助垃圾工人更快速地处理垃圾。 此外,垃圾处理中的可回收物品的回收利用也是一个重要的方向。智能机器人可以通过识别和分拣可回收物品,提高回收率和资源利用效率。利用机器学习和视觉识别技术,可以让机器人从大量混杂的垃圾中找到有价值的可回收物品。 另一方面,垃圾处理还需要考虑环境保护和可持续发展的因素。trash_icra19可能也涉及到了利用机器人进行垃圾焚烧和废弃物处理的研究。通过探索新的能源回收和清洁技术,可以使垃圾处理过程更加环保和可持续。 总之,trash_icra19可能涵盖了垃圾处理领域的各个方面,包括垃圾收集、分类、回收利用、环境保护等。通过应用智能机器人技术,可以改善垃圾处理过程的效率和可持续性,为建设清洁、健康和可持续的城市提供支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值