poj2195

題意:在一個圖中給出房子和人的位置,一個房子對應一個人,求出這些房子和人的最小距離。

思路:這是一個帶權二分圖,沒法用匈牙利所以直接上最小費用最大流,設立一個超級源到源點的流量爲房子的個數即總流量,價格爲0。

從源點到各個人流量爲1,價格爲0,目的是限制每個人只能被用一次。人和房子直接用距離作爲價格,流量爲1

各個房子到匯點的流量限制也爲1理由和到人的流量限制爲1的理由相同。

建圖以後直接最小費用最大流就可以了毋庸贅言。

#include<iostream>
#include<Cstdio>
#include<string>
using namespace std;

struct Node
{
    int x, y;
};

struct map  
{
	int cap, flow, cost;
};

int count1, count2, s,t, size;
Node list1[500], list2[500];
map edge[500][500];
int vis[10000], dis[10000];
int pre[10000];
int q[20000];
void build(int  m, int n)
{
	int j, i; char temp;
	int tmp1, tmp2;
	count1 = 0; count2 = 0;
	
	for(j = 1; j <= m; j++)
	{
		
		for(i = 1; i <= n; i++)
		{
			
			cin >> temp;
			if(temp == 'm')
			{
				list1[count1].x = i; list1[count1].y = j;
				count1++;
			}
			else if(temp == 'H')
			{
				list2[count2].x = i; list2[count2].y = j;
				count2++;
			}
		}
	}
	
	size = count1 + count2+ 3;
	for(i = 0; i < size; i++)
		for(j = 0; j < size; j++)
		{
			edge[i][j].cost = 1000000;
			edge[i][j].flow = 0;
			edge[i][j].cap = 0;
		}
	
	for(i = 0; i < count1; i++)
	{
		for(j = 0; j < count2; j++)
		{
			tmp1 = list1[i].x - list2[j].x;
			tmp2 = list1[i].y - list2[j].y;
			
			if(tmp1 < 0)
				tmp1 *= -1;
			if(tmp2 < 0)
				tmp2 *= -1;
			
			edge[i][j + count1].cap = 1;
			edge[i][j + count1].cost = tmp1 + tmp2 ;
			edge[j + count1][i].cost = -1 * edge[i][j + count1].cost;

		}
	}
	edge[count1 + count2+ 2][count1 + count2].cap = count1;
	edge[count1 + count2+ 2][count1 + count2].cost = 0;
	edge[count1 + count2][count1 + count2 + 2].cost = 0;
	
	for(i = 0; i < count1; i++)
	{
		edge[count1 + count2][i].cap = 1;
		edge[count1 + count2][i].cost = 0;
		
		edge[i][count1 + count2].cap = 0;
		edge[i][count1 + count2].cost = 0;
	}
	
	for(i = count1 ; i < count1 + count2; i++)
	{
		edge[i][count1 + count2 + 1].cap = 1;
		edge[i][count1 + count2 + 1].cost = 0;
		edge[count1 + count2 + 1][i].cost = 0;
		
	}
	s = count1 + count2+ 2; t = count1 + count2 + 1;	
}

int spfa()
{
	int i, temp;
	for(i = 0; i < size; i++)
	{
		vis[i] = -1;
		dis[i] = 100000;
	}
	
	int front = 0, tail= 1;
	vis[s] = 1; dis[s] = 0;
	q[front] = s;
	while(front < tail)
	{
		
		temp = q[front];
		vis[temp] = -1;
		front++;
		for(i = 0; i < size; i++)
		{
			int res = edge[temp][i].cap - edge[temp][i].flow;
			if(res > 0 && dis[i] > dis[temp] + edge[temp][i].cost)
			{
				dis[i] = dis[temp] + edge[temp][i].cost;
				pre[i] =temp;
			    if(vis[i] == -1)
			    {
				    vis[i] = 1;
				    q[tail++] = i;
			    }
			}
		}
	}
	return dis[t];
}

int MCMF()
{
	int temp, min, flow, sum = 0, cost, val;
	while(spfa() < 8000)
	{  
		temp = t; 
		flow = 100000; cost = 0;
		while(temp != s)
		{
			val = edge[pre[temp]][temp].cap - edge[pre[temp]][temp].flow;
			cost += edge[pre[temp]][temp].cost;
			if(val < flow)
				flow = val;
			temp = pre[temp];
		}
		temp = t;
		sum += flow * cost;
		while(temp != s)
		{
			edge[pre[temp]][temp].flow += flow;
			edge[temp][pre[temp]].flow -= flow;
			temp = pre[temp];
		}
	}
	
	return sum;
}



int main()
{
	int m, n;
	while(1)
	{
		cin >> m >> n;
		if(m == 0 && n == 0)
			break;
		build(m, n);
		cout << MCMF() << endl;
	}
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章